static bool check( bool ok, const uint8_t *rmdHex, const uint8_t *dataHex ) { uint8_t rmd[kRIPEMD160ByteSize]; fromHex(rmd, rmdHex, kRIPEMD160ByteSize, false); uint8_t data[1024]; auto dataLen = strlen((const char*)dataHex)/2; if(0<dataLen) { fromHex(data, dataHex, dataLen, false); } uint8_t cmp[kRIPEMD160ByteSize]; rmd160(cmp, data, dataLen); TEST_CHECK( ok, 0==memcmp(cmp, rmd, sizeof(rmd)), "RIPEMD-160 fails for %s\n", dataHex ); return ok; }
int ripemd(const char *fpt, unsigned long *IH, unsigned long type) { unsigned long c = 0x00000000UL; unsigned long e = 0x00000000UL; unsigned long i = 0x00000000UL; union { unsigned long long qword ; unsigned long dword[0x02]; } byte; FILE *fp = NULL ; union { unsigned long dword[0x10]; unsigned short word[0x20]; unsigned char byte[0x40]; } M; if(strcmp(fpt, "-")) { if(checkf(fpt) == 1) return 1; fp = fopen(fpt, "rb"); } else fp = stdin; if(fpt && fp) { byte.qword = 0x0000000000000000ULL; c = 0x00000001UL; for(e=0; e<0x08; e++) IH[e] = 0x00000000UL; /* erase first */ while (!feof(fp)) { for(e=0; e<0x10; e++) M.dword[e] = 0x00000000UL; /* erase first */ if((i = fread(&M, 1, 0x40, fp)) == 0x40) { if (type == 160) { rmd160(M.dword, IH, c); c = 0; } else if (type == 128) { rmd128(M.dword, IH, c); c = 0; } else if (type == 256) { rmd256(M.dword, IH, c); c = 0; } byte.qword = byte.qword + 0x40; } else { if (i == 0) { byte.qword = (byte.qword + i) << 3; M.dword[0x0] = 0x00000080UL; M.dword[0xe] = (byte.dword[0x0]); M.dword[0xf] = (byte.dword[0x1]); if (type == 160) rmd160(M.dword, IH, c); else if (type == 128) rmd128(M.dword, IH, c); else if (type == 256) rmd256(M.dword, IH, c); } else { M.byte [ i ] = 0x80; if(i < 0x38) { byte.qword = (byte.qword + i) << 3; M.dword[0xe] = (byte.dword[0x0]); M.dword[0xf] = (byte.dword[0x1]); if (type == 160) rmd160(M.dword, IH, c); else if (type == 128) rmd128(M.dword, IH, c); else if (type == 256) rmd256(M.dword, IH, c); } else { M.dword[0xe] = (M.dword[0xe]); M.dword[0xf] = (M.dword[0xf]); if (type == 160) { rmd160(M.dword, IH, c); c = 0; } else if (type == 128) { rmd128(M.dword, IH, c); c = 0; } else if (type == 256) { rmd256(M.dword, IH, c); c = 0; } for(e=0; e<0x10; e++) M.dword[e] = 0x00000000UL; /* erase first */ byte.qword = (byte.qword + i) << 3; M.dword[0xe] = (byte.dword[0x0]); M.dword[0xf] = (byte.dword[0x1]); if (type == 160) rmd160(M.dword, IH, c); else if (type == 128) rmd128(M.dword, IH, c); else if (type == 256) rmd256(M.dword, IH, c); } } } } fclose(fp); } else { return 1; } for(e=0; e<0x08; e++) IH[e] = swapul(IH[e]); return 0; }