Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

修复签名方法 #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
77 changes: 47 additions & 30 deletions src/main/java/com/qcloud/image/sign/Sign.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,52 @@
*/
public class Sign {

/**
* 返回图片识别的签名
*
* @param cred
* 包含用户秘钥信息
* @param bucketName
* bucket名
* @param expired
* 超时时间
* @return 返回base64编码的字符串
* @throws AbstractImageException 异常
*/
public static String appSign(Credentials cred, String bucketName, long expired) throws AbstractImageException {
int appId = cred.getAppId();
String secretId = cred.getSecretId();
String secretKey = cred.getSecretKey();
long now = System.currentTimeMillis() / 1000;
int rdm = Math.abs(new Random().nextInt());
String plainText = String.format("a=%d&b=%s&k=%s&t=%d&e=%d", appId, bucketName, secretId, now, now+expired);
byte[] hmacDigest;
try {
hmacDigest = CommonCodecUtils.HmacSha1(plainText, secretKey);
} catch (Exception e) {
throw new UnknownException(e.getMessage());
}
byte[] signContent = new byte[hmacDigest.length + plainText.getBytes().length];
System.arraycopy(hmacDigest, 0, signContent, 0, hmacDigest.length);
System.arraycopy(plainText.getBytes(), 0, signContent, hmacDigest.length, plainText.getBytes().length);

return CommonCodecUtils.Base64Encode(signContent);
/**
* 返回万象图片的多次有效签名
*
* @param cred 包含用户秘钥信息
* @param bucketName bucket名
* @param expired 超时时间
* @return 返回base64编码的字符串
* @throws AbstractImageException 异常
*/
public static String appSign(Credentials cred, String bucketName, long expired) throws AbstractImageException {
return appSign(cred, bucketName, "", expired);
}

/**
* 返回万象图片特定资源的多次有效签名
*
* @param cred 包含用户秘钥信息
* @param bucketName bucket名
* @param fileid 特定资源(会验证与当前操作的文件路径是否一致)
* @param expired 超时时间
* @return 返回base64编码的字符串
* @throws AbstractImageException 异常
*/
public static String appSign(Credentials cred, String bucketName, String fileid, long expired) throws AbstractImageException {
return generateSignature(cred, bucketName, fileid, expired);
}

private static String generateSignature(Credentials cred, String bucketName, String fileid, long expired) throws AbstractImageException {
int appId = cred.getAppId();
String secretId = cred.getSecretId();
String secretKey = cred.getSecretKey();
int rdm = Math.abs(new Random().nextInt());
long now = System.currentTimeMillis() / 1000;

String plainText = String.format("a=%d&b=%s&k=%s&t=%d&e=%d&r=%d&f=%s", appId, bucketName, secretId, now, now + expired, rdm, fileid);

byte[] hmacDigest;
try {
hmacDigest = CommonCodecUtils.HmacSha1(plainText, secretKey);
} catch (Exception e) {
throw new UnknownException(e.getMessage());
}
byte[] signContent = new byte[hmacDigest.length + plainText.getBytes().length];
System.arraycopy(hmacDigest, 0, signContent, 0, hmacDigest.length);
System.arraycopy(plainText.getBytes(), 0, signContent, hmacDigest.length, plainText.getBytes().length);

return CommonCodecUtils.Base64Encode(signContent);
}
}