| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- package com.doc.common.utils.encrypt;
- import com.doc.common.utils.SecurityUtils;
- import org.bouncycastle.crypto.digests.SM3Digest;
- import org.bouncycastle.util.encoders.Hex;
- import java.util.Random;
- /**
- * SM3 HASH加密
- *
- * @author wukai
- * @date 2023-11-02
- */
- public class Sm3Util {
- public static String encrypt(String str) {
- SM3Digest sm3 = new SM3Digest();
- // 将消息转换为字节数组
- byte[] input = str.getBytes();
- // 更新摘要
- sm3.update(input, 0, input.length);
- // 完成摘要计算
- byte[] result = new byte[sm3.getDigestSize()];
- sm3.doFinal(result, 0);
- // 将摘要结果转换为十六进制字符串
- String digestHex = Hex.toHexString(result);
- return digestHex;
- }
- public static void main(String[] args) throws Exception {
- Random random = new Random();
- int number = random.nextInt(9000) + 1000;
- System.out.println("随机数字: " + number);
- String message = "1qaz@WSX";
- String en1 = encrypt(message);
- String en2 = SecurityUtils.encryptPassword(en1);
- System.out.println("SM3 Digest 1: " + en1);
- System.out.println("SM3 Digest 2: " + en2);
- System.err.println(SecurityUtils.encryptPassword("admin123"));
- }
- }
|