void calculateAddress(const uint8_t* data, size_t size, char* nemAddress) { unsigned char sha3result[32]; Keccak_HashInstance _hctx, *hctx = &_hctx; Keccak_HashInitialize_SHA3_256(hctx); Keccak_HashUpdate(hctx, data, size * 8); Keccak_HashSqueeze(hctx, sha3result, 256); unsigned char r160result[25]; computeRIPEMD160(sha3result, 32, r160result + 1); r160result[0] = 0x68; Keccak_HashInitialize_SHA3_256(hctx); Keccak_HashUpdate(hctx, r160result, 21 * 8); Keccak_HashSqueeze(hctx, sha3result, 256); *(uint32_t*)(r160result + 21) = *(uint32_t*)(sha3result); base32_encode(r160result, 25, (unsigned char*)nemAddress); nemAddress[40] = 0; }
static PyObject * SHA3_update(SHAobject *self, PyObject *args) { unsigned char *cp; int len; if (!PyArg_ParseTuple(args, "s#:update", &cp, &len)) return NULL; Keccak_HashUpdate(&self->state, cp, len * 8); Py_INCREF(Py_None); return Py_None; }
unsigned char *SHA3_512(const unsigned char *dataIn, size_t nBytesIn, unsigned char *md) { Keccak_HashInstance h; static BitSequence m[SHA3_512_DL]; if (md == NULL) { md = m; } Keccak_HashInitialize_SHA3_512(&h); Keccak_HashUpdate(&h, dataIn, (DataLength)nBytesIn * 8); Keccak_HashFinal(&h, md); return(md); }
static int crypt_all(int *pcount, struct db_salt *salt) { const int count = *pcount; int index = 0; #ifdef _OPENMP #pragma omp parallel for #endif for (index = 0; index < count; index++) { Keccak_HashInstance hash; Keccak_HashInitialize(&hash, 1088, 512, 256, 0x01); Keccak_HashUpdate(&hash, (unsigned char*)saved_key[index], saved_len[index] * 8); Keccak_HashFinal(&hash, (unsigned char*)crypt_out[index]); } return count; }
int SHAKE80(const unsigned char *dataIn, size_t nBitsIn, unsigned char *md, int nOutBytes) { Keccak_HashInstance h; if (md == NULL || nOutBytes == 0) { return 0; } if (nOutBytes > SHAKE_MAX_BITS / 8) { nOutBytes = SHAKE_MAX_BITS / 8; } Keccak_HashInitialize(&h, SHAKE80_R, SHAKE80_C, 0, SHAKE80_P); Keccak_HashUpdate(&h, dataIn, (DataLength)nBitsIn); Keccak_HashFinal(&h, NULL); Keccak_HashSqueeze(&h, md, nOutBytes * 8); return nOutBytes; }
int main(int argc, char* argv[]) { if (argc < 3) { print_usage(); } int hash_size = atoi(argv[1]); Keccak_HashInstance hash; HashReturn initResult = FAIL; if (hash_size == 224) { initResult = Keccak_HashInitialize_SHA3_224(&hash); } else if (hash_size == 256) { initResult = Keccak_HashInitialize_SHA3_256(&hash); } else if (hash_size == 384) { initResult = Keccak_HashInitialize_SHA3_384(&hash); } else if (hash_size == 512) { initResult = Keccak_HashInitialize_SHA3_512(&hash); } else { printf("Error : wrong size argument\nAvailable sizes : 224, 256, 384, 512\n"); exit(0); } if (initResult != SUCCESS) { printf("error when itializing SHA-3\n"); return -1; } if (Keccak_HashUpdate(&hash, (unsigned char*)argv[STRING_POSITION], strlen(argv[STRING_POSITION]) * 8) != SUCCESS) { printf("error when updating SHA-3\n"); return -1; } int bytes_hash_size = hash_size / 8; unsigned char* output = malloc(bytes_hash_size); if (output == NULL) { printf("memory allocation error\n"); return -1; } if (Keccak_HashFinal(&hash, output) != SUCCESS) { printf("error when extracting the result hash\n"); return -1; } int i; for (i = 0; i < bytes_hash_size; i++) { printf("%02x", (unsigned int)output[i]); } putchar('\n'); return 0; }