int pass2hash160(unsigned char *pass, size_t pass_sz) { /* only initialize stuff once */ static int bwc_is_init = 0; if (!bwc_is_init) { /* initialize buffers */ mem = malloc(4096); /* initialize hashs */ sha256_ctx = malloc(sizeof(*sha256_ctx)); ripemd160_ctx = malloc(sizeof(*ripemd160_ctx)); /* set the flag */ bwc_is_init = 1; } unsigned char *pub_chr = mem; int pub_chr_sz; SHA256_Init(sha256_ctx); SHA256_Update(sha256_ctx, pass, pass_sz); SHA256_Final(hash256, sha256_ctx); secp256k1_ecdsa_pubkey_create(pub_chr, &pub_chr_sz, hash256, 0); #if 0 i = 0; for (i = 0; i < pub_chr_sz; i++) { printf("%02x", pub_chr[i]); } printf("\n"); #endif /* yo dawg, i heard you like hashes... */ SHA256_Init(sha256_ctx); SHA256_Update(sha256_ctx, pub_chr, pub_chr_sz); SHA256_Final(hash256, sha256_ctx); /* ...so i put a hash in your hash */ RIPEMD160_Init(ripemd160_ctx); RIPEMD160_Update(ripemd160_ctx, hash256, SHA256_DIGEST_LENGTH); RIPEMD160_Final(hash160_tmp.uc, ripemd160_ctx); memcpy(hash160_uncmp.uc, hash160_tmp.uc, 20); /* ugly key compression hack */ pub_chr[0] = 0x02 | (pub_chr[64] & 0x01); /* yo dawg, i heard you like hashes... */ SHA256_Init(sha256_ctx); SHA256_Update(sha256_ctx, pub_chr, 33); SHA256_Final(hash256, sha256_ctx); /* ...so i put a hash in your hash */ RIPEMD160_Init(ripemd160_ctx); RIPEMD160_Update(ripemd160_ctx, hash256, SHA256_DIGEST_LENGTH); RIPEMD160_Final(hash160_tmp.uc, ripemd160_ctx); memcpy(hash160_compr.uc, hash160_tmp.uc, 20); return 0; }
inline static int priv2hash160(unsigned char *priv) { /* only initialize stuff once */ if (!brainflayer_is_init) { /* initialize buffers */ mem = malloc(4096); /* initialize hashs */ sha256_ctx = malloc(sizeof(*sha256_ctx)); ripemd160_ctx = malloc(sizeof(*ripemd160_ctx)); /* set the flag */ brainflayer_is_init = 1; } unsigned char *pub_chr = mem; int pub_chr_sz; secp256k1_ecdsa_pubkey_create(pub_chr, &pub_chr_sz, priv, 0); #if 0 i = 0; for (i = 0; i < pub_chr_sz; i++) { printf("%02x", pub_chr[i]); } printf("\n"); #endif /* compute hash160 for uncompressed public key */ /* sha256(pub) */ SHA256_Init(sha256_ctx); SHA256_Update(sha256_ctx, pub_chr, pub_chr_sz); SHA256_Final(hash256, sha256_ctx); /* ripemd160(sha256(pub)) */ RIPEMD160_Init(ripemd160_ctx); RIPEMD160_Update(ripemd160_ctx, hash256, SHA256_DIGEST_LENGTH); RIPEMD160_Final(hash160_tmp.uc, ripemd160_ctx); /* save result to global struct */ memcpy(hash160_uncmp.uc, hash160_tmp.uc, 20); /* quick and dirty public key compression */ pub_chr[0] = 0x02 | (pub_chr[64] & 0x01); /* compute hash160 for compressed public key */ /* sha256(pub) */ SHA256_Init(sha256_ctx); SHA256_Update(sha256_ctx, pub_chr, 33); SHA256_Final(hash256, sha256_ctx); /* ripemd160(sha256(pub)) */ RIPEMD160_Init(ripemd160_ctx); RIPEMD160_Update(ripemd160_ctx, hash256, SHA256_DIGEST_LENGTH); RIPEMD160_Final(hash160_tmp.uc, ripemd160_ctx); /* save result to global struct */ memcpy(hash160_compr.uc, hash160_tmp.uc, 20); return 0; }
static int authcheck_ripemd160(aClient *cptr, anAuthStruct *as, char *para) { char buf[512]; int i, r; char *saltstr, *hashstr; if (!para) return -1; r = parsepass(as->data, &saltstr, &hashstr); if (r) { /* New method with salt: b64(RIPEMD160(RIPEMD160(<pass>)+salt)) */ char result1[MAXSALTLEN+20+1]; char result2[20]; char rsalt[MAXSALTLEN+1]; int rsaltlen; RIPEMD160_CTX hash; /* First, decode the salt to something real... */ rsaltlen = b64_decode(saltstr, rsalt, sizeof(rsalt)); if (rsaltlen <= 0) return -1; /* Then hash the password (1st round)... */ RIPEMD160_Init(&hash); RIPEMD160_Update(&hash, para, strlen(para)); RIPEMD160_Final(result1, &hash); /* Add salt to result */ memcpy(result1+20, rsalt, rsaltlen); /* b64_decode already made sure bounds are ok */ /* Then hash it all together again (2nd round)... */ RIPEMD160_Init(&hash); RIPEMD160_Update(&hash, result1, rsaltlen+20); RIPEMD160_Final(result2, &hash); /* Then base64 encode it all and we are done... */ if ((i = b64_encode(result2, sizeof(result2), buf, sizeof(buf)))) { if (!strcmp(buf, hashstr)) return 2; else return -1; } else return -1; } else { /* OLD auth */ if ((i = b64_encode(RIPEMD160(para, strlen(para), NULL), 20, buf, sizeof(buf)))) { if (!strcmp(buf, as->data)) return 2; else return -1; } else return -1; } }
static char *mkpass_ripemd160(char *para) { static char buf[128]; char result1[20+REALSALTLEN]; char result2[20]; char saltstr[REALSALTLEN]; /* b64 encoded printable string*/ char saltraw[RAWSALTLEN]; /* raw binary */ char xresult[64]; RIPEMD160_CTX hash; int i; if (!para) return NULL; /* generate a random salt... */ for (i=0; i < RAWSALTLEN; i++) saltraw[i] = getrandom8(); i = b64_encode(saltraw, RAWSALTLEN, saltstr, REALSALTLEN); if (!i) return NULL; /* b64(RIPEMD160(RIPEMD160(<pass>)+salt)) * ^^^^^^^^^^^ * step 1 * ^^^^^^^^^^^^^^^^^^^^^ * step 2 * ^^^^^^^^^^^^^^^^^^^^^^^^^^ * step 3 */ /* STEP 1 */ RIPEMD160_Init(&hash); RIPEMD160_Update(&hash, para, strlen(para)); RIPEMD160_Final(result1, &hash); /* STEP 2 */ /* add salt to result */ memcpy(result1+20, saltraw, RAWSALTLEN); /* Then hash it all together */ RIPEMD160_Init(&hash); RIPEMD160_Update(&hash, result1, RAWSALTLEN+20); RIPEMD160_Final(result2, &hash); /* STEP 3 */ /* Then base64 encode it all together.. */ i = b64_encode(result2, sizeof(result2), xresult, sizeof(xresult)); if (!i) return NULL; /* Good.. now create the whole string: * $<saltb64d>$<totalhashb64d> */ ircsprintf(buf, "$%s$%s", saltstr, xresult); return buf; }
ERL_NIF_TERM ucrypto_ripemd160_init_nif(ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) { ERL_NIF_TERM result; RIPEMD160_Init((RIPEMD160_CTX *)enif_make_new_binary(env, sizeof(RIPEMD160_CTX), &result)); return result; }
openssl_ripemd160_hasher::openssl_ripemd160_hasher() { static_assert(sizeof(decltype( openssl_ripemd160_hasher::ctx_)) == sizeof(RIPEMD160_CTX), ""); auto const ctx = reinterpret_cast< RIPEMD160_CTX*>(ctx_); RIPEMD160_Init(ctx); }
void rmd160( uint8_t *result, const uint8_t *data, size_t len ) { RIPEMD160_CTX ctx; RIPEMD160_Init(&ctx); RIPEMD160_Update(&ctx, data, len); RIPEMD160_Final(result, &ctx); }
unsigned char *RIPEMD160(const unsigned char *d, unsigned long n, unsigned char *md) { RIPEMD160_CTX c; static unsigned char m[RIPEMD160_DIGEST_LENGTH]; if (md == NULL) md=m; RIPEMD160_Init(&c); RIPEMD160_Update(&c,d,n); RIPEMD160_Final(md,&c); memset(&c,0,sizeof(c)); /* security consideration */ return(md); }
unsigned char *RIPEMD160(const unsigned char *d, size_t n, unsigned char *md) { RIPEMD160_CTX c; static unsigned char m[RIPEMD160_DIGEST_LENGTH]; if (md == NULL) md = m; if (!RIPEMD160_Init(&c)) return NULL; RIPEMD160_Update(&c, d, n); RIPEMD160_Final(md, &c); OPENSSL_cleanse(&c, sizeof(c)); /* security consideration */ return (md); }
short_hash generate_ripemd_hash(const data_chunk& chunk) { hash_digest sha_hash; SHA256_CTX sha_ctx; SHA256_Init(&sha_ctx); SHA256_Update(&sha_ctx, &chunk[0], chunk.size()); SHA256_Final(sha_hash.data(), &sha_ctx); short_hash ripemd_hash; RIPEMD160_CTX ripemd_ctx; RIPEMD160_Init(&ripemd_ctx); RIPEMD160_Update(&ripemd_ctx, sha_hash.data(), SHA256_DIGEST_LENGTH); RIPEMD160_Final(ripemd_hash.data(), &ripemd_ctx); return ripemd_hash; }
static void RIPEMD160_File(FILE *file, unsigned char **output, int *outlength) { *output = new unsigned char[RIPEMD160_DIGEST_LENGTH]; *outlength = RIPEMD160_DIGEST_LENGTH; RIPEMD160_CTX c; int i; unsigned char buf[RIPEMD160_FILE_BUFFER_SIZE]; RIPEMD160_Init(&c); for (;;) { i = fread(buf,1,RIPEMD160_FILE_BUFFER_SIZE,file); if(i <= 0) break; RIPEMD160_Update(&c,buf,(unsigned long)i); } RIPEMD160_Final(*output, &c); }
void do_fp(FILE *f) { RIPEMD160_CTX c; unsigned char md[RIPEMD160_DIGEST_LENGTH]; int fd; int i; static unsigned char buf[BUFSIZE]; fd=fileno(f); RIPEMD160_Init(&c); for (;;) { i=read(fd,buf,BUFSIZE); if (i <= 0) break; RIPEMD160_Update(&c,buf,(unsigned long)i); } RIPEMD160_Final(&(md[0]),&c); pt(md); }
ikptr ikrt_openssl_ripemd160_init (ikpcb * pcb) { #ifdef HAVE_RIPEMD160_INIT RIPEMD160_CTX * ctx; int rv; ctx = malloc(sizeof(RIPEMD160_CTX)); if (ctx) { rv = RIPEMD160_Init(ctx); if (rv) return ika_pointer_alloc(pcb, (long)ctx); else free(ctx); } return IK_FALSE; #else feature_failure(__func__); #endif }
static void S2KItSaltedRIPEMD160Generator(char *password, unsigned char *key, int length) { unsigned char keybuf[KEYBUFFER_LENGTH]; RIPEMD160_CTX ctx; int i, j; int32_t tl; int32_t mul; int32_t bs; uint8_t *bptr; int32_t n; uint32_t numHashes = (length + RIPEMD160_DIGEST_LENGTH - 1) / RIPEMD160_DIGEST_LENGTH; memcpy(keybuf, cur_salt->salt, SALT_LENGTH); // TODO: This is not very efficient with multiple hashes for (i = 0; i < numHashes; i++) { RIPEMD160_Init(&ctx); for (j = 0; j < i; j++) { RIPEMD160_Update(&ctx, "\0", 1); } // Find multiplicator tl = strlen(password) + SALT_LENGTH; mul = 1; while (mul < tl && ((64 * mul) % tl)) { ++mul; } // Try to feed the hash function with 64-byte blocks bs = mul * 64; bptr = keybuf + tl; n = bs / tl; memcpy(keybuf + SALT_LENGTH, password, strlen(password)); while (n-- > 1) { memcpy(bptr, keybuf, tl); bptr += tl; } n = cur_salt->count / bs; while (n-- > 0) { RIPEMD160_Update(&ctx, keybuf, bs); } RIPEMD160_Update(&ctx, keybuf, cur_salt->count % bs); RIPEMD160_Final(key + (i * RIPEMD160_DIGEST_LENGTH), &ctx); } }
int main(void) { RIPEMD160_CTX context; unsigned char hash[RIPEMD160_DIGEST_LENGTH]; BIO* bio_out; char* data_to_hash = "The worthwhile problems are the ones you can" "really solve or help solve, the ones you can" "really contribute something to. ... No " "problem is too small or too trivial if we " "can really do something about it." "- Richard Feynman"; int length = (int)strlen(data_to_hash); int i = 0; RIPEMD160_Init(&context); while (i < length) { if ((length - i) < DATA_LENGTH) RIPEMD160_Update(&context, (void*)(data_to_hash + i), length - i); else RIPEMD160_Update(&context, (void*)(data_to_hash + i), DATA_LENGTH); i += DATA_LENGTH; } RIPEMD160_Final(hash, &context); bio_out = BIO_new_fp(stdout, BIO_NOCLOSE); for (i = 0; i < RIPEMD160_DIGEST_LENGTH; i++) BIO_printf(bio_out, "%02x", (unsigned char*)hash[i]); return 0; }
void genkey(const Memblock &string, uint8_t *key, uint32_t length) const { RIPEMD160_CTX ctx; uint32_t numHashes = (length + RIPEMD160_DIGEST_LENGTH - 1) / RIPEMD160_DIGEST_LENGTH; // TODO: This is not very efficient with multiple hashes for (uint32_t i = 0; i < numHashes; i++) { RIPEMD160_Init(&ctx); for (uint32_t j = 0; j < i; j++) { RIPEMD160_Update(&ctx, "\0", 1); } // Find multiplicator int32_t tl = string.length + 8; int32_t mul = 1; while (mul < tl && ((64 * mul) % tl)) { ++mul; } // Try to feed the hash function with 64-byte blocks const int32_t bs = mul * 64; assert(bs <= KEYBUFFER_LENGTH); uint8_t *bptr = m_keybuf + tl; int32_t n = bs / tl; memcpy(m_keybuf + 8, string.data, string.length); while (n-- > 1) { memcpy(bptr, m_keybuf, tl); bptr += tl; } n = m_count / bs; while (n-- > 0) { RIPEMD160_Update(&ctx, m_keybuf, bs); } RIPEMD160_Update(&ctx, m_keybuf, m_count % bs); RIPEMD160_Final(key + (i * RIPEMD160_DIGEST_LENGTH), &ctx); } }
static void digest_init(DIGEST_CTX *c) { switch (digesttype) { case DIGEST_NONE: break; case DIGEST_MD5: MD5Init(&(c->MD5)); break; case DIGEST_RIPEMD160: RIPEMD160_Init(&(c->RIPEMD160)); break; case DIGEST_SHA1: SHA1_Init(&(c->SHA1)); break; case DIGEST_SHA256: SHA256_Init(&(c->SHA256)); break; case DIGEST_SHA512: SHA512_Init(&(c->SHA512)); break; } }
static int init(EVP_MD_CTX *ctx) { return RIPEMD160_Init(ctx->md_data); }
CGUL::RIPEMD::~RIPEMD() { RIPEMD160_Init(ctx); }
static int __archive_libmd_ripemd160init(archive_rmd160_ctx *ctx) { RIPEMD160_Init(ctx); return (ARCHIVE_OK); }
void CGUL::RIPEMD::Initialize() { RIPEMD160_Init(ctx); }
CGUL::RIPEMD::RIPEMD() { digest = new Byte[20]; RIPEMD160_Init(ctx); }