SHA-1#

Warning

SHA-1 is insecure. Please avoid or ask your cryptographer of trust for permission.

API Reference#

One-Shot#

Example

// Note: HACL Packages will provide this (or a similar) define in a later
// version.
#define HACL_HASH_SHA1_DIGEST_LENGTH 20
const char* message = "Hello, World!";
uint32_t message_size = strlen(message);

uint8_t digest[HACL_HASH_SHA1_DIGEST_LENGTH];

Hacl_Hash_SHA1_hash(digest, (uint8_t*)message, message_size);
void Hacl_Hash_SHA1_hash(uint8_t *output, uint8_t *input, uint32_t input_len)#

Streaming#

Example

// Note: HACL Packages will provide this (or a similar) define in a later
// version.
#define HACL_HASH_SHA1_DIGEST_LENGTH 20
void
print_hex_ln(size_t bytes_len, uint8_t* bytes)
{
  for (int i = 0; i < bytes_len; ++i) {
    printf("%02x", bytes[i]);
  }

  printf("\n");
}
// This example shows how to hash the byte sequence "Hello, World!" in two
// chunks. As a bonus, it also shows how to obtain intermediate results by
// calling `digest` more than once.

const char* chunk_1 = "Hello, ";
const char* chunk_2 = "World!";
uint32_t chunk_1_size = strlen(chunk_1);
uint32_t chunk_2_size = strlen(chunk_2);

uint8_t digest_1[HACL_HASH_SHA1_DIGEST_LENGTH];
uint8_t digest_2[HACL_HASH_SHA1_DIGEST_LENGTH];

// Init
Hacl_Hash_SHA1_state_t* state = Hacl_Hash_SHA1_malloc();
Hacl_Hash_SHA1_reset(state);

// 1/2 Include `Hello, ` into the hash calculation and
// obtain the intermediate hash of "Hello, ".
Hacl_Hash_SHA1_update(state, (uint8_t*)chunk_1, chunk_1_size);
// This is optional when no intermediate results are required.
Hacl_Hash_SHA1_digest(state, digest_1);

// 2/2 Include `World!` into the hash calculation and
// obtain the final hash of "Hello, World!".
Hacl_Hash_SHA1_update(state, (uint8_t*)chunk_2, chunk_2_size);
Hacl_Hash_SHA1_digest(state, digest_2);

// Cleanup
Hacl_Hash_SHA1_free(state);

print_hex_ln(HACL_HASH_SHA1_DIGEST_LENGTH, digest_1);
print_hex_ln(HACL_HASH_SHA1_DIGEST_LENGTH, digest_2);
typedef Hacl_Streaming_MD_state_32 Hacl_Hash_SHA1_state_t#
Hacl_Streaming_MD_state_32 *Hacl_Hash_SHA1_malloc(void)#
Hacl_Streaming_Types_error_code Hacl_Hash_SHA1_update(Hacl_Streaming_MD_state_32 *state, uint8_t *chunk, uint32_t chunk_len)#

0 = success, 1 = max length exceeded

void Hacl_Hash_SHA1_digest(Hacl_Streaming_MD_state_32 *state, uint8_t *output)#
void Hacl_Hash_SHA1_reset(Hacl_Streaming_MD_state_32 *state)#
void Hacl_Hash_SHA1_free(Hacl_Streaming_MD_state_32 *state)#