Sm3Util.java 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package com.doc.common.utils.encrypt;
  2. import com.doc.common.utils.SecurityUtils;
  3. import org.bouncycastle.crypto.digests.SM3Digest;
  4. import org.bouncycastle.util.encoders.Hex;
  5. import java.util.Random;
  6. /**
  7. * SM3 HASH加密
  8. *
  9. * @author wukai
  10. * @date 2023-11-02
  11. */
  12. public class Sm3Util {
  13. public static String encrypt(String str) {
  14. SM3Digest sm3 = new SM3Digest();
  15. // 将消息转换为字节数组
  16. byte[] input = str.getBytes();
  17. // 更新摘要
  18. sm3.update(input, 0, input.length);
  19. // 完成摘要计算
  20. byte[] result = new byte[sm3.getDigestSize()];
  21. sm3.doFinal(result, 0);
  22. // 将摘要结果转换为十六进制字符串
  23. String digestHex = Hex.toHexString(result);
  24. return digestHex;
  25. }
  26. public static void main(String[] args) throws Exception {
  27. Random random = new Random();
  28. int number = random.nextInt(9000) + 1000;
  29. System.out.println("随机数字: " + number);
  30. String message = "1qaz@WSX";
  31. String en1 = encrypt(message);
  32. String en2 = SecurityUtils.encryptPassword(en1);
  33. System.out.println("SM3 Digest 1: " + en1);
  34. System.out.println("SM3 Digest 2: " + en2);
  35. System.err.println(SecurityUtils.encryptPassword("admin123"));
  36. }
  37. }