sapling/cfastmanifest/checksum.c
Adam Simpkins 5977867b68 sha1: provide wrapper functions around the SHA-1 implementation
Summary:
Add a clib/sha1.h file with SHA-1 utility functions that hide the details of
the underlying SHA-1 implementation being used.  This will make it easier in
the future if we want to use the faster SHA-1 implementation from OpenSSL if it
is available, but fall back to the sha1collisiondetection library if it is not
available.

Test Plan: Confirmed the code builds and passes unit tests.

Reviewers: #fbhgext, ryanmce

Reviewed By: #fbhgext, ryanmce

Differential Revision: https://phab.mercurial-scm.org/D282
2017-08-22 19:09:07 -07:00

36 lines
966 B
C

// Copyright 2016-present Facebook. All Rights Reserved.
//
// checksum.c: implementation for recalculating the checksums for
// intermediate nodes in a tree.
//
// no-check-code
#include "clib/sha1.h"
#include "node.h"
#include "tree.h"
static void update_checksum(node_t *node) {
fbhg_sha1_ctx_t ctx;
fbhg_sha1_init(&ctx);
// find all the children and make sure their checksums are up-to-date.
for (child_num_t ix = 0; ix < node->num_children; ix++) {
node_t* child = get_child_by_index(node, ix);
if (child->checksum_valid == false) {
update_checksum(child);
}
fbhg_sha1_update(&ctx, child->name, child->name_sz);
fbhg_sha1_update(&ctx, child->checksum, child->checksum_sz);
fbhg_sha1_update(&ctx, &child->flags, 1);
}
fbhg_sha1_final(node->checksum, &ctx);
node->checksum_sz = SHA1_BYTES;
node->checksum_valid = true;
}
void update_checksums(tree_t *tree) {
update_checksum(tree->shadow_root);
}