前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >对象存储签名生成

对象存储签名生成

原创
作者头像
_12291_721
发布2021-04-27 17:08:00
3.6K0
发布2021-04-27 17:08:00
举报
文章被收录于专栏:官网API实践
代码语言:java
复制
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;


public class Authorization {
    private static final Charset UTF8 = StandardCharsets.UTF_8;;
    private static final String SecretId="";
    private static final String SecretKey="";
    private static final String StartTimestamp = Long.toString(System.currentTimeMillis()/1000L);
    private static final String EndTimestamp = Long.toString(System.currentTimeMillis()/1000L + 30);
    private static final String KeyTime= StartTimestamp + ";" + EndTimestamp;
    public static String HmacSHA1(String key, String msg) throws Exception {
       byte[] data = key.getBytes(UTF8);
       Mac mac = Mac.getInstance("HmacSHA1");
       SecretKeySpec secretKeySpec = new SecretKeySpec(data, mac.getAlgorithm());
       mac.init(secretKeySpec);
       byte[] HmacSha1 = mac.doFinal(msg.getBytes(UTF8));
       StringBuilder stringBuilder = new StringBuilder("");
        for (int i = 0; i < HmacSha1.length; i++) {
            int v = HmacSha1[i] & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        return stringBuilder.toString();
    }
    public static String HttpString(String HttpMethod, String UriPathname, String HttpParameters, String HttpHeaders)throws Exception{
        return HttpMethod + "\n" + UriPathname + "\n" + HttpParameters + "\n" + HttpHeaders + "\n";
    }
    public static String SHA1(String StringToSign) throws Exception{
        char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
        MessageDigest messageDigest = MessageDigest.getInstance("SHA1");
        messageDigest.update(StringToSign.getBytes(UTF8));
        byte[] md = messageDigest.digest();
        int j = md.length;
        char buf[] = new char[j * 2];
        int k = 0;
        for (int i = 0; i < j; i++) {
            byte byte0 = md[i];
            buf[k++] = hexDigits[byte0 >>> 4 & 0xf];
            buf[k++] = hexDigits[byte0 & 0xf];
        }
        return new String(buf);
    }
    public static String Sign(String HttpMethod, String UriPathname) throws Exception{
        String SignKey =  HmacSHA1(SecretKey, KeyTime);
        String HttpStrings = HttpString(HttpMethod, UriPathname, "", "");
        String StringToSign = "sha1\n"+KeyTime+"\n"+SHA1(HttpStrings)+"\n";
        String Signature = HmacSHA1(SignKey, StringToSign);
        String Authorization = String.format("q-sign-algorithm=%s&q-ak=%s&q-sign-time=%s&q-key-time=%s&q-header-list=%s&q-url-param-list=%s&q-signature=%s", "sha1", SecretId, KeyTime, KeyTime,"","",Signature);
        return Authorization;
    }
}

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
云 API
云 API 是腾讯云开放生态的基石。通过云 API,只需少量的代码即可快速操作云产品;在熟练的情况下,使用云 API 完成一些频繁调用的功能可以极大提高效率;除此之外,通过 API 可以组合功能,实现更高级的功能,易于自动化, 易于远程调用, 兼容性强,对系统要求低。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档