bouncycastle JAVA implements SM3 algorithm

bouncycastle JAVA implements SM3 algorithm (National Secret 3)

bouncycastle already supports SM3 algorithm as early as version 1.5

Here is a simple example

First introduce maven


 <dependency>
            <groupId>org.bouncycastle</groupId>
            <artifactId>bcprov-jdk15on</artifactId>
            <version>1.69</version>
 </dependency>
        

The following is an implementation example

SM3Digest sm3Digest=new SM3Digest();
byte[] srcData = "abc".getBytes();
sm3Digest.update(srcData,0,srcData.length);
byte[] hash = new byte[sm3Digest.getDigestSize()];
sm3Digest.doFinal(hash, 0);
System.out.println("sm3加密11111:"+SM3.byteArrayToHexString(hash));
//假如输出为66C7F0F462EEEDD9D1F2D46BDC10E4E24167C4875CF2F7A2297DA02B8F4BA8E0就说明符合SM3算法要求



private static char[] hexDigits = {
    
    '0', '1', '2', '3', '4', '5', '6', '7', '8',
        '9', 'A', 'B', 'C', 'D', 'E', 'F'};
public static String byteArrayToHexString(byte[] b) {
    
    
       StringBuffer resultSb = new StringBuffer();
       for (int i = 0; i < b.length; i++) {
    
    
           resultSb.append(byteToHexString(b[i]));
       }
       return resultSb.toString();
   }

private static String byteToHexString(byte b) {
    
    
     int n = b;
     if (n < 0)
         n = 256 + n;
     int d1 = n / 16;
     int d2 = n % 16;
     return ""+hexDigits[d1] + hexDigits[d2];
 }

おすすめ

転載: blog.csdn.net/qq_38881740/article/details/119380312