/// for ASC mode static std::string& encodeAsc(const RdResponse& rsp, std::string& buff) { Buffer binary; encode(rsp, binary); buff.append(bytesToHex(binary.rd_ptr(), binary.readable_bytes())); return buff; }
/* * pg_md5_hash * * Calculates the MD5 sum of the bytes in a buffer. * * SYNOPSIS #include "md5.h" * int pg_md5_hash(const void *buff, size_t len, char *hexsum) * * INPUT buff the buffer containing the bytes that you want * the MD5 sum of. * len number of bytes in the buffer. * * OUTPUT hexsum the MD5 sum as a '\0'-terminated string of * hexadecimal digits. an MD5 sum is 16 bytes long. * each byte is represented by two heaxadecimal * characters. you thus need to provide an array * of 33 characters, including the trailing '\0'. * * RETURNS false on failure (out of memory for internal buffers) or * true on success. * * STANDARDS MD5 is described in RFC 1321. * * AUTHOR Sverre H. Huseby <*****@*****.**> * */ bool pg_md5_hash(const void *buff, size_t len, char *hexsum) { uint8 sum[16]; if (!calculateDigestFromBuffer((uint8 *) buff, len, sum)) return false; bytesToHex(sum, hexsum); return true; }
/* * pool_md5_hash * * Calculates the MD5 sum of the bytes in a buffer. * * SYNOPSIS int md5_hash(const void *buff, size_t len, char *hexsum) * * INPUT buff the buffer containing the bytes that you want * the MD5 sum of. * len number of bytes in the buffer. * * OUTPUT hexsum the MD5 sum as a '\0'-terminated string of * hexadecimal digits. an MD5 sum is 16 bytes long. * each byte is represented by two heaxadecimal * characters. you thus need to provide an array * of 33 characters, including the trailing '\0'. * * RETURNS false on failure (out of memory for internal buffers) or * true on success. * * STANDARDS MD5 is described in RFC 1321. * * AUTHOR Sverre H. Huseby <*****@*****.**> * MODIFIED by Taiki Yamaguchi <*****@*****.**> * */ int md5_hash(const void *buff, size_t len, char *hexsum) { uint8 sum[16]; if (!calculateDigestFromBuffer((uint8 *) buff, len, sum)) return 0; /* failed */ bytesToHex(sum, hexsum); return 1; /* success */ }
char *encode(EncodeFormat format, const char *src, size_t srclen, size_t &dstlen, size_t linelen) { switch (format) { case ENCODE_BASE64: default: return b64_encode(src, srclen, linelen, dstlen); case ENCODE_HEX: return bytesToHex((const uint8 *) src, srclen, dstlen); } }
static error_t cmd_aes(int argc, char** argv) { uint8_t* key = NULL; uint8_t* iv = NULL; uint8_t* data = NULL; uint8_t* buff = NULL; uint32_t keyType; uint32_t keyLength; uint32_t ivLength; uint32_t dataLength; if(argc < 4) { bufferPrintf("Usage: %s <enc/dec> <gid/uid/key> [data] [iv]\r\n", argv[0]); return -1; } if(strcmp(argv[2], "gid") == 0) { keyType = 512; } else if(strcmp(argv[2], "uid") == 0) { keyType = 513; } else { hexToBytes(argv[2], &key, (int*)&keyLength); switch(keyLength*8) { case 128: keyType = 0 << 28; break; case 192: keyType = 1 << 28; break; case 256: keyType = 2 << 28; break; default: bufferPrintf("Usage: %s <enc/dec> <gid/uid/key> [data] [iv]\r\n", argv[0]); goto return_free; } } hexToBytes(argv[3], &data, (int*)&dataLength); buff = memalign(DMA_ALIGN, dataLength); if (!buff) { bufferPrintf("out of memory.\r\n"); goto return_free; } memcpy(buff, data, dataLength); free(data); data = NULL; if(argc > 4) { hexToBytes(argv[4], &iv, (int*)&ivLength); } if(strcmp(argv[1], "enc") == 0) aes_crypto_cmd(0x10, buff, buff, dataLength, keyType, key, iv); else if(strcmp(argv[1], "dec") == 0) aes_crypto_cmd(0x11, buff, buff, dataLength, keyType, key, iv); else { bufferPrintf("Usage: %s <enc/dec> <gid/uid/key> [data] [iv]\r\n", argv[0]); goto return_free; } bytesToHex(buff, dataLength); bufferPrintf("\r\n"); return_free: if (data) free(data); if (iv) free(iv); if (key) free(key); if (buff) free(buff); return 0; }
int run_tests() { int numBytes; unsigned char *asHex; // // fromBase58() - bad input // asHex = fromBase58("Lbwb5yufe3TCSvjwoAUrk9FORe7aeJ62YTo6ABDiFts7ovY8tcak"); assert(asHex == NULL); // // fromBase58() - good input // asHex = fromBase58("Lbwb5yufe3TCSvjwoAUrk9FzRe7aeJ62YTo6ABDiFts7ovY8tcak"); assert(asHex != NULL); assert(strlen(asHex) == 76); assert(strncmp( "84835d5042486a38e5e93345649ace3943efddc643b3c0ecc3d144e654113682ce7d4882f799", asHex, 76) == 0); unsigned char keyHex[65]; memcpy (keyHex, asHex + 2, 64); keyHex[64] = '\0'; free(asHex); // // bytesToHex(hexToBytes(str)) == str // char *inputHex = "0450863AD64A87AE8A2FE83C1AF1A8403CB53F53E486D8511DAD8A04887E5B2355"; unsigned char* inputBytes = hexToBytes((unsigned char*) inputHex, &numBytes); unsigned char* outputHex = bytesToHex(inputBytes, numBytes); assert (inputBytes != NULL); assert (outputHex != NULL); assert (strncmp((char*)inputHex, (char*) outputHex, strlen(inputHex)) == 0); free(inputBytes); free(outputHex); // // hexToBytes - bad input // unsigned char* secret; secret = hexToBytes("A", &numBytes); assert(secret == NULL); secret = hexToBytes("AG", &numBytes); assert(secret == NULL); // // hexToBytes - good input // secret = hexToBytes(keyHex, &numBytes); assert(secret != NULL); assert(numBytes == 32); unsigned char *privateB58U; unsigned char *publicB58U; unsigned char *privateB58C; unsigned char *publicB58C; privateB58U = secretBytesToPrivate(secret, 32, 0); publicB58U = secretBytesToPublic(secret, 32, 0); assert(privateB58U != NULL); assert(publicB58U != NULL); assert(strncmp(privateB58U, "5Jp97RMBQfEy5xDCuz59HQzL8TMPGmrsVvzT37zAotrE53brxHx", strlen(privateB58U)) == 0); assert(strncmp(publicB58U, "1LEyj89YU4qGfFk7dc1jpGAYX5DhQm9moP", strlen(publicB58U)) == 0); privateB58C = secretBytesToPrivate(secret, 32, 1); publicB58C = secretBytesToPublic(secret, 32, 1); assert(privateB58C != NULL); assert(publicB58C != NULL); assert(strncmp(privateB58C, "L1d4oLU4wRneYrgsaruHZSt3RuJyJd17thJZvMqLHyrGgMq74H5L", strlen(privateB58C)) == 0); assert(strncmp(publicB58C, "1PaJnioooVnq5oMgF4wYNer6rVyxZLaTqf", strlen(publicB58C)) == 0); free(secret); free(privateB58U); free(publicB58U); free(privateB58C); free(publicB58C); return 0; }
void cmd_aes(int argc, char** argv) { AESKeyType keyType; uint8_t* data = NULL; uint8_t* key = NULL; uint8_t* iv = NULL; int dataLength; int keyLength; int ivLength; if(argc < 4) { bufferPrintf("Usage: %s <enc/dec> <gid/uid/key> [data] [iv]\r\n", argv[0]); return; } if(strcmp(argv[2], "gid") == 0) { keyType = AESGID; } else if(strcmp(argv[2], "uid") == 0) { keyType = AESUID; } else { hexToBytes(argv[2], &key, &keyLength); keyType = AESCustom; } hexToBytes(argv[3], &data, &dataLength); if(argc > 4) { hexToBytes(argv[4], &iv, &ivLength); } if(strcmp(argv[1], "enc") == 0) { aes_encrypt(data, dataLength, keyType, key, iv); bytesToHex(data, dataLength); bufferPrintf("\r\n"); } else if(strcmp(argv[1], "dec") == 0) { aes_decrypt(data, dataLength, keyType, key, iv); bytesToHex(data, dataLength); bufferPrintf("\r\n"); } else { bufferPrintf("Usage: %s <enc/dec> <GID/UID/key> [data] [iv]\r\n", argv[0]); } if(data) free(data); if(iv) free(iv); if(key) free(key); }