想簽名以下信息:
PUT /quotes/nelson HTTP/1.0
Content-Md5: c8fdb181845a4ca6b8fec737b3581d76
Content-Type: text/html
Date: Thu, 17 Nov 2005 18:49:58 GMT
X-OSS-Meta-Author: [email protected]
X-OSS-Magic: abracadabra
假如AccessID是: "44CF9590006BF252F707"
AccessKey 是 "OtxrzxIsfpFjA7SwPzILwy8Bw21TLhquhboDYROV",可用以下
方法簽名
簽名計算結果應該為”63mwfl zYIOG6k95yxbgMruQ6QI=”
- private String hmac_sha1(String key, String datas)
- {
- String reString = "";
-
- try
- {
- byte[] data = key.getBytes("UTF-8");
- //根據給定的字節數組構造一個密鑰,第二參數指定一個密鑰算法的名稱
- SecretKey secretKey = new SecretKeySpec(data, "HmacSHA1");
- //生成一個指定 Mac 算法 的 Mac 對象
- Mac mac = Mac.getInstance("HmacSHA1");
- //用給定密鑰初始化 Mac 對象
- mac.init(secretKey);
-
- byte[] text = datas.getBytes("UTF-8");
- //完成 Mac 操作
- byte[] text1 = mac.doFinal(text);
-
- reString = Base64.encodeToString(text1, Base64.DEFAULT);
-
- } catch (Exception e)
- {
- // TODO: handle exception
- }
-
- return reString;
- }