bool hash_store_verify_hash(struct hash_store* self, const uint8_t* payload, size_t payloadsz, const uint8_t* expect_hash) { uint8_t actual_hash[SEGMENT_HASHSZ]; struct ndn_digest* digest = ndn_digest_create(NDN_DIGEST_SHA256); ndn_digest_init(digest); ndn_digest_update(digest, payload, payloadsz); ndn_digest_final(digest, actual_hash, sizeof(actual_hash)); ndn_digest_destroy(&digest); return 0 == memcmp(expect_hash, actual_hash, sizeof(actual_hash)); }
bool hash_store_insert(struct hash_store* self, ndnr_accession accession, struct ndn_charbuf* co, struct ndn_parsed_ContentObject* pco) { int res; // retrieve payload struct ndn_parsed_ContentObject pco2 = {0}; if (pco == NULL) { res = ndn_parse_ContentObject(co->buf, co->length, pco = &pco2, NULL); if (res != 0) return false; } const uint8_t* payload; size_t payloadsz; ndn_content_get_value(co->buf, co->length, pco, &payload, &payloadsz); // calculate hash uint8_t hash[SEGMENT_HASHSZ]; struct ndn_digest* digest = ndn_digest_create(NDN_DIGEST_SHA256); ndn_digest_init(digest); ndn_digest_update(digest, payload, payloadsz); ndn_digest_final(digest, hash, sizeof(hash)); ndn_digest_destroy(&digest); LOG("hash_store_insert(%" PRIx64 ") ", (uint64_t)ndnr_accession_encode(self->h, accession)); LOG_hash(hash, SEGMENT_HASHSZ); // find where to insert struct ndn_btree_node* leaf = NULL; res = ndn_btree_lookup(self->btree, hash, sizeof(hash), &leaf); int i = NDN_BT_SRCH_INDEX(res); if (NDN_BT_SRCH_FOUND(res)) { LOG(" duplicate(%u,%d)\n", leaf->nodeid, i); return true;// already have it } LOG(" insert(%u,%d)\n", leaf->nodeid, i); // prepare payload uint64_t accession_encoded = ndnr_accession_encode(self->h, accession); // insert index entry res = ndn_btree_prepare_for_update(self->btree, leaf); if (res < 0) return false; res = ndn_btree_insert_entry(leaf, i, hash, sizeof(hash), &accession_encoded, sizeof(accession_encoded)); if (res < 0) return false; // btree maintenance if (ndn_btree_oversize(self->btree, leaf)) { res = ndn_btree_split(self->btree, leaf); for (int limit = 100; res >= 0 && self->btree->nextsplit != 0; --limit) { if (limit == 0) abort(); struct ndn_btree_node* node = ndn_btree_getnode(self->btree, self->btree->nextsplit, 0); if (node == NULL) break; res = ndn_btree_split(self->btree, node); } } hash_store_clean(self); return true; }
static int create_key_digest(const unsigned char *dkey, size_t dkey_size, unsigned char **o_key_digest, size_t *o_key_digest_size) { struct ndn_digest *digest; unsigned char *key_digest = NULL; size_t key_digest_size; int r; assert(o_key_digest); assert(o_key_digest_size); digest = ndn_digest_create(NDN_DIGEST_SHA256); JUMP_IF_NULL(digest, error); ndn_digest_init(digest); key_digest_size = ndn_digest_size(digest); key_digest = malloc(key_digest_size); JUMP_IF_NULL(key_digest, error); r = ndn_digest_update(digest, dkey, dkey_size); JUMP_IF_NEG(r, error); r = ndn_digest_final(digest, key_digest, key_digest_size); JUMP_IF_NEG(r, error); ndn_digest_destroy(&digest); *o_key_digest = key_digest; *o_key_digest_size = key_digest_size; return 0; error: PyErr_SetString(g_PyExc_NDNKeyError, "unable to generate key digest"); if (key_digest) free(key_digest); ndn_digest_destroy(&digest); return -1; }