2019-09-25 13:37:26 +00:00
|
|
|
#include "crypto/hash.h"
|
2019-06-10 12:56:11 +00:00
|
|
|
#include "crypto/crc32c.h"
|
2019-09-25 13:37:26 +00:00
|
|
|
#include "crypto/xxhash.h"
|
2019-10-07 11:13:26 +00:00
|
|
|
#include "crypto/sha.h"
|
2019-10-07 16:19:51 +00:00
|
|
|
#include "crypto/blake2.h"
|
2019-09-25 13:37:26 +00:00
|
|
|
|
2019-06-10 12:56:11 +00:00
|
|
|
int hash_crc32c(const u8* buf, size_t length, u8 *out)
|
|
|
|
{
|
|
|
|
u32 crc = ~0;
|
|
|
|
|
|
|
|
crc = crc32c(~0, buf, length);
|
|
|
|
put_unaligned_le32(~crc, out);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-09-25 13:37:26 +00:00
|
|
|
int hash_xxhash(const u8 *buf, size_t length, u8 *out)
|
|
|
|
{
|
|
|
|
XXH64_hash_t hash;
|
|
|
|
|
|
|
|
hash = XXH64(buf, length, 0);
|
|
|
|
/*
|
|
|
|
* NOTE: we're not taking the canonical form here but the plain hash to
|
|
|
|
* be compatible with the kernel implementation!
|
|
|
|
*/
|
|
|
|
memcpy(out, &hash, 8);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2019-10-07 11:13:26 +00:00
|
|
|
|
|
|
|
int hash_sha256(const u8 *buf, size_t len, u8 *out)
|
|
|
|
{
|
|
|
|
SHA256Context context;
|
|
|
|
|
|
|
|
SHA256Reset(&context);
|
|
|
|
SHA256Input(&context, buf, len);
|
|
|
|
SHA256Result(&context, out);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2019-10-07 16:19:51 +00:00
|
|
|
|
|
|
|
int hash_blake2b(const u8 *buf, size_t len, u8 *out)
|
|
|
|
{
|
|
|
|
blake2b_state S;
|
|
|
|
|
|
|
|
blake2b_init(&S, CRYPTO_HASH_SIZE_MAX);
|
|
|
|
blake2b_update(&S, buf, len);
|
|
|
|
blake2b_final(&S, out, CRYPTO_HASH_SIZE_MAX);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|