安全哈希算法(Secure Hash Algorithm)主要適用於數字簽名標準 (Digital Signature Standard DSS)裏面定義的數字簽名算法(Digital Signature Algorithm DSA)。對於長度小於2^64位的消息,SHA1會產生一個160位的消息摘要。當接收到消息的時候,這個消息摘要能夠用來驗證數據的完整性。在傳輸的過程當中,數據極可能會發生變化,那麼這時候就會產生不一樣的消息摘要。 SHA1有以下特性:不能夠從消息摘要中復原信息;兩個不一樣的消息不會產生一樣的消息摘要,(但會有1x10 ^ 48分之一的機率出現相同的消息摘要,通常使用時忽略)。git
(注:最後有案例代碼)算法
1 public class SHA1 { 2 private final int[] abcde = { 0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0 }; 3 // 摘要數據存儲數組 4 private int[] digestInt = new int[5]; 5 // 計算過程當中的臨時數據存儲數組 6 private int[] tmpData = new int[80]; 7 8 // 計算sha-1摘要 9 private int process_input_bytes(byte[] bytedata) { 10 // 初試化常量 11 System.arraycopy(abcde, 0, digestInt, 0, abcde.length); 12 // 格式化輸入字節數組,補10及長度數據 13 byte[] newbyte = byteArrayFormatData(bytedata); 14 // 獲取數據摘要計算的數據單元個數 15 int MCount = newbyte.length / 64; 16 // 循環對每一個數據單元進行摘要計算 17 for (int pos = 0; pos < MCount; pos++) { 18 // 將每一個單元的數據轉換成16個整型數據,並保存到tmpData的前16個數組元素中 19 for (int j = 0; j < 16; j++) { 20 tmpData[j] = byteArrayToInt(newbyte, (pos * 64) + (j * 4)); 21 } 22 // 摘要計算函數 23 encrypt(); 24 } 25 return 20; 26 } 27 28 // 格式化輸入字節數組格式 29 private byte[] byteArrayFormatData(byte[] bytedata) { 30 // 補0數量 31 int zeros = 0; 32 // 補位後總位數 33 int size = 0; 34 // 原始數據長度 35 int n = bytedata.length; 36 // 模64後的剩餘位數 37 int m = n % 64; 38 // 計算添加0的個數以及添加10後的總長度 39 if (m < 56) { 40 zeros = 55 - m; 41 size = n - m + 64; 42 } else if (m == 56) { 43 zeros = 63; 44 size = n + 8 + 64; 45 } else { 46 zeros = 63 - m + 56; 47 size = (n + 64) - m + 64; 48 } 49 // 補位後生成的新數組內容 50 byte[] newbyte = new byte[size]; 51 // 複製數組的前面部分 52 System.arraycopy(bytedata, 0, newbyte, 0, n); 53 // 得到數組Append數據元素的位置 54 int l = n; 55 // 補1操做 56 newbyte[l++] = (byte) 0x80; 57 // 補0操做 58 for (int i = 0; i < zeros; i++) { 59 newbyte[l++] = (byte) 0x00; 60 } 61 // 計算數據長度,補數據長度位共8字節,長整型 62 long N = (long) n * 8; 63 byte h8 = (byte) (N & 0xFF); 64 byte h7 = (byte) ((N >> 8) & 0xFF); 65 byte h6 = (byte) ((N >> 16) & 0xFF); 66 byte h5 = (byte) ((N >> 24) & 0xFF); 67 byte h4 = (byte) ((N >> 32) & 0xFF); 68 byte h3 = (byte) ((N >> 40) & 0xFF); 69 byte h2 = (byte) ((N >> 48) & 0xFF); 70 byte h1 = (byte) (N >> 56); 71 newbyte[l++] = h1; 72 newbyte[l++] = h2; 73 newbyte[l++] = h3; 74 newbyte[l++] = h4; 75 newbyte[l++] = h5; 76 newbyte[l++] = h6; 77 newbyte[l++] = h7; 78 newbyte[l++] = h8; 79 return newbyte; 80 } 81 82 private int f1(int x, int y, int z) { 83 return (x & y) | (~x & z); 84 } 85 86 private int f2(int x, int y, int z) { 87 return x ^ y ^ z; 88 } 89 90 private int f3(int x, int y, int z) { 91 return (x & y) | (x & z) | (y & z); 92 } 93 94 private int f4(int x, int y) { 95 return (x << y) | x >>> (32 - y); 96 } 97 98 // 單元摘要計算函數 99 private void encrypt() { 100 for (int i = 16; i <= 79; i++) { 101 tmpData[i] = f4(tmpData[i - 3] ^ tmpData[i - 8] ^ tmpData[i - 14] ^ tmpData[i - 16], 1); 102 } 103 int[] tmpabcde = new int[5]; 104 for (int i1 = 0; i1 < tmpabcde.length; i1++) { 105 tmpabcde[i1] = digestInt[i1]; 106 } 107 for (int j = 0; j <= 19; j++) { 108 int tmp = f4(tmpabcde[0], 5) + f1(tmpabcde[1], tmpabcde[2], tmpabcde[3]) + tmpabcde[4] + tmpData[j] 109 + 0x5a827999; 110 tmpabcde[4] = tmpabcde[3]; 111 tmpabcde[3] = tmpabcde[2]; 112 tmpabcde[2] = f4(tmpabcde[1], 30); 113 tmpabcde[1] = tmpabcde[0]; 114 tmpabcde[0] = tmp; 115 } 116 for (int k = 20; k <= 39; k++) { 117 int tmp = f4(tmpabcde[0], 5) + f2(tmpabcde[1], tmpabcde[2], tmpabcde[3]) + tmpabcde[4] + tmpData[k] 118 + 0x6ed9eba1; 119 tmpabcde[4] = tmpabcde[3]; 120 tmpabcde[3] = tmpabcde[2]; 121 tmpabcde[2] = f4(tmpabcde[1], 30); 122 tmpabcde[1] = tmpabcde[0]; 123 tmpabcde[0] = tmp; 124 } 125 for (int l = 40; l <= 59; l++) { 126 int tmp = f4(tmpabcde[0], 5) + f3(tmpabcde[1], tmpabcde[2], tmpabcde[3]) + tmpabcde[4] + tmpData[l] 127 + 0x8f1bbcdc; 128 tmpabcde[4] = tmpabcde[3]; 129 tmpabcde[3] = tmpabcde[2]; 130 tmpabcde[2] = f4(tmpabcde[1], 30); 131 tmpabcde[1] = tmpabcde[0]; 132 tmpabcde[0] = tmp; 133 } 134 for (int m = 60; m <= 79; m++) { 135 int tmp = f4(tmpabcde[0], 5) + f2(tmpabcde[1], tmpabcde[2], tmpabcde[3]) + tmpabcde[4] + tmpData[m] 136 + 0xca62c1d6; 137 tmpabcde[4] = tmpabcde[3]; 138 tmpabcde[3] = tmpabcde[2]; 139 tmpabcde[2] = f4(tmpabcde[1], 30); 140 tmpabcde[1] = tmpabcde[0]; 141 tmpabcde[0] = tmp; 142 } 143 for (int i2 = 0; i2 < tmpabcde.length; i2++) { 144 digestInt[i2] = digestInt[i2] + tmpabcde[i2]; 145 } 146 for (int n = 0; n < tmpData.length; n++) { 147 tmpData[n] = 0; 148 } 149 } 150 151 // 4字節數組轉換爲整數 152 private int byteArrayToInt(byte[] bytedata, int i) { 153 return ((bytedata[i] & 0xff) << 24) | ((bytedata[i + 1] & 0xff) << 16) | ((bytedata[i + 2] & 0xff) << 8) 154 | (bytedata[i + 3] & 0xff); 155 } 156 157 // 整數轉換爲4字節數組 158 private void intToByteArray(int intValue, byte[] byteData, int i) { 159 byteData[i] = (byte) (intValue >>> 24); 160 byteData[i + 1] = (byte) (intValue >>> 16); 161 byteData[i + 2] = (byte) (intValue >>> 8); 162 byteData[i + 3] = (byte) intValue; 163 } 164 165 // 將字節轉換爲十六進制字符串 166 private static String byteToHexString(byte ib) { 167 char[] Digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; 168 char[] ob = new char[2]; 169 ob[0] = Digit[(ib >>> 4) & 0X0F]; 170 ob[1] = Digit[ib & 0X0F]; 171 String s = new String(ob); 172 return s; 173 } 174 175 // 將字節數組轉換爲十六進制字符串 176 private static String byteArrayToHexString(byte[] bytearray) { 177 String strDigest = ""; 178 for (int i = 0; i < bytearray.length; i++) { 179 strDigest += byteToHexString(bytearray[i]); 180 } 181 return strDigest; 182 } 183 184 // 計算sha-1摘要,返回相應的字節數組 185 public byte[] getDigestOfBytes(byte[] byteData) { 186 process_input_bytes(byteData); 187 byte[] digest = new byte[20]; 188 for (int i = 0; i < digestInt.length; i++) { 189 intToByteArray(digestInt[i], digest, i * 4); 190 } 191 return digest; 192 } 193 194 // 計算sha-1摘要,返回相應的十六進制字符串 195 public String getDigestOfString(byte[] byteData) { 196 return byteArrayToHexString(getDigestOfBytes(byteData)); 197 } 198 199 public static void main(String[] args) { 200 String data = "123456"; 201 String digest = new SHA1().getDigestOfString(data.getBytes()); 202 } 203 }