Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdint.h>
- #include <sys/types.h>
- /* antirez hash function one (AHF1)
- *
- * FIXME: make it byte ordering agnostic.
- *
- * TEST VECTOR:
- * "123456789" => 2934016909
- * "Message Digest" => 1454815842
- */
- uint32_t ahf1(unsigned char *s, size_t len) {
- uint32_t hash;
- unsigned char *h = (unsigned char*) &hash;
- int idx = 0;
- h[0] = 0x8D;
- h[1] = 0xCA;
- h[2] = 0x2E;
- h[3] = 0x35;
- while(len) {
- int target = ((*s) ^ h[idx]) & 3;
- int rot = *s & 0x1F;
- h[target] ^= *s; /* step 1: XOR with target byte. */
- hash = (hash << rot) | (hash >> (32-rot)); /* step 2: Circular shifting. */
- len--;
- s++;
- idx = (idx+1) & 3;
- }
- return hash;
- }
- #include <stdio.h>
- #include <string.h>
- int main(void) {
- printf("%lu\n", (unsigned long) ahf1((unsigned char*)"123456789",9));
- return 0;
- }
Add Comment
Please, Sign In to add comment