Advertisement
ossifrage

GMP hash function

Jun 3rd, 2015
607
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.90 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include "gmp.h"
  4.  
  5. void hashfunc(mpz_t hash, const char *input, const int len) {
  6.   int n,i;
  7.   static int first=1;
  8.   unsigned long c;
  9.   static mpz_t POW14, POW26_1, h14;
  10.   if (first) {
  11.     mpz_init_set_str(POW14, "11112006825558016", 10);
  12.     mpz_init_set_str(POW26_1, "6156119580207157310796674288400203777", 10);
  13.     mpz_init(h14);
  14.     first=0;
  15.   }
  16.   mpz_set_ui(hash, 1);
  17.   for (n=0; n<3; n++) {
  18.     for (i=0; i<len; i++) {
  19.       c = input[i];
  20.       mpz_add_ui(hash, hash, c);
  21.       mpz_mul(h14,hash,POW14);
  22.       mpz_xor(hash,hash,h14);
  23.       mpz_mod(hash,hash,POW26_1);
  24.     }
  25.   }
  26. }
  27.  
  28. int main() {
  29.   mpz_t hash;
  30.   char *tests[] = { "", "Test", "test" };
  31.   int i;
  32.   mpz_init(hash);
  33.   for (i=0; i<sizeof(tests)/sizeof(char*); i++) {
  34.     hashfunc(hash, tests[i], strlen(tests[i]));
  35.     gmp_printf("'%s' => %Zd\n", tests[i], hash);
  36.   }
  37.   return 0;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement