1 sha-1的核心计算类
package com.encryption.presentationTool.encryptionTool;
import java.util.Arrays;
public class SHA1 {
private static final boolean hexcase = false;
private static final String b64pad = "=";
private static final int chrsz = 8;
public String hex_sha1(String s) {
s = (s == null) ? "" : s;
int[][] result = core_sha1(str2binb(s), s.length() * chrsz);
int[] final_result = new int[5];
for (int i = 0; i <5 ; i++){
final_result[i] = result[0][i];
}
return binb2hex(final_result);
}
public String binb2b64(int[] binarray) {
String tab = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789+/";
StringBuilder str = new StringBuilder();
binarray = strechbinarray(binarray, binarray.length * 4);
for (int i = 0; i < binarray.length * 4; i += 3) {
int triplet = (((binarray[i >> 2] >> 8 * (3 - i % 4)) & 0xff) << 16)
| (((binarray[i + 1 >> 2] >> 8 * (3 - (i + 1) % 4)) & 0xff) << 8)
| ((binarray[i + 2 >> 2] >> 8 * (3 - (i + 2) % 4)) & 0xff);
for (int j = 0; j < 4; j++) {
if (i * 8 + j * 6 > binarray.length * 32) {
str.append(b64pad);
} else {
str.append(tab.charAt((triplet >> 6 * (3 - j)) & 0x3f));
}
}
}
return cleanb64str(str.toString());
}
public String binb2hex(int[] binarray) {
String hex_tab = hexcase ? "0123456789abcdef" : "0123456789abcdef";
StringBuilder str = new StringBuilder();
for (int i = 0; i < binarray.length * 4; i++) {
char a = (char) hex_tab.charAt((binarray[i >> 2] >> ((3 - i % 4) * 8 + 4)) & 0xf);
char b = (char) hex_tab.charAt((binarray[i >> 2] >> ((3 - i % 4) * 8)) & 0xf);
str.append(Character.toString(a)).append(Character.toString(b));
}
return str.toString();
}
public String cleanb64str(String str) {
str = (str == null) ? "" : str;
int len = str.length();
if (len <= 1) {
return str;
}
char trailchar = str.charAt(len - 1);
StringBuilder trailstr = new StringBuilder();
for (int i = len - 1; i >= 0 && str.charAt(i) == trailchar; i--) {
trailstr.append(str.charAt(i));
}
return str.substring(0, str.indexOf(trailstr.toString()));
}
public String getInputStretchHexStr(int[] x, int len){
System.out.println("x:"+ Arrays.toString(x)+"len:"+len);
int size = (len >> 5);
x = strechbinarray(x, size);
x[len >> 5] |= 0x80 << (24 - len % 32);
size = ((len + 64 >> 9) << 4) + 15;
x = strechbinarray(x, size);
x[((len + 64 >> 9) << 4) + 15] = len;
return binb2hex(x);
}
public int[][] core_sha1(int[] x, int len) {
int[][] retval = new int[2][5];
System.out.println("x:"+ Arrays.toString(x)+"len:"+len);
int size = (len >> 5);
x = strechbinarray(x, size);
x[len >> 5] |= 0x80 << (24 - len % 32);
size = ((len + 64 >> 9) << 4) + 15;
x = strechbinarray(x, size);
x[((len + 64 >> 9) << 4) + 15] = len;
int[] w = new int[80];
int a = 0x67452301;
int b = 0xEFCDAB89;
int c = 0x98BADCFE;
int d = 0x10325476;
int e = 0xC3D2E1F0;
for (int i = 0; i < x.length; i += 16) {
int olda = a;
int oldb = b;
int oldc = c;
int oldd = d;
int olde = e;
for (int j = 0; j < 80; j++) {
if (j < 16) {
w[j] = x[i + j];
} else {
w[j] = rol(w[j - 3] ^ w[j - 8] ^ w[j - 14] ^ w[j - 16], 1);
}
int t = safe_add(safe_add(rol(a, 5), sha1_ft(j, b, c, d)), safe_add(safe_add(e, w[j]), sha1_kt(j)));
e = d;
d = c;
c = rol(b, 30);
b = a;
a = t;
retval[1][0] = a;
retval[1][1] = b;
retval[1][2] = c;
retval[1][3] = d;
retval[1][4] = e;
}
a = safe_add(a, olda);
b = safe_add(b, oldb);
c = safe_add(c, oldc);
d = safe_add(d, oldd);
e = safe_add(e, olde);
}
retval[0][0] = a;
retval[0][1] = b;
retval[0][2] = c;
retval[0][3] = d;
retval[0][4] = e;
return retval;
}
public int rol(int num, int cnt) {
return (num << cnt) | (num >>> (32 - cnt));
}
public int safe_add(int x, int y) {
int lsw = (int) (x & 0xffff) + (int) (y & 0xffff);
System.out.println("lsw:" + lsw);
int msw = (x >> 16) + (y >> 16) + (lsw >> 16);
System.out.println("msw:" + msw);
return (msw << 16) | (lsw & 0xffff);
}
public int sha1_ft(int t, int b, int c, int d) {
if (t < 20)
return (b & c) | ((~b) & d);
if (t < 40)
return b ^ c ^ d;
if (t < 60)
return (b & c) | (b & d) | (c & d);
return b ^ c ^ d;
}
public int sha1_kt(int t) {
return (t < 20) ? 1518500249 : (t < 40) ? 1859775393 : (t < 60) ? -1894007588 : -899497514;
}
public boolean sha1_vm_test() {
return hexcase ? hex_sha1("abc").equals("a9993e364706816aba3e25717850c26c9cd0d89d") : hex_sha1("abc").equals(
"a9993e364706816aba3e25717850c26c9cd0d89d");
}
public int[] str2binb(String str) {
str = (str == null) ? "" : str;
int[] tmp = new int[str.length() * chrsz];
int mask = (1 << chrsz) - 1;
for (int i = 0; i < str.length() * chrsz; i += chrsz) {
System.out.println("tmp:"+ Arrays.toString(tmp));
tmp[i >> 5] |= ((int) (str.charAt(i / chrsz)) & mask) << (24 - i % 32);
}
int len = 0;
System.out.println("tmp.length:"+ tmp.length);
for (int i = 0; i < tmp.length && tmp[i] != 0; i++, len++);
System.out.println("tmp:"+ Arrays.toString(tmp));
System.out.println("len:"+len);
int[] bin = new int[len];
for (int i = 0; i < len; i++) {
bin[i] = tmp[i];
}
return bin;
}
public int[] strechbinarray(int[] oldbin, int size) {
int currlen = oldbin.length;
if (currlen >= size + 1) {
return oldbin;
}
int[] newbin = new int[size + 1];
for (int i = 0; i < size; newbin[i] = 0, i++);
for (int i = 0; i < currlen; i++) {
newbin[i] = oldbin[i];
}
return newbin;
}
public static void main(String[] args) {
SHA1 sha1 = new SHA1();
System.out.println(sha1.hex_sha1("123456"));
}
}
2 直接运行hex_sha1()方法
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/bbb8f17e7294ad512dcc5edcb880f9b9.png)
3 获得算法的中间过程
3.1 视图(VO)封装类
package com.encryption.presentationTool.VO;
import java.util.Arrays;
public class SHA1VO {
private int[] first_80;
private String[] first_80_hex;
private int[] retval;
private String retval_base64Str;
private String[] retval_hex;
private String input;
private String input_hexStr ;
private String input_stretch_hexStr;
public int[] getFirst_80() {
return first_80;
}
public void setFirst_80(int[] first_80) {
this.first_80 = first_80;
}
public String[] getFirst_80_hex() {
return first_80_hex;
}
public void setFirst_80_hex(String[] first_80_hex) {
this.first_80_hex = first_80_hex;
}
public int[] getRetval() {
return retval;
}
public void setRetval(int[] retval) {
this.retval = retval;
}
public String getRetval_base64Str() {
return retval_base64Str;
}
public void setRetval_base64Str(String retval_base64Str) {
this.retval_base64Str = retval_base64Str;
}
public String[] getRetval_hex() {
return retval_hex;
}
public void setRetval_hex(String[] retval_hex) {
this.retval_hex = retval_hex;
}
public String getInput_hexStr() {
return input_hexStr;
}
public void setInput_hexStr(String input_hexStr) {
this.input_hexStr = input_hexStr;
}
public String getInput_stretch_hexStr() {
return input_stretch_hexStr;
}
public void setInput_stretch_hexStr(String input_stretch_hexStr) {
this.input_stretch_hexStr = input_stretch_hexStr;
}
public String getInput() {
return input;
}
public void setInput(String input) {
this.input = input;
}
@Override
public String toString() {
return "SHA1VO{" +
"first_80=" + Arrays.toString(first_80) +
", first_80_hex=" + Arrays.toString(first_80_hex) +
", retval=" + Arrays.toString(retval) +
", retval_base64Str='" + retval_base64Str + '\'' +
", retval_hex=" + Arrays.toString(retval_hex) +
", input='" + input + '\'' +
", input_hexStr='" + input_hexStr + '\'' +
", input_stretch_hexStr='" + input_stretch_hexStr + '\'' +
'}';
}
}
3.2 调用VO封装计算过程
SHA1VO sha1VO = new SHA1VO();
sha1VO.setInput(input);
codeTool codeTool = new codeTool();
sha1VO.setInput_hexStr(codeTool.strToAsciiHexStr(input));
SHA1 sha1 = new SHA1();
sha1VO.setInput_stretch_hexStr(sha1.getInputStretchHexStr(sha1.str2binb(input),input.length() * 8));
int[][] result = sha1.core_sha1(sha1.str2binb(input),input.length() * 8);
sha1VO.setFirst_80(result[1]);
sha1VO.setFirst_80_hex(codeTool.intToAsciiHexStr(result[1]));
sha1VO.setRetval(result[0]);
sha1VO.setRetval_base64Str(sha1.binb2b64(result[0]));
sha1VO.setRetval_hex(codeTool.intToAsciiHexStr(result[0]));
3.3 结合前端页面的测试结果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/b67ce8ba38a14b40525feda4336ab3f7.png)