Skip to content

Bad performance HmacSM3Signer#hash #965

Open

Description

@jht5945

Source file: https://github.com/aliyun/aliyun-openapi-java-sdk/blob/master/aliyun-java-sdk-core/src/main/java/com/aliyuncs/auth/signers/HmacSM3Signer.java

new BouncyCastleProvider() is very heavy, BouncyCastleProvider should use as singleton, here is the simple benchmark test code:

import org.bouncycastle.jce.provider.BouncyCastleProvider;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

public class Tt {

    public static void main(String[] args) throws NoSuchAlgorithmException, InterruptedException {
        long t1 = System.currentTimeMillis();
        for (int i = 0; i < 1000; i++) {
            String digest = Base64.getEncoder().encodeToString(hash("test".getBytes(StandardCharsets.UTF_8)));
            AssertUtil.isTrue("VeEukWUNL+xW7HTh0+Tdv84u86ZYkMKhns+IowfnaiM=".equals(digest));
        }
        System.out.println((System.currentTimeMillis() - t1) + " ms");

        long t2 = System.currentTimeMillis();
        for (int i = 0; i < 1000; i++) {
            String digest = Base64.getEncoder().encodeToString(hash2("test".getBytes(StandardCharsets.UTF_8)));
            AssertUtil.isTrue("VeEukWUNL+xW7HTh0+Tdv84u86ZYkMKhns+IowfnaiM=".equals(digest));
        }
        System.out.println((System.currentTimeMillis() - t2) + " ms");
    }

    private static String HASH_SM3 = "SM3";
    private static BouncyCastleProvider PROVIDER = new BouncyCastleProvider();

    public static byte[] hash(byte[] raw) throws NoSuchAlgorithmException {
        if (null == raw) {
            return null;
        }
        BouncyCastleProvider provider = new BouncyCastleProvider();
        MessageDigest digest = MessageDigest.getInstance(HASH_SM3, provider);
        return digest.digest(raw);
    }

    public static byte[] hash2(byte[] raw) throws NoSuchAlgorithmException {
        if (null == raw) {
            return null;
        }
        MessageDigest digest = MessageDigest.getInstance(HASH_SM3, PROVIDER);
        return digest.digest(raw);
    }
}

Outputs:

2760 ms
4 ms

Activity

  1. yndu13 commented on Jan 3, 2025

    @yndu13
    Collaborator

    Thanks for your feedback, we will consider it and reply to you as soon as possible.

  2. mvanhorn commented on Oct 20, 2026

    @mvanhorn

    HmacSM3Signer.hash constructs a new BouncyCastleProvider for every non-null input. This repeats expensive provider initialization on every call.

    A static final BouncyCastleProvider is reused across hash calls and signer instances. Each call still creates its own MessageDigest, and null inputs still return null.

    #994

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions