From 1868be01f915f7f849c2fac4dfcab0c3d34797a8 Mon Sep 17 00:00:00 2001 From: Wenjie Chen Date: Tue, 16 Jan 2018 21:59:53 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AD=97=E6=AE=B5=20r=20=E4=B8=BA=E5=BF=85?= =?UTF-8?q?=E5=A1=AB=EF=BC=9B=E5=AD=97=E6=AE=B5=20f=20=E8=99=BD=E4=B8=BA?= =?UTF-8?q?=E5=8F=AF=E9=80=89=EF=BC=8C=E4=BD=86=E7=94=9F=E6=88=90=E7=AD=BE?= =?UTF-8?q?=E5=90=8D=E6=97=B6=E4=B9=9F=E5=BF=85=E9=A1=BB=E5=8C=85=E5=90=AB?= =?UTF-8?q?=E8=BF=99=E4=B8=AA=E5=AD=97=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/qcloud/image/sign/Sign.java | 77 +++++++++++-------- 1 file changed, 47 insertions(+), 30 deletions(-) diff --git a/src/main/java/com/qcloud/image/sign/Sign.java b/src/main/java/com/qcloud/image/sign/Sign.java index 8fa3f30..6c9387c 100644 --- a/src/main/java/com/qcloud/image/sign/Sign.java +++ b/src/main/java/com/qcloud/image/sign/Sign.java @@ -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); + } }