1.SHA-1算法
SHA-1算法也是一种哈希算法。
- 输出160 bits/20bytes
- 由美国国家安全局开发
SHA-0/SHA-1/SHA-256/SHA-512
*SHA-0有问题,已经作废了
算法 | 输出长度:bits | 输出长度:bytes |
---|---|---|
SHA-1 | 160 bits | 20 bytes |
SHA-256 | 256 bits | 32 bytes |
SHA-512 | 512 bits | 64 bytes |
2.示例代码
SHA-1用法同MD5
import java.math.BigInteger;
import java.security.MessageDigest;
import java.util.Arrays;
public class SplitString {
public static void main(String[] args) throws Exception {
SplitString ns = new SplitString("helloworld");
String[] ng = {"hello","world"};
SplitString ns2 = new SplitString(ng);
}
public SplitString(String[] ss) throws Exception {
MessageDigest md = MessageDigest.getInstance("SHA-1");
for(String s:ss){
md.update(s.getBytes("UTF-8"));
}
byte[] br = md.digest();
String result = String.format("%032x",new BigInteger(1,br));
System.out.println(Arrays.toString(ss)+": "+result);
}
public SplitString(String s) throws Exception{
String[] ss = {s,};
new SplitString(ss);
}
}
## 3.优化算法代码,可以指定算法
```#java
package com.testList;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.util.Arrays;
public class SplitString {
public static byte[] digest(String hashAlggorithm, byte[] input){
MessageDigest md;
try{
md = MessageDigest.getInstance(hashAlggorithm);
}catch (Exception e){
throw new RuntimeException(e);
}
md.update(input);
return md.digest();
}
public static void main(String[] args) throws Exception{
String s = "Java摘要算法测试";
byte[] input = s.getBytes("UTF-8");
byte[] r1 = digest("MD5",input);
System.out.println(r1.length+":"+String.format("%0"+(r1.length2)+"x",new BigInteger(1,r1)));
byte[] r2 = digest("SHA-1",input);
System.out.println(r2.length+":"+String.format("%0"+(r2.length2)+"x",new BigInteger(1,r2)));
byte[] r3 = digest("SHA-256",input);
System.out.println(r3.length+":"+String.format("%0"+(r3.length2)+"x",new BigInteger(1,r3)));
byte[] r4 = digest("RipeMD160",input);
System.out.println(r4.length+":"+String.format("%0"+(r4.length2)+"x",new BigInteger(1,r4)));
}
<img src="https://img2018.cnblogs.com/blog/1418970/201905/1418970-20190508220053277-957444074.png" width="500" />
[通过oracle官网可以查看](https://docs.oracle.com/javase/8/docs/api/javax/xml/crypto/dsig/DigestMethod.html)
## 4.总结:
* SHA-1算法是比MD5更安全的哈希算法
* 其他哈希算法:SHA-256/SHA-512/RipeMD160
## 5.问题:
### 5.1 为什么经过哈希算法处理后,输出的字符串是byte数组长度的2倍
原因:byte转化为16进制是2位,因此数组的长度乘以2,即是输出字符串的长度
```#java
import java.math.BigInteger;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.Arrays;
public class SplitString {
public static byte[] digest(String hashAlggorithm, byte[] input){
MessageDigest md;
try{
md = MessageDigest.getInstance(hashAlggorithm);
}catch (Exception e){
throw new RuntimeException(e);
}
md.update(input);
return md.digest();
}
public static void main(String[] args) throws Exception{
String s = "Java摘要算法测试";
byte[] input = s.getBytes("UTF-8");
byte[] r1 = digest("MD5",input);
System.out.println(r1.length+":"+String.format("%0"+(r1.length*2)+"x",new BigInteger(1,r1)));
//为什么输出的长度是byte长度的2位
//byte转化为16进制是2位,因此数组的长度乘以2,即是输出字符串的长度
System.out.println(Arrays.toString(r1));
for(byte b:r1){
System.out.print(Integer.toHexString(b)+" ");
}
}
}