个人随笔
目录
工具类:Java实现SHA256算法
2020-09-11 23:03:27

一个Hash函数的安全性高低最终要看能否找到函数的整体碰撞,由于SHA-256算法具有迭代型结构,根据迭代算法的雪崩效应,随着轮数的增加,相应的整体碰撞复杂度会急剧上升,这就使得找到整体碰撞变得非常困难,直至目前现有的攻击还无法找到SHA-256的一个整体碰撞。因此,SHA-256算法被认为是目前最安全的Hash函数之一。

  1. /**
  2. * @author lin
  3. */
  4. public class SHA256 {
  5. public static String encrypt(String str){
  6. MessageDigest messageDigest;
  7. String encodeStr = "";
  8. try {
  9. messageDigest = MessageDigest.getInstance("SHA-256");
  10. messageDigest.update(str.getBytes("UTF-8"));
  11. encodeStr = byte2Hex(messageDigest.digest());
  12. } catch (NoSuchAlgorithmException e) {
  13. e.printStackTrace();
  14. } catch (UnsupportedEncodingException e) {
  15. e.printStackTrace();
  16. }
  17. return encodeStr;
  18. }
  19. /**
  20. * @param bytes
  21. * @return
  22. */
  23. private static String byte2Hex(byte[] bytes){
  24. StringBuffer stringBuffer = new StringBuffer();
  25. String temp = null;
  26. for (int i=0;i<bytes.length;i++){
  27. temp = Integer.toHexString(bytes[i] & 0xFF);
  28. if (temp.length()==1){
  29. stringBuffer.append("0");
  30. }
  31. stringBuffer.append(temp);
  32. }
  33. return stringBuffer.toString();
  34. }
  35. public static void main(String[] args) {
  36. String url = "https://www.suibibk.com/blog/579412311547052032/557891087667036160/611874046816026624";
  37. String sha256 = SHA256.encrypt(url);
  38. System.out.println("sha256:"+sha256);
  39. }
  40. }
 1174

啊!这个可能是世界上最丑的留言输入框功能~


当然,也是最丑的留言列表

有疑问发邮件到 : suibibk@qq.com 侵权立删
Copyright : 个人随笔   备案号 : 粤ICP备18099399号-2