#SHA-1
c
#define FIO_SHA1
#include "fio-stl.h"SHA-1 hashing and HMAC. It is cryptographically broken, so do not use it for anything that needs security. Some protocols (hello, WebSockets) still ask for it, so it lives here for compatibility.
#Types
#fio_sha1_s
c
typedef union {
#ifdef __SIZEOF_INT128__
__uint128_t align__;
#else
uint64_t align__;
#endif
uint32_t v[5];
uint8_t digest[20];
} fio_sha1_s;Container for the 20-byte SHA-1 digest.
Members:
align__- alignment padding.v- the 5 × 32-bit state words.digest- the 20-byte digest as raw bytes.
#API Functions
#fio_sha1
c
fio_sha1_s fio_sha1(const void *data, uint64_t len);One-shot SHA-1 hash of len bytes at data.
Parameters:
data- pointer to the data to hash.len- length of the data in bytes.
Returns: a fio_sha1_s containing the 20-byte digest.
#fio_sha1_hmac
c
fio_sha1_s fio_sha1_hmac(const void *key,
uint64_t key_len,
const void *msg,
uint64_t msg_len);Computes HMAC-SHA1, producing a 20-byte authentication code.
Parameters:
key- pointer to the secret key.key_len- length of the key in bytes.msg- pointer to the message to authenticate.msg_len- length of the message in bytes.
Returns: a fio_sha1_s containing the 20-byte HMAC.
Note: keys longer than 64 bytes are first hashed with SHA-1.
#fio_sha1_len
c
size_t fio_sha1_len(void);Returns: the SHA-1 digest length in bytes (20).
#fio_sha1_digest
c
uint8_t *fio_sha1_digest(fio_sha1_s *s);Parameters:
s- pointer to afio_sha1_sresult.
Returns: a pointer to the 20-byte digest inside s.
#Examples
#One-shot hash
c
#define FIO_SHA1
#include "fio-stl.h"
#include <stdio.h>
#include <string.h>
int main(void) {
const char *msg = "hello";
fio_sha1_s r = fio_sha1(msg, strlen(msg));
for (size_t i = 0; i < fio_sha1_len(); ++i)
printf("%02x", fio_sha1_digest(&r)[i]);
printf("\n");
return 0;
}#HMAC
c
fio_sha1_s mac = fio_sha1_hmac("secret", 6, "hello", 5);Note: SHA-1 is broken. Prefer SHA-256, SHA-3, or another modern hash for security-sensitive work.