R_API char *r_hash_to_string(RHash *ctx, const char *name, const ut8 *data, int len) { ut64 algo = r_hash_name_to_bits (name); char *digest_hex = NULL; RHash *myctx = NULL; int i, digest_size; if (!algo || !data) { return NULL; } if (!ctx) { myctx = ctx = r_hash_new (true, algo); } r_hash_do_begin (ctx, algo); digest_size = r_hash_calculate (ctx, algo, data, len); r_hash_do_end (ctx, algo); if (digest_size > 0) { if (digest_size * 2 < digest_size) { digest_hex = NULL; } else { digest_hex = malloc ((digest_size * 2) + 1); for (i = 0; i < digest_size; i++) { sprintf (digest_hex + (i * 2), "%02x", ctx->digest[i]); } digest_hex[digest_size * 2] = 0; } } if (myctx) { r_hash_free (myctx); } return digest_hex; }
R_API char *r_hash_to_string(RHash *ctx, const char *name, const ut8 *data, int len) { char *digest_hex = NULL; int i, digest_size; ut64 algo = r_hash_name_to_bits (name); if (!ctx) ctx = r_hash_new (R_TRUE, algo); r_hash_do_begin (ctx, algo); r_hash_calculate (ctx, algo, data, len); r_hash_do_end (ctx, algo); digest_size = r_hash_size (algo); digest_hex = malloc ((digest_size *2)+1); for (i = 0; i< digest_size; i++) sprintf (digest_hex+(i*2), "%02x", ctx->digest[i]); digest_hex[digest_size*2] = 0; r_hash_free (ctx); return digest_hex; }
main () { int HASH = R_HASH_MD5; RHash *h = r_hash_new (1, HASH); r_hash_do_begin (h, HASH); r_hash_do_md5 (h, "hello", 5); printmd5("hello", h); r_hash_do_md5 (h, "world", 5); printmd5("world", h); r_hash_do_end (h, HASH); printmd5("FINISH", h); r_hash_do_md5 (h, "helloworld", 10); printmd5("helloworld", h); }
static int do_hash(const char *file, const char *algo, RIO *io, int bsize, int rad, int ule) { ut64 j, fsize, algobit = r_hash_name_to_bits (algo); RHash *ctx; ut8 *buf; int i, first = 1; if (algobit == R_HASH_NONE) { eprintf ("rahash2: Invalid hashing algorithm specified\n"); return 1; } fsize = r_io_size (io); if (fsize <1) { eprintf ("rahash2: Invalid file size\n"); return 1; } if (bsize<0) bsize = fsize / -bsize; if (bsize == 0 || bsize > fsize) bsize = fsize; if (to == 0LL) to = fsize; if (from>to) { eprintf ("rahash2: Invalid -f -t range\n"); return 1; } if (fsize == -1LL) { eprintf ("rahash2: Unknown file size\n"); return 1; } buf = malloc (bsize+1); if (!buf) return 1; ctx = r_hash_new (R_TRUE, algobit); if (rad == 'j') printf ("["); if (incremental) { for (i=1; i<0x800000; i<<=1) { if (algobit & i) { int hashbit = i & algobit; int dlen = r_hash_size (hashbit); r_hash_do_begin (ctx, i); if (rad == 'j') { if (first) { first = 0; } else { printf (","); } } if (s.buf && s.prefix) { do_hash_internal (ctx, hashbit, s.buf, s.len, rad, 0, ule); } for (j=from; j<to; j+=bsize) { int len = ((j+bsize)>to)? (to-j): bsize; r_io_pread (io, j, buf, len); do_hash_internal (ctx, hashbit, buf, len, rad, 0, ule); } if (s.buf && !s.prefix) { do_hash_internal (ctx, hashbit, s.buf, s.len, rad, 0, ule); } r_hash_do_end (ctx, i); if (iterations>0) r_hash_do_spice (ctx, i, iterations, _s); if (!*r_hash_name (i)) continue; if (!quiet && rad != 'j') printf ("%s: ", file); do_hash_print (ctx, i, dlen, rad, ule); } } if (_s) free (_s->buf); } else { /* iterate over all algorithm bits */ if (s.buf) eprintf ("Warning: Seed ignored on per-block hashing.\n"); for (i=1; i<0x800000; i<<=1) { ut64 f, t, ofrom, oto; if (algobit & i) { int hashbit = i & algobit; ofrom = from; oto = to; f = from; t = to; for (j=f; j<t; j+=bsize) { int nsize = (j+bsize<fsize)? bsize: (fsize-j); r_io_pread (io, j, buf, bsize); from = j; to = j+bsize; if (to>fsize) to = fsize; do_hash_internal (ctx, hashbit, buf, nsize, rad, 1, ule); } from = ofrom; to = oto; } } } if (rad == 'j') printf ("]\n"); r_hash_free (ctx); free (buf); return 0; }
static int do_hash(const char *algo, RIO *io, int bsize, int rad) { ut8 *buf; RHash *ctx; ut64 j, fsize; int i; ut64 algobit = r_hash_name_to_bits (algo); if (algobit == R_HASH_NONE) { eprintf ("Invalid hashing algorithm specified\n"); return 1; } fsize = r_io_size (io); if (bsize == 0 || bsize > fsize) bsize = fsize; if (to == 0LL) to = fsize; if (from>to) { eprintf ("Invalid -f -t range\n"); return 1; } if (fsize == -1LL) { eprintf ("Unknown file size\n"); return 1; } buf = malloc (bsize+1); ctx = r_hash_new (R_TRUE, algobit); if (incremental) { for (i=1; i<0x800000; i<<=1) { if (algobit & i) { int hashbit = i & algobit; int dlen = r_hash_size (hashbit); r_hash_do_begin (ctx, i); for (j=from; j<to; j+=bsize) { r_io_read_at (io, j, buf, bsize); do_hash_internal (ctx, hashbit, buf, ((j+bsize)<fsize)? bsize: (fsize-j), rad, 0); } r_hash_do_end (ctx, i); do_hash_print (ctx, i, dlen, rad); } } } else { /* iterate over all algorithm bits */ for (i=1; i<0x800000; i<<=1) { ut64 f, t, ofrom, oto; if (algobit & i) { int hashbit = i & algobit; ofrom = from; oto = to; f = from; t = to; for (j=f; j<t; j+=bsize) { int nsize = (j+bsize<fsize)? bsize: (fsize-j); r_io_read_at (io, j, buf, bsize); from = j; to = j+bsize; do_hash_internal (ctx, hashbit, buf, nsize, rad, 1); } from = ofrom; to = oto; } } } r_hash_free (ctx); free (buf); return 0; }