Home Rumble Youtube Twitter/X Kofi Contact / Crypto

Public Domain Hash Functions

These are a couple of Public Domain implementations of common hash functions that are included in sti for internal use and general convenience. I did not write any of them, but I did make minor superficial modifications to make the code conform to the desired compiler settings.

MurmurHash 3

Thanks to Austin Appleby

From the author: // Note - The x86 and x64 versions do _not_ produce the same results, as the // algorithms are optimized for their respective platforms. You can still // compile and run any of them on any platform, but your performance with the // non-native version will be less than optimal.

void MurmurHash3_x86_32(const void* key, int len, uint32_t seed, void* out )
Calculates a 32-bit Murmur3 hash of the input and copies it to out. Optimized for ancient machines.
void MurmurHash3_x86_128(const void* key, int len, uint32_t seed, void* out )
Calculates a 128-bit Murmur3 hash of the input and copies it to out. Optimized for ancient machines.
void MurmurHash3_x86_128(const void* key, int len, uint32_t seed, void* out )
Calculates a 128-bit Murmur3 hash of the input and copies it to out. Optimized for modern machines.

SHA2-256 and SHA2-512

Thanks to https://github.com/kalven/sha-2 and LibTomCrypt, converted to C by me.

typedef struct sha256_state { uint64_t length; uint32_t state[8]; uint32_t curlen; unsigned char buf[64]; } sha256_state; typedef struct sha512_state { uint64_t length; uint64_t state[8]; uint32_t curlen; unsigned char buf[128]; } sha512_state; // sha224 is a truncated sha256 // sha384 is a truncated sha512

All in one go

void sha256_sum(const void* in, uint32_t inlen, uint8_t* out32)
Calculates the SHA2-256 hash for the input. out32 must point to a 32-byte buffer.
void sha512_sum(const void* in, uint32_t inlen, uint8_t* out64)
Calculates the SHA2-512 hash for the input. out64 must point to a 64-byte buffer.

Block-by-block incremental processing

void sha256_init(sha256_state* md)
Initializes the state structure. Must be called before calling sha256_process().
void sha256_process(sha256_state* md, const void* in, uint32_t inlen)
Feeds the next block of data into the hash state. Must call sha256_init() before this function, and sha256_done() when finished.
void sha256_done(sha256_state* md, uint8_t* out32)
Finishes the hash and copies the result into out32, which must be a 32-byte buffer.

void sha512_init(sha512_state* md)
Initializes the state structure. Must be called before calling sha512_process().
void sha512_process(sha512_state* md, const void* in, uint32_t inlen)
Feeds the next block of data into the hash state. Must call sha512_init() before this function, and sha512_done() when finished.
void sha512_done(sha512_state* md, uint8_t* out64)
Finishes the hash and copies the result into out64, which must be a 64-byte buffer.