static char *i_cid_pid() { if(db_vars_get(0, "cid") && db_vars_get(0, "pid")) return NULL; guint64 r = rand_64(); struct tiger_ctx t; char pid[24]; tiger_init(&t); tiger_update(&t, (char *)&r, 8); tiger_final(&t, pid); // now hash the PID so we have our CID char cid[24]; tiger_init(&t); tiger_update(&t, pid, 24); tiger_final(&t, cid); // encode and save char enc[40] = {}; base32_encode(pid, enc); db_vars_set(0, "pid", enc); base32_encode(cid, enc); db_vars_set(0, "cid", enc); return NULL; }
/* get tth root hash */ void tth_final(tth_ctx *ctx, unsigned char result[24]) { uint64_t it = 1; unsigned pos = 0; unsigned char msg[24]; unsigned char* last_message; /* process bytes left in the context buffer */ if(ctx->tiger.length>1 || ctx->block_count==0) { tth_process_block_hash(ctx); } for(; it < ctx->block_count && (it&ctx->block_count)==0; it <<= 1) pos += 3; last_message = (unsigned char*)(ctx->stack + pos); for(it <<= 1; it <= ctx->block_count; it <<= 1) { /* merge tth sums in the tree */ pos += 3; if(it&ctx->block_count) { tiger_init(&ctx->tiger); ctx->tiger.message[ ctx->tiger.length++ ] = 1; tiger_update(&ctx->tiger, (unsigned char*)(ctx->stack + pos), 24); tiger_update(&ctx->tiger, last_message, 24); tiger_final(&ctx->tiger, msg); bswap_3x64(msg); last_message = msg; } } memcpy(result, last_message, 24); return; }
void TigerHash::Finalize() { CoreAssert(this != NULL); if (m_hash) delete [] m_hash; m_hash = new unsigned char[TIGER_DIGEST_SIZE]; tiger_final(m_hash, &m_state); }
static void tth_process_block_hash(tth_ctx *ctx) { uint64_t it; unsigned pos = 0; unsigned char msg[24]; for(it=1; it & ctx->block_count; it <<= 1) { tiger_final(&ctx->tiger, msg); bswap_3x64(msg); tiger_init(&ctx->tiger); ctx->tiger.message[ ctx->tiger.length++ ] = 1; tiger_update(&ctx->tiger, (unsigned char*)(ctx->stack + pos), 24); /* note: we can cut this step, if the previous tiger_final saves directly to ctx->tiger.message+25; */ tiger_update(&ctx->tiger, msg, 24); pos += 3; } tiger_final(&ctx->tiger, (unsigned char*)(ctx->stack + pos)); bswap_3x64( ctx->stack + pos ); ctx->block_count++; }
void tth_final(tth_ctx_t *ctx, char *result) { // finish up last leaf if(!ctx->gotfirst || ctx->tiger.length > 1) { tiger_final(&ctx->tiger, result); tth_update_leaf(ctx, result); } // calculate final hash tth_stack_final(ctx, result); }
int TigerHash::Process(const void * _data, size_t _length) { CoreAssert(this != NULL); Reset(); if (!_data) return -1; tiger_update(&m_state, (unsigned char *)_data, _length); m_hash = new unsigned char[TIGER_DIGEST_SIZE]; tiger_final(m_hash, &m_state); return 0; }
void tth_update(tth_ctx_t *ctx, const char *msg, size_t len) { char leaf[24]; int left; if(len > 0) ctx->gotfirst = 1; while(len > 0) { left = MIN(tth_base_block - (ctx->tiger.length-1), len); tiger_update(&ctx->tiger, msg, left); len -= left; msg += left; g_assert(ctx->tiger.length-1 <= tth_base_block); // we've got a new base leaf if(ctx->tiger.length-1 == tth_base_block) { tiger_final(&ctx->tiger, leaf); tth_update_leaf(ctx, leaf); tth_new_leaf(ctx); } } g_assert(len == 0); }