基于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评论
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评论
Tea算法被某公司应用的酣畅淋漓,最近用到了,在测试了多分代码后,推荐此代码。保留原作者信息。
cpypt.cpp
/** * The QQ2003C protocol plugin * * for gaim * * Copyright © 2004 Puzzlebird * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * ************************************************** * Reorganized by Minmin <csdengxm@hotmail.com>, 2005-3-27 ************************************************** */ #include "stdafx.h" #include "crypt.h" #include <string.h> #ifdef _WIN32 #include <winsock.h> #else #include <arpa/inet.h> #endif #include <cstring> Crypt::Crypt() { } Crypt::~Crypt() { } void Crypt::teaEncipher(unsigned int *const v, const unsigned int *const k, unsigned int *const w) { register unsigned int y = ntohl(v[0]), z = ntohl(v[1]), a = ntohl(k[0]), b = ntohl(k[1]), c = ntohl(k[2]), d = ntohl(k[3]), n = 0x10, /* do encrypt 16 (0x10) times */ sum = 0, delta = 0x9E3779B9; /* 0x9E3779B9 - 0x100000000 = -0x61C88647 */ while (n-- > 0) { sum += delta; y += ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b); z += ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d); } w[0] = htonl(y); w[1] = htonl(z); } void Crypt::teaDecipher(unsigned int *const v, const unsigned int *const k, unsigned int *const w) { register unsigned int y = ntohl(v[0]), z = ntohl(v[1]), a = ntohl(k[0]), b = ntohl(k[1]), c = ntohl(k[2]), d = ntohl(k[3]), n = 0x10, sum = 0xE3779B90, /* why this ? must be related with n value*/ delta = 0x9E3779B9; void *p = &a; /* sum = delta<<5, in general sum = delta * n */ while (n-- > 0) { z -= ((y << 4) + c) ^ (y + sum) ^ ((y >> 5) + d); y -= ((z << 4) + a) ^ (z + sum) ^ ((z >> 5) + b); sum -= delta; } w[0] = htonl(y); w[1] = htonl(z); } void Crypt::encrypt( unsigned char* instr, int32 instrlen, unsigned char* key, unsigned char* outstr, int32* outstrlen_ptr) { unsigned char plain[8], /* plain text buffer*/ plain_pre_8[8], /* plain text buffer, previous 8 bytes*/ * crypted, /* crypted text*/ * crypted_pre_8, /* crypted test, previous 8 bytes*/ * inp; /* current position in instr*/ int pos_in_byte = 1, /* loop in the byte */ is_header=1, /* header is one byte*/ count=0, /* number of bytes being crypted*/ padding = 0; /* number of padding stuff*/ //void encrypt_every_8_byte (void); /*** we encrypt every eight byte ***/ #define encrypt_every_8_byte() \ {\ for(pos_in_byte=0; pos_in_byte<8; pos_in_byte++) {\ if(is_header) { plain[pos_in_byte] ^= plain_pre_8[pos_in_byte]; }\ else { plain[pos_in_byte] ^= crypted_pre_8[pos_in_byte]; }\ } /* prepare plain text*/\ teaEncipher( (unsigned int *) plain,\ (unsigned int *) key, \ (unsigned int *) crypted); /* encrypt it*/\ \ for(pos_in_byte=0; pos_in_byte<8; pos_in_byte++) {\ crypted[pos_in_byte] ^= plain_pre_8[pos_in_byte]; \ } \ memcpy(plain_pre_8, plain, 8); /* prepare next*/\ \ crypted_pre_8 = crypted; /* store position of previous 8 byte*/\ crypted += 8; /* prepare next output*/\ count += 8; /* outstrlen increase by 8*/\ pos_in_byte = 0; /* back to start*/\ is_header = 0; /* and exit header*/\ }/* encrypt_every_8_byte*/ pos_in_byte = (instrlen + 0x0a) % 8; /* header padding decided by instrlen*/ if (pos_in_byte) { pos_in_byte = 8 - pos_in_byte; } plain[0] = (rand() & 0xf8) | pos_in_byte; memset(plain+1, rand()&0xff, pos_in_byte++); memset(plain_pre_8, 0x00, sizeof(plain_pre_8)); crypted = crypted_pre_8 = outstr; padding = 1; /* pad some stuff in header*/ while (padding <= 2) { /* at most two byte */ if(pos_in_byte < 8) { plain[pos_in_byte++] = rand() & 0xff; padding ++; } if(pos_in_byte == 8){ encrypt_every_8_byte(); } } inp = instr; while (instrlen > 0) { if (pos_in_byte < 8) { plain[pos_in_byte++] = *(inp++); instrlen --; } if (pos_in_byte == 8){ encrypt_every_8_byte(); } } padding = 1; /* pad some stuff in tailer*/ while (padding <= 7) { /* at most sever byte*/ if (pos_in_byte < 8) { plain[pos_in_byte++] = 0x00; padding ++; } if (pos_in_byte == 8){ encrypt_every_8_byte(); } } *outstrlen_ptr = count; } int Crypt::rand(void) { /* it can be the real random seed function*/ return 0xdead; /* override with number, convenient for debug*/ } int Crypt::decrypt( unsigned char* instr, int32 instrlen, unsigned char* key, unsigned char* outstr, int32* outstrlen_ptr) { unsigned char decrypted[8], m[8], * crypt_buff, * crypt_buff_pre_8, * outp; int count, context_start, pos_in_byte, padding; #define decrypt_every_8_byte() {\ bool bNeedRet = false;\ for (pos_in_byte = 0; pos_in_byte < 8; pos_in_byte ++ ) {\ if (context_start + pos_in_byte >= instrlen) \ {\ bNeedRet = true;\ break;\ }\ decrypted[pos_in_byte] ^= crypt_buff[pos_in_byte];\ }\ if( !bNeedRet ) { \ teaDecipher( (unsigned int *) decrypted, \ (unsigned int *) key, \ (unsigned int *) decrypted);\ \ context_start += 8;\ crypt_buff += 8;\ pos_in_byte = 0;\ }\ }/* decrypt_every_8_byte*/ /* at least 16 bytes and %8 == 0*/ if ((instrlen % 8) || (instrlen < 16)) return 0; /* get information from header*/ teaDecipher( (unsigned int *) instr, (unsigned int *) key, (unsigned int *) decrypted); pos_in_byte = decrypted[0] & 0x7; count = instrlen - pos_in_byte - 10; /* this is the plaintext length*/ /* return if outstr buffer is not large enought or error plaintext length*/ if (*outstrlen_ptr < count || count < 0) return 0; memset(m, 0, 8); crypt_buff_pre_8 = m; *outstrlen_ptr = count; /* everything is ok! set return string length*/ crypt_buff = instr + 8; /* address of real data start */ context_start = 8; /* context is at the second 8 byte*/ pos_in_byte ++; /* start of paddng stuffv*/ padding = 1; /* at least one in header*/ while (padding <= 2) { /* there are 2 byte padding stuff in header*/ if (pos_in_byte < 8) { /* bypass the padding stuff, none sense data*/ pos_in_byte ++; padding ++; } if (pos_in_byte == 8) { crypt_buff_pre_8 = instr; //if (! decrypt_every_8_byte()) return 0; decrypt_every_8_byte(); } }/* while*/ outp = outstr; while(count !=0) { if (pos_in_byte < 8) { *outp = crypt_buff_pre_8[pos_in_byte] ^ decrypted[pos_in_byte]; outp ++; count --; pos_in_byte ++; } if (pos_in_byte == 8) { crypt_buff_pre_8 = crypt_buff - 8; //if (! decrypt_every_8_byte()) return 0; decrypt_every_8_byte(); } }/* while*/ for (padding = 1; padding < 8; padding ++) { if (pos_in_byte < 8) { if (crypt_buff_pre_8[pos_in_byte] ^ decrypted[pos_in_byte]) return 0; pos_in_byte ++; } if (pos_in_byte == 8 ) { crypt_buff_pre_8 = crypt_buff; //if (! decrypt_every_8_byte()) return 0; decrypt_every_8_byte(); } }/* for*/ return 1; } int Crypt::qq_crypt ( unsigned char flag, unsigned char* instr, int32 instrlen, unsigned char* key, unsigned char* outstr, int32* outstrlen_ptr) { if (flag == DECRYPT) return decrypt(instr, instrlen, key, outstr, outstrlen_ptr); else if (flag == ENCRYPT) encrypt(instr, instrlen, key, outstr, outstrlen_ptr); return 1; /* flag must be DECRYPT or ENCRYPT*/ }/* qq_crypt*/
blackfeather 2016/4/18 ℃ 0评论