基于C++11的线程池(threadpool)简洁且可以带任意多的参数
基于C++11的线程池(threadpool)简洁且可以带任意多的参数
源代码来自Github上作者progschj,地址为:A simple C++11 Thread Pool implementation,具体博客可以参见Jakob’s Devlog,地址为:A Thread Pool with C++11
blackfeather 2020/10/2 ℃ 0评论
基于C++11的线程池(threadpool)简洁且可以带任意多的参数
源代码来自Github上作者progschj,地址为:A simple C++11 Thread Pool implementation,具体博客可以参见Jakob’s Devlog,地址为:A Thread Pool with C++11
blackfeather 2020/10/2 ℃ 0评论
timer定时器,多线程,回调为阻塞模式。支持参数绑定,非常好使。
用法:
////////////////////////////////////////////////////////////////////////// //以下是测试代码 namespace timertester { class timertestclass { public: timertestclass(){}; ~timertestclass(){}; void testfn() { printf("timer callback is class func.\n"); FTimerEvent mTimer(3000, &timertestclass::threadfn, this); for (int i = 0; i < 5; i++) { printf("press any key to ...\n"); getchar(); mTimer.SetTimerEvent(); } printf("stop timer \n"); mTimer.StopTimer(); } void threadf(int i) { printf("test f i:%d\n", i); } void threadfn() { printf("test fn d\n"); } void threadfn2() { printf("test fn2222 \n"); } }; static void testfn1(timertestclass *cls) { cls->threadfn(); } static void testpf() { printf("test printf \n"); printf("callback ret. \n"); } static void prt(int& i) { printf("print %d\n", i); } static void timertest() { int t = 0; FTimerEvent timerstdref(1000, prt, std::ref(t)); for (int i = 0; i < 10; i++) { t = i; Sleep(1000); } timerstdref.StopTimer(); { printf("timer 0 manual to set\n"); //如果定时为0,则每次为手动触发 FTimerEvent timerman(0, [](){ printf("timerman in.\n"); Sleep(5000); printf("timerman out.\n"); }); timerman.SetTimerEvent(); timerman.SetTimerEvent(); timerman.StopTimer(); } printf("stop timer in callback\n"); FTimerEvent timer4; timer4.InitTimer(1000, [](FTimerEvent *pTimer){ printf("exit timer\n"); pTimer->StopTimer(); }, &timer4); Sleep(3000); timer4.StopTimer(); printf("set timer in callback\n"); FTimerEvent timer5; timer5.InitTimer(2000, [](FTimerEvent *pTimer){ static bool set = false; printf("timer in\n"); if (!set) { printf("set timer\n"); pTimer->SetTimerEvent(); set = true; } printf("timer out\n"); }, &timer5); Sleep(10000); timer5.StopTimer(); timertestclass test1; test1.testfn(); int x = 0; FTimerEvent timerref(1000, [&x]() { printf("x: %d \n", x); }); for (int i = 0; i < 10; i++) { x = i; Sleep(1000); } timerref.StopTimer(); FTimerEvent timerx(1000, [&test1]() { test1.threadfn2(); }); Sleep(10000); timerx.StopTimer(); FTimerEvent timer0(1000, testpf); Sleep(10000); timer0.StopTimer(); FTimerEvent timer1(1000, testfn1, &test1); Sleep(10000); timer1.StopTimer(); FTimerEvent timer2(1000, [](){ printf("lambada no param \n"); }); Sleep(10000); timer2.StopTimer(); int param = 0; FTimerEvent timer3(1000, [](int *p){ printf("lambada with param: %d \n", *p); }, ¶m); Sleep(10000); timer3.StopTimer(); } }
blackfeather 2020/9/7 ℃ 0评论
基于openssl库写的aes_gcm解密方法
兼容gcm 128、192、256位,兼容变长iv算法,与python和java的gcmdecrypt方法结果保持一致。
最恶心的地方在iv长度,要手动设置,否则某些情况下会与python或者java的结果不一致(代码的24行)。
std::string AesGCMDecrypt(const std::string &strKey, const std::string &strIV, const std::string &strBuffer, int nPadding/* = 1*/) { std::string strRet; if (strBuffer.empty()) return strRet; const EVP_CIPHER *cipher; if (strKey.length() == 16) cipher = EVP_aes_128_gcm(); else if (strKey.length() == 24) cipher = EVP_aes_192_gcm(); else if (strKey.length() == 32) cipher = EVP_aes_256_gcm(); else return strRet; unsigned char *pszDecode = new unsigned char[strBuffer.length() + 32]; ZeroMemory(pszDecode, strBuffer.length() + 32); unsigned char *pszOut = pszDecode; int nDecodeLen, nTotalLen = 0; EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new(); EVP_DecryptInit_ex(ctx, cipher, NULL, NULL, NULL); if (strIV.length() != 12) EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, strIV.length(), NULL); EVP_DecryptInit_ex(ctx, NULL, NULL, (const unsigned char *)strKey.c_str(), (const unsigned char *)strIV.c_str()); EVP_CIPHER_CTX_set_padding(ctx, nPadding); EVP_DecryptUpdate(ctx, pszOut, &nDecodeLen, (const unsigned char *)strBuffer.c_str(), strBuffer.length()); nTotalLen = nDecodeLen; pszOut += nDecodeLen; EVP_DecryptFinal(ctx, pszOut, &nDecodeLen); nTotalLen += nDecodeLen; strRet.assign((const char *)pszDecode, nTotalLen); delete[] pszDecode; EVP_CIPHER_CTX_free(ctx); return strRet; }
blackfeather 2020/9/1 ℃ 1评论
在C/C++编码中,open对close,new对delete,malloc对free等等是严格要求的,否则会有各种泄露的奇葩情况。
但是实际编码中,各种异常捕捉和错误判断又让编码变得繁琐臃肿。
后来在使用golang中,defer方法使用非常方便,C++虽然没有自带的,但是可以写嘛!
借助C++类的析构方法和lambada表达式,还有std::function容器,很容易写出来一个自己的defer(以下代码是复制来的,感觉非常的干练,自己写的不好看不贴了,后来也用了这个.h)
blackfeather 2020/9/1 ℃ 0评论
有缘人拿去。。。RFC 3394,AESUnwrap。。。某些情况下使用的。
uint8_t AES_unwrap(uint8_t *kek, uint16_t key_len, uint8_t *cipher_text, uint16_t cipher_len, uint8_t *output) { uint8_t a[8], b[16]; uint8_t *r; uint8_t *c; uint16_t i, j, n; AES_KEY ctx; if (! kek || cipher_len < 16 || ! cipher_text || ! output) { /* We don't do anything with the return value */ return 1; } /* Initialize variables */ n = (cipher_len/8)-1; /* the algorithm works on 64-bits at a time */ memcpy(a, cipher_text, 8); r = output; c = cipher_text; memcpy(r, c+8, cipher_len - 8); /* Compute intermediate values */ for (j=5; j >= 0; --j) { r = output + (n - 1) * 8; /* DEBUG_DUMP("r1", (r-8), 8); */ /* DEBUG_DUMP("r2", r, 8); */ for (i = n; i >= 1; --i) { uint16_t t = (n*j) + i; /* DEBUG_DUMP("a", a, 8); */ memcpy(b, a, 8); b[7] ^= t; /* DEBUG_DUMP("a plus t", b, 8); */ memcpy(b+8, r, 8); AES_set_decrypt_key(kek, 128, &ctx); AES_decrypt(b, b, &ctx); /* NOTE: we are using the same src and dst buffer. It's ok. */ /* DEBUG_DUMP("aes decrypt", b, 16) */ memcpy(a,b,8); memcpy(r, b+8, 8); r -= 8; } } /* DEBUG_DUMP("a", a, 8); */ /* DEBUG_DUMP("output", output, cipher_len - 8); */ return 0; }
blackfeather 2019/6/25 ℃ 0评论
scrypt算法,不多解释。
可直接在windows、linux、android(ndk)上编译。
crypto_scrypt.h
/*- * Copyright 2009 Colin Percival * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * This file was originally written by Colin Percival as part of the Tarsnap * online backup system. */ #ifndef _CRYPTO_SCRYPT_H_ #define _CRYPTO_SCRYPT_H_ #include <stdint.h> /** * crypto_scrypt(passwd, passwdlen, salt, saltlen, N, r, p, buf, buflen): * Compute scrypt(passwd[0 .. passwdlen - 1], salt[0 .. saltlen - 1], N, r, * p, buflen) and write the result into buf. The parameters r, p, and buflen * must satisfy r * p < 2^30 and buflen <= (2^32 - 1) * 32. The parameter N * must be a power of 2 greater than 1. * * Return 0 on success; or -1 on error. */ int crypto_scrypt(const uint8_t *, size_t, const uint8_t *, size_t, uint64_t, uint32_t, uint32_t, uint8_t *, size_t); #endif /* !_CRYPTO_SCRYPT_H_ */
blackfeather 2019/6/19 ℃ 0评论
安卓很早就开始启用了gatekeeper的锁屏密码机制,如果未启用TEE的情况下,是默认使用“软加密”的方式,可以离线破解出原始密码,知道原始密码在某些情况下还是比较有用的,以下是破解实例,自行调整。
import struct import binascii import scrypt N = 16384; r = 8; p = 1; f = open('gatekeeper.password.key', 'rb') blob = f.read() s = struct.Struct('<'+'17s 8s 32s') (meta, salt, signature) = s.unpack_from(blob) for one in "0123456789": for two in "0123456789": for three in "0123456789": for four in "0123456789": password = one + two + three + four; print 'pass: %s' % password to_hash = meta to_hash += password hash = scrypt.hash(to_hash, salt, N, r, p) print 'signature %s' % signature.encode('hex') print 'Hash: %s' % hash[0:32].encode('hex') if hash[0:32] == signature: print "OK" exit()
blackfeather 2019/6/19 ℃ 0评论
工作中经常用到protobuf的场景,但是由于protobuf不是明文信息,在调试和检查时不是很直观,于是就有转换为json的需求,方便排查。
json使用的jsoncpp库,自行补上头文件等。
void _field2json(const Message& msg, const FieldDescriptor *field, size_t index, Json::Value &jsonValue) { const Reflection *ref = msg.GetReflection(); const bool repeated = field->is_repeated(); switch (field->cpp_type()) { #define _CONVERT(type, ctype, sfunc, afunc) \ case FieldDescriptor::type: { \ const ctype value = (repeated)? \ ref->afunc(msg, field, index): \ ref->sfunc(msg, field); \ jsonValue = value; \ break; \ } _CONVERT(CPPTYPE_DOUBLE, double, GetDouble, GetRepeatedDouble); _CONVERT(CPPTYPE_FLOAT, double, GetFloat, GetRepeatedFloat); _CONVERT(CPPTYPE_INT64, INT64, GetInt64, GetRepeatedInt64); _CONVERT(CPPTYPE_UINT64, UINT64, GetUInt64, GetRepeatedUInt64); _CONVERT(CPPTYPE_INT32, INT32, GetInt32, GetRepeatedInt32); _CONVERT(CPPTYPE_UINT32, UINT32, GetUInt32, GetRepeatedUInt32); _CONVERT(CPPTYPE_BOOL, bool, GetBool, GetRepeatedBool); #undef _CONVERT case FieldDescriptor::CPPTYPE_STRING: { std::string scratch; const std::string &value = (repeated) ? ref->GetRepeatedStringReference(msg, field, index, &scratch) : ref->GetStringReference(msg, field, &scratch); if (field->type() == FieldDescriptor::TYPE_BYTES) jsonValue = base64_encode((const unsigned char *)value.c_str(), value.length()); else jsonValue = value.c_str(); break; } case FieldDescriptor::CPPTYPE_MESSAGE: { const Message& mf = (repeated) ? ref->GetRepeatedMessage(msg, field, index) : ref->GetMessage(msg, field); jsonValue = Protobuf2Json(mf); break; } case FieldDescriptor::CPPTYPE_ENUM: { const EnumValueDescriptor* ef = (repeated) ? ref->GetRepeatedEnum(msg, field, index) : ref->GetEnum(msg, field); jsonValue = ef->number(); break; } default: break; } //if (!jf) throw j2pb_error(field, "Fail to convert to json"); //return jsonRet; } Json::Value Protobuf2Json(const Message &msg) { Json::Value jsonRet; const Descriptor *d = msg.GetDescriptor(); const Reflection *ref = msg.GetReflection(); if (!d || !ref) return jsonRet; std::vector<const FieldDescriptor *> fields; ref->ListFields(msg, &fields); for (size_t i = 0; i != fields.size(); i++) { const FieldDescriptor *field = fields[i]; const std::string &name = (field->is_extension()) ? field->full_name() : field->name(); if (field->is_repeated()) { size_t count = ref->FieldSize(msg, field); if (!count) continue; for (size_t j = 0; j < count; j++) _field2json(msg, field, j, jsonRet[name][j]); } else if (ref->HasField(msg, field)) _field2json(msg, field, 0, jsonRet[name]); else continue; } return jsonRet; }
blackfeather 2019/6/6 ℃ 0评论
安卓相册中有一个隐藏目录,里面有一个thumbdata后缀的文件,经常会非常大(几个G),这就是类似于电脑的thumb.db一样是图片的缩略图缓存文件,用于加速相册等应用显示的。
如果相册里面的照片删除后,这个缓存里面的是不会自动清理的,所以经常被利用来恢复照片,缺点是缩略图,太小了。
这个缓存数据结构是块的,具体也不解释了,直接贴代码好了:
import os from struct import * currpath = "D:\\recovery" filename = ".thumbdata4--1967290299" offset=0 file_size=os.path.getsize(filename) f=open(filename, "rb") while(offset<file_size): f.seek(offset) tmp=f.read(10000) if tmp[0]=="\x01": #print offset #normal mode and xiaomi mode if tmp[21]!="\xFF": #normal mode tmp_magic_code=tmp[1:9] tmp_jpg_size=tmp[9:13] #magic_code=unpack("i",tmp_magic_code[::-1]) jpg_size=unpack("I",tmp_jpg_size[::-1]) #print jpg_size f2=open(currpath + "\\" + str(offset/10000) + ".jpg","wb") f.seek(offset + 13) f2.write(f.read(int(jpg_size[0]))) #print "extracting " + str(offset/10000) + ".jpg" else: tmp_magic_code = tmp[1:17] tmp_jpg_size = tmp[17:21] jpg_size=unpack("I",tmp_jpg_size[::-1]) #print jpg_siz f2 = open(currpath + "\\" + str(offset / 10000) + ".jpg", "wb") f.seek(offset + 21) f2.write(f.read(int(jpg_size[0]))) #print "extracting " + str(offset / 10000) + ".jpg" offset+=10000 print "All Done!"
blackfeather 2019/3/15 ℃ 0评论
IOS通过备份,得到的目录里面是一片乱码(严格来说是sha1编码)。解析出具体的文件列表的分析有很多了,整理了一下,写出来。
大概介绍下,这个文件的开头是“mbdb”,前六个字节一般是固定的“mbdb\05\00”,然后就是一个个结构体。结构体大概是这样子的:
struct fileinfo{ char *domain; char *filename; char *linktarget; char *datahash; char *unknown1; unsigned short mode; unsigned int unknown2; unsigned int inode; unsigned int userid; unsigned int groupid; unsigned int mtime; unsigned int atime; unsigned int ctime; unsigned long long filelen; unsigned int flag; unsigned int numprops; };
domain就是包名,每个应用都不同。filename就是path。datahash发现现在都是空的了,无用。最后就是那个numprops是附加数据,这个必须处理。flag为0是文件夹或者link,也可以通过filelen来判断是否为文件。
blackfeather 2016/7/25 ℃ 1评论