Big H2(ZZn6 y) { // Hash and compress an Fp6 to a big number sha sh; ZZn u,v,w; ZZn2 x; Big a,h,p,xx[2]; char s[HASH_LEN]; int i,j,m; shs_init(&sh); y.get(x); x.get(u,v); xx[0]=u; xx[1]=v; for (i=0;i<2;i++) { a=xx[i]; while (a>0) { m=a%256; shs_process(&sh,m); a/=256; } } shs_hash(&sh,s); h=from_binary(HASH_LEN,s); return h; }
Big H1(char *string) { // Hash a zero-terminated string to a number < modulus Big h,p; char s[HASH_LEN]; int i,j; sha sh; shs_init(&sh); for (i=0;;i++) { if (string[i]==0) break; shs_process(&sh,string[i]); } shs_hash(&sh,s); p=get_modulus(); h=1; j=0; i=1; forever { h*=256; if (j==HASH_LEN) {h+=i++; j=0;} else h+=s[j++]; if (h>=p) break; } h%=p; return h; }
static void fill_pool(csprng *rng) { /* hash down output of RNG to re-fill the pool */ int i; sha sh; shs_init(&sh); for (i=0;i<128;i++) shs_process(&sh,sbrand(rng)); shs_hash(&sh,rng->pool); rng->pool_ptr=0; }
static void hashing(FILE *fp,big hash) { /* compute hash function */ char h[20]; sha sh; int i,ch; shs_init(&sh); while ((ch=fgetc(fp))!=EOF) shs_process(&sh,ch); shs_hash(&sh,h); bytes_to_big(20,h,hash); }
void PFC::add_to_hash(const Big& x) { int m; Big a=x; while (a>0) { m=a%256; shs_process(&SH,m); a/=256; } }
void PFC::add_to_hash(const G1& x) { Big a,X,Y; int i,m; x.g.get(X,Y); a=X; while (a>0) { m=a%256; shs_process(&SH,m); a/=256; } a=Y; while (a>0) { m=a%256; shs_process(&SH,m); a/=256; } }
static void hashing(miracl *mip,FILE *fp,big hash) { /* compute hash function */ char h[20]; int len,ch; sha sh; shs_init(&sh); while ((ch=fgetc(fp))!=EOF) shs_process(&sh,ch); shs_hash(&sh,h); len=(MIRACL*MR_STATIC)/8; if (len>20) len=20; bytes_to_big(mip,len,h,hash); }
void mcl_ecpbs_Hhash(big result, char *info, big m) { char hash[20]; int i; sha sh; shs_init(&sh); for (i = 0; info[i] != 0; i++) { shs_process(&sh, info[i]); } shs_hash(&sh, hash); bytes_to_big(20, hash, result); mcl_ecpbs_mod(result, m); }
static Big hash(ifstream &fp) { /* compute hash function */ char ch,s[20]; char uch; int i; Big h; sha_r sh; shs_init(&sh); forever { /* read in bytes from message file */ fp.get(ch); if (fp.eof()) break; shs_process(&sh,ch); } shs_hash(&sh,s); h=from_binary(20,s); return h; }
void PFC::add_to_hash(const G2& x) { ZZn3 X,Y; ECn3 v=x.g; Big a; ZZn xx[6]; int i,m; v.get(X,Y); X.get(xx[0],xx[1],xx[2]); Y.get(xx[3],xx[4],xx[5]); for (i=0;i<6;i++) { a=(Big)xx[i]; while (a>0) { m=a%256; shs_process(&SH,m); a/=256; } } }
void strong_init(csprng *rng,int rawlen,char *raw,mr_unsign32 tod) { /* initialise from at least 128 byte string of raw * * random (keyboard?) input, and 32-bit time-of-day */ int i; mr_unsign32 hash[MR_HASH_BYTES/4]; sha sh; rng->pool_ptr=0; for (i=0;i<NK;i++) rng->ira[i]=0; if (rawlen>0) { shs_init(&sh); for (i=0;i<rawlen;i++) shs_process(&sh,raw[i]); shs_hash(&sh,(char *)hash); /* initialise PRNG from distilled randomness */ for (i=0;i<MR_HASH_BYTES/4;i++) sirand(rng,hash[i]); } sirand(rng,tod); fill_pool(rng); }
// 计算 CDKEY 中 hash 散列的值 big key_hash(unsigned int Key, unsigned char Type, unsigned char Data, big rx, big ry) { unsigned char buf[128], m[132], sha_out[20]; unsigned long dw; int i, j, len, len2; sha sh; big hash; KEY tmp; tmp.a = Key; // 钥匙的值 m[0] = tmp.b[3]; // 取钥匙的第3个字节 m[1] = tmp.b[2]; // 取钥匙的第2个字节 m[2] = Type; // CDKEY 中类型的值 m[3] = Data; // CDKEY 中数据的值 // 循环将 RX 的值放入 m[] 数组中,作为用于生成散列的内容。 len = rx->len * 4; // 获得RX的长度 big_to_bytes(len, rx, (char*)buf, FALSE); // 将 rx 转成 数值 for (i = len - 1, j = 4; i >= 0; i--, j++) m[j] = buf[i]; // 循环将 rx 写入 m[] // 循环将 RY 的值放入 m[] 数组中,作为用于生成散列的内容。 len2 = ry->len * 4; // 获得RY的长度 big_to_bytes(len2, ry, (char*)buf, FALSE); // 将 ry 转成 数值 for (i = len2 - 1, j = len + 4; i >= 0; i--, j++) m[j] = buf[i]; // 循环将 ry 写入 m[] // 开始散列 shs_init(&sh); // 初始化 sha1 散列 for (i = 0; i < (4 + rx->len + ry->len); i++) shs_process(&sh, m[i]); // 将 m[] 写入 sha1 处理 shs_hash(&sh, (char*)sha_out); // 计算 sha1 散列值 memcpy(&dw, sha_out, 4); // 将前4字节写入 dw dw = dw & 0x7FFFFFFF; // 取后 31bit 数值 hash = mirvar(dw); return hash; // 返回31bit散列的值 }
void PFC::add_to_hash(const GT& x) { ZZn6 u=x.g; ZZn2 v; ZZn l,h; Big a,xx[2]; int i,j,m; u.get(v); v.get(l,h); xx[0]=l; xx[1]=h; for (i=0;i<2;i++) { a=xx[i]; while (a>0) { m=a%256; shs_process(&SH,m); a/=256; } } }