aliyun / aliyun/aliyun-openapi-java-sdk
Bad performance HmacSM3Signer#hash
Open
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 1.4k
- Forks
- 1.1k
- PR merge metrics
- No merged PRs in 30d
Description
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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
The affected source is aliyun-java-sdk-core/src/main/java/com/aliyuncs/auth/signers/HmacSM3Signer.java. Start by reading HmacSM3Signer#hash and compare its provider construction with the singleton approach in the supplied benchmark. Done means preserving the expected SM3 digest while avoiding repeated BouncyCastleProvider construction and confirming the performance difference with the benchmark.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- cryptography
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100