想签名以下信息:
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: foo@bar.com
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; }