📄 sha1.java
字号:
package rsa;public class sha1 { private int[]sha=new int[5]; public static final int[] K = { 0x5a827999, 0x5a827999, 0x5a827999, 0x5a827999, 0x5a827999, 0x5a827999, 0x5a827999, 0x5a827999, 0x5a827999, 0x5a827999, 0x5a827999, 0x5a827999, 0x5a827999, 0x5a827999, 0x5a827999, 0x5a827999, 0x5a827999, 0x5a827999, 0x5a827999, 0x5a827999, 0x6ed9eba1, 0x6ed9eba1, 0x6ed9eba1, 0x6ed9eba1, 0x6ed9eba1, 0x6ed9eba1, 0x6ed9eba1, 0x6ed9eba1, 0x6ed9eba1, 0x6ed9eba1, 0x6ed9eba1, 0x6ed9eba1, 0x6ed9eba1, 0x6ed9eba1, 0x6ed9eba1, 0x6ed9eba1, 0x6ed9eba1, 0x6ed9eba1, 0x6ed9eba1, 0x6ed9eba1, 0x8f1bbcdc, 0x8f1bbcdc, 0x8f1bbcdc, 0x8f1bbcdc, 0x8f1bbcdc, 0x8f1bbcdc, 0x8f1bbcdc, 0x8f1bbcdc, 0x8f1bbcdc, 0x8f1bbcdc, 0x8f1bbcdc, 0x8f1bbcdc, 0x8f1bbcdc, 0x8f1bbcdc, 0x8f1bbcdc, 0x8f1bbcdc, 0x8f1bbcdc, 0x8f1bbcdc, 0x8f1bbcdc, 0x8f1bbcdc, 0xca62c1d6, 0xca62c1d6, 0xca62c1d6, 0xca62c1d6, 0xca62c1d6, 0xca62c1d6, 0xca62c1d6, 0xca62c1d6, 0xca62c1d6, 0xca62c1d6, 0xca62c1d6, 0xca62c1d6, 0xca62c1d6, 0xca62c1d6, 0xca62c1d6, 0xca62c1d6, 0xca62c1d6, 0xca62c1d6, 0xca62c1d6, 0xca62c1d6 }; public sha1(byte[]test_byte){ int test_length=test_byte.length; int l=8*test_length; int k=(448-l-1)%512; if(k<0)k+=512; int k8=(k-7)>>>3; int tot4=(test_length+1+k8+8)>>>2; int[]in=new int[16]; sha[0] = 0x67452301; sha[1] = 0xefcdab89; sha[2] = 0x98badcfe; sha[3] = 0x10325476; sha[4] = 0xc3d2e1f0; int test_reste=test_length; int offset=0; int un_ok=0; for(int i=0;i<tot4;i+=16){ int in_i=0; if(i+16<tot4){ for(int j=0;j<16;j++){ switch (test_reste) { case 0 : if(un_ok==1)in[in_i++]=0; else in[in_i++]=0x80 << 24; un_ok=1; break; case 1 : in[in_i++]=(test_byte[offset++] << 24) | (0x80 << 16); test_reste=0; un_ok=1; break; case 2 : in[in_i++]=(test_byte[offset++] << 24) | ((test_byte[offset++] & 0xFF) << 16) | (0x80 << 8) ; test_reste=0; un_ok=1; break; case 3 : in[in_i++]=(test_byte[offset++] << 24) | ((test_byte[offset++] & 0xFF) << 16) | ((test_byte[offset++] & 0xFF) << 8) | 0x80 ; test_reste=0; un_ok=1; break; default : in[in_i++]=(test_byte[offset++] << 24) | ((test_byte[offset++] & 0xFF) << 16) | ((test_byte[offset++] & 0xFF) << 8) | (test_byte[offset++] & 0xFF) ; test_reste-=4; un_ok=0; break; } } }else{ for(int j=0;j<14;j++){ switch (test_reste) { case 0 : if(un_ok==1)in[in_i++]=0; else in[in_i++]=0x80<<24; un_ok=1; break; case 1 : in[in_i++]=(test_byte[offset++] << 24) | (0x80 << 16); test_reste=0; un_ok=1; break; case 2 : in[in_i++]=(test_byte[offset++] << 24) | ((test_byte[offset++] & 0xFF) << 16) | (0x80 << 8) ; test_reste=0; un_ok=1; break; case 3 : in[in_i++]=(test_byte[offset++] << 24) | ((test_byte[offset++] & 0xFF) << 16) | ((test_byte[offset++] & 0xFF) << 8) | 0x80 ; test_reste=0; un_ok=1; break; default : in[in_i++]=(test_byte[offset++] << 24) | ((test_byte[offset++] & 0xFF) << 16) | ((test_byte[offset++] & 0xFF) << 8) | (test_byte[offset++] & 0xFF) ; test_reste-=4; un_ok=0; break; } } in[in_i++]=0; in[in_i++]=l; } sha=sha(sha,in); } } public sha1(String seed,int radix){ int test_length=seed.length()/2; byte []test_byte=new byte[test_length]; for(int i=0;i<test_length;i++)test_byte[i]=(byte)Integer.parseInt(seed.substring(2*(test_length-i-1), 2*(test_length-i)), radix); int l=8*test_length; int k=(448-l-1)%512; if(k<0)k+=512; int k8=(k-7)>>>3; int tot4=(test_length+1+k8+8)>>>2; int[]in=new int[16]; sha[0] = 0x67452301; sha[1] = 0xefcdab89; sha[2] = 0x98badcfe; sha[3] = 0x10325476; sha[4] = 0xc3d2e1f0; int test_reste=test_length; int offset=0; int un_ok=0; for(int i=0;i<tot4;i+=16){ int in_i=0; if(i+16<tot4){ for(int j=0;j<16;j++){ switch (test_reste) { case 0 : if(un_ok==1)in[in_i++]=0; else in[in_i++]=0x80 << 24; un_ok=1; break; case 1 : in[in_i++]=(test_byte[offset++] << 24) | (0x80 << 16); test_reste=0; un_ok=1; break; case 2 : in[in_i++]=(test_byte[offset++] << 24) | ((test_byte[offset++] & 0xFF) << 16) | (0x80 << 8) ; test_reste=0; un_ok=1; break; case 3 : in[in_i++]=(test_byte[offset++] << 24) | ((test_byte[offset++] & 0xFF) << 16) | ((test_byte[offset++] & 0xFF) << 8) | 0x80 ; test_reste=0; un_ok=1; break; default : in[in_i++]=(test_byte[offset++] << 24) | ((test_byte[offset++] & 0xFF) << 16) | ((test_byte[offset++] & 0xFF) << 8) | (test_byte[offset++] & 0xFF) ; test_reste-=4; un_ok=0; break; } } }else{ for(int j=0;j<14;j++){ switch (test_reste) { case 0 : if(un_ok==1)in[in_i++]=0; else in[in_i++]=0x80<<24; un_ok=1; break; case 1 : in[in_i++]=(test_byte[offset++] << 24) | (0x80 << 16); test_reste=0; un_ok=1; break; case 2 : in[in_i++]=(test_byte[offset++] << 24) | ((test_byte[offset++] & 0xFF) << 16) | (0x80 << 8) ; test_reste=0; un_ok=1; break; case 3 : in[in_i++]=(test_byte[offset++] << 24) | ((test_byte[offset++] & 0xFF) << 16) | ((test_byte[offset++] & 0xFF) << 8) | 0x80 ; test_reste=0; un_ok=1; break; default : in[in_i++]=(test_byte[offset++] << 24) | ((test_byte[offset++] & 0xFF) << 16) | ((test_byte[offset++] & 0xFF) << 8) | (test_byte[offset++] & 0xFF) ; test_reste-=4; un_ok=0; break; } } in[in_i++]=0; in[in_i++]=l; } sha=sha(sha,in); } } public int[]getHash(){ return sha; } public static int[] sha(int []sha,int[] in) { int a=sha[0]; int b=sha[1]; int c=sha[2]; int d=sha[3]; int e=sha[4]; int T; int []W=sha1_sch(in); for (int j = 0; j < 80; j++) { T = S(5,a) + f(j,b,c,d) + e + K[j] + W[j]; e = d; d = c; c = S(30,b); b = a; a = T; } return new int[] { sha[0] + a, sha[1] + b, sha[2] + c, sha[3] + d, sha[4] + e}; } public static int[]sha1_sch(int[]M){ int[]W=new int[80]; for(int i=0;i<16;i++)W[i]=M[i]; for(int i=16;i<80;i++)W[i]=S(1,W[i-3]^W[i-8]^W[i-14]^W[i-16]); return W; } public static int Ch(int x,int y,int z){ return ((x & y)^((~x) & z)); } public static int Maj(int x,int y,int z){ return ((x & y)^(x & z)^(y & z)); } public static int Parity(int x,int y,int z){ return x^y^z; } public static int f(int i,int x,int y,int z){ if(i<20){ return Ch(x,y,z); }else if(i<40){ return Parity(x,y,z); }else if(i<60){ return Maj(x,y,z); }else { return Parity(x,y,z); } } public static int S(int n,int x){ int xx=(x << n) | (x >>> (32-n)); return xx; } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -