Skip to content

Instantly share code, notes, and snippets.

@cupakromer
Created December 20, 2012 02:07
Show Gist options
  • Save cupakromer/4342420 to your computer and use it in GitHub Desktop.
Save cupakromer/4342420 to your computer and use it in GitHub Desktop.
A little SHA in the C
char *sha256_hexdigest(const void *buf_to_hash, size_t buf_len, char *hexdigest)
{
uint8_t binary_hash[SHA256_DIGEST_LENGTH];
uint8_t *digest;
SHA256_CTX context;
int i;
SHA256_Init(&context);
SHA256_Update(&context, buf_to_hash, buf_len);
SHA256_Final(binary_hash, &context);
for (digest = hexdigest, i = 0; i < SHA256_DIGEST_LENGTH; i++)
{
digest += sprintf(digest, "%02x", binary_hash[i]);
}
return hexdigest;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment