Skip to content

Instantly share code, notes, and snippets.

@liangfu
Created May 24, 2016 16:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save liangfu/5b7bc147c6acd22a5ce6fa9cc11006f1 to your computer and use it in GitHub Desktop.
Save liangfu/5b7bc147c6acd22a5ce6fa9cc11006f1 to your computer and use it in GitHub Desktop.
simple hash string code in c
char num2char(size_t num){
char res = 0; int val = num%62;
if (val<10){ res = val+48;
}else if (val>=10&&val<36){ res = val+65-10;
}else if (val>=36&&val<62){ res = val+97-36;
}return res;
}
void swap(char & a, char & b){char t=b;b=a;a=t;}
void hash(char * src, char * dst, int sz){
memcpy(dst,src,32);
static int swap_table[32]={/* shuffled 0 .. 31 */};
int i; const int maxiter=32;
for (int iter=0;iter<maxiter;iter++){
for (i=0;i<32;i++){swap(dst[31-swap_table[i]],dst[swap_table[i]]);}
for (i=0;i<32;i++){dst[i]+=swap_table[i]-16;}
for (i=0;i<32;i++){
dst[i]=num2char(int(dst[i])+
int(dst[swap_table[i]])+
237);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment