Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions src/main/java/com/aliyun/oss/common/auth/HmacSHA1Signature.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package com.aliyun.oss.common.auth;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.Mac;

import com.aliyun.oss.common.utils.BinaryUtil;
Expand All @@ -40,8 +41,11 @@ public class HmacSHA1Signature extends ServiceSignature {

private static final Object LOCK = new Object();

/* Prototype of the Mac instance. */
private static Mac macInstance;
private final ThreadLocal<Mac> reference;

public HmacSHA1Signature() {
this.reference = new MacThreadLocal();
}

public String getAlgorithm() {
return ALGORITHM;
Expand All @@ -53,15 +57,31 @@ public String getVersion() {

public String computeSignature(String key, String data) {
try {
byte[] signData = sign(key.getBytes(DEFAULT_ENCODING), data.getBytes(DEFAULT_ENCODING), macInstance,
LOCK, ALGORITHM);
byte[] signData = sign(key.getBytes(DEFAULT_ENCODING), data.getBytes(DEFAULT_ENCODING), getMac(),
LOCK, ALGORITHM);
return BinaryUtil.toBase64String(signData);
} catch (UnsupportedEncodingException ex) {
throw new RuntimeException("Unsupported algorithm: " + DEFAULT_ENCODING, ex);
}
}

public byte[] computeHash(byte[] key, byte[] data) {
return sign(key, data, macInstance, LOCK, ALGORITHM);
return sign(key, data, getMac(), LOCK, ALGORITHM);
}

public Mac getMac() {
return reference.get();
}

private static class MacThreadLocal extends ThreadLocal<Mac> {

@Override
protected Mac initialValue() {
try {
return Mac.getInstance(HmacSHA1Signature.ALGORITHM);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("Unsupported algorithm: " + HmacSHA1Signature.ALGORITHM, e);
}
}
}
}
30 changes: 25 additions & 5 deletions src/main/java/com/aliyun/oss/common/auth/HmacSHA256Signature.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package com.aliyun.oss.common.auth;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.Mac;

import com.aliyun.oss.common.utils.BinaryUtil;
Expand All @@ -40,8 +41,11 @@ public class HmacSHA256Signature extends ServiceSignature {

private static final Object LOCK = new Object();

/* Prototype of the Mac instance. */
private static Mac macInstance;
private final ThreadLocal<Mac> reference;

public HmacSHA256Signature() {
this.reference = new HmacSHA256Signature.MacThreadLocal();
}

public String getAlgorithm() {
return ALGORITHM;
Expand All @@ -53,15 +57,31 @@ public String getVersion() {

public String computeSignature(String key, String data) {
try {
byte[] signData = sign(key.getBytes(DEFAULT_ENCODING), data.getBytes(DEFAULT_ENCODING), macInstance,
LOCK, ALGORITHM);
byte[] signData = sign(key.getBytes(DEFAULT_ENCODING), data.getBytes(DEFAULT_ENCODING), getMac(),
LOCK, ALGORITHM);
return BinaryUtil.toBase64String(signData);
} catch (UnsupportedEncodingException ex) {
throw new RuntimeException("Unsupported algorithm: " + DEFAULT_ENCODING, ex);
}
}

public byte[] computeHash(byte[] key, byte[] data) {
return sign(key, data, macInstance, LOCK, ALGORITHM);
return sign(key, data, getMac(), LOCK, ALGORITHM);
}

public Mac getMac() {
return reference.get();
}

private static class MacThreadLocal extends ThreadLocal<Mac> {

@Override
protected Mac initialValue() {
try {
return Mac.getInstance(HmacSHA256Signature.ALGORITHM);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("Unsupported algorithm: " + HmacSHA256Signature.ALGORITHM, e);
}
}
}
}