我也是簡單的查閱了網上的一些資料
發現用sha-2的人還是相對比較多的了,有原因是因爲其他存在破解的風險性還有聽說pip等其他的一些將md5算法轉而sha-256
sha-256算法是sha-2裏面的其中一個,爲什麼呢,主要也是因爲這個算法大多都在用,至少現在沒有什麼問題
具體的sha-256算法實現如下所示:
package com.read.data;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.security.MessageDigest;
import static com.read.data.utils.CommonUtils.printf;
@RunWith(SpringRunner.class)
@SpringBootTest
public class ReadDataAuthorityApplicationTests {
@Test
public void contextLoads() {
printf(getSHA256("123"));
}
/**
* * 利用java原生的類實現SHA256加密
* * @param str 加密後的報文
* * @return
*
*/
public static String getSHA256(String str) {
MessageDigest messageDigest;
String encodestr = "";
try {
messageDigest = MessageDigest.getInstance("SHA-256");
messageDigest.update(str.getBytes("UTF-8"));
encodestr = byte2Hex(messageDigest.digest());
} catch (Exception e) {
e.printStackTrace();
}
return encodestr;
}
/**
* * 將byte轉爲16進制
* * @param bytes
* * @return
*
*/
private static String byte2Hex(byte[] bytes) {
StringBuffer stringBuffer = new StringBuffer();
String temp = null;
for (int i = 0; i < bytes.length; i++) {
temp = Integer.toHexString(bytes[i] & 0xFF);
if (temp.length() == 1) {
//1得到一位的進行補0操作
stringBuffer.append("0");
}
stringBuffer.append(temp);
}
return stringBuffer.toString();
}
}