📄 shs.java
字号:
/*Christoforos Pirillos @ Villanova University - May 1999based on code from the book "Java Network Programming" by Hughes*/package encryption;/** Provides integrity checking on any streams-based communicationschannel*/public class SHS extends Hash {/**Returns a digest of the specified byte array. The digest is computedfrom len bytes of the array, starting from index off*/public byte[] digest (byte[] text, int off, int len) { byte[] message = pad (text, off, len); int[] data = new int[80]; int[] digest = { h0,h1,h2,h3,h4}; for (int i=0; i<message.length; i +=64) { Crypt.bytesToInts (message, i, data, 0, 16) ; transform (digest, data); } byte[] result = new byte[20]; Crypt.intsToBytes (digest, 0, 5, result, 0); return result;}protected static final int h0 = 0x67452301;protected static final int h1 = 0xEFCDAB89;protected static final int h2 = 0x98BADCFE;protected static final int h3 = 0x10325476;protected static final int h4 = 0xC3D2E1F0;/**Pads the specified text to be a multiple of the hash block size*/protected byte[] pad (byte[] text, int off, int len) { int padLen = (len + 9 + 63) & ~63; byte[] paddedText = new byte[padLen]; System.arraycopy (text, off, paddedText, 0, len); paddedText[len] = (byte) 0x80; Crypt.longToBytes ((long) len << 3, paddedText, padLen -8); return paddedText;}/**Performs the body of the SHA digest. Expands the 16 int values in thedata array into 80 by applying various XOR and shift operations.NOTE: this class contains some corrections and it provides differentresults than the original implementation, so the original test cases doapply here.*/protected final void transform (int[] digest, int[] data) { int a = digest[0]; int b = digest[1]; int c = digest[2]; int d = digest[3]; int e = digest[4];for (int i=16; i<80; ++i) { int tmp = data[i-3]^data[i-8]^data[i-14]^data[i-16]; data[i] = (tmp<<1) | (tmp >>>31);}for (int i=0;i<80;++i) { int temp = ((a<<5) | (a>>>27)) + e +data[i]; if (i<20) temp += ((b&c) | (~b & d))+k1; else if (i<40) temp+=(b^c^d)+k2; else if (i<60) temp+=((b&c) | (b&d) | (c&d))+k3; else temp+=(b^c^d)+k4; e=d; d=c; c=((b<<30) | (b>>>2)); b=a; a=temp;}digest[0]+=a;digest[1]+=b;digest[2]+=c;digest[3]+=d;digest[4]+=e;}protected static final int k1=0x5A827999;protected static final int k2=0x6ED9EBA1;protected static final int k3=0x8F1BBCDC;protected static final int k4=0xCA62C1D6;/**This algorithm produces a 160-bit digest or 20 bytes. This returns thenumber of bytes used*/public int digestSize () { return 20;}} /*end of class SHS */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -