获取淘宝参数签名

 1 public static String getSign(String secret, Map<String, String> params) {
 2         if (secret == null || "".equals(secret) || params == null)
 3             throw new RuntimeException("secret or params is null or blank, please check...");
 4         StringBuilder sb = new StringBuilder();//.append(secret);
 5         String result = null;
 6         try {
 7             // Set<String> sortedKeys = new TreeSet<String>();
 8             String[] keys = params.keySet().toArray(new String[0]);
 9             Arrays.sort(keys);
10             // sortedKeys.addAll(keys);
11             for (String key : keys) {
12                 String KeyValue = params.get(key);
13                 sb.append(key).append(KeyValue);
14             }
15             byte[] bytes = encryptHMAC(sb.toString(), secret);
16             //MessageDigest md = MessageDigest.getInstance("MD5");
17             result = byteTohex(bytes);
18 
19         } catch (Exception e) {
20             throw new RuntimeException(e);
21         }
22         return result;
23     }
24 
25     private static byte[] encryptHMAC(String data, String secret)throws IOException {
26         byte[] bytes = null;
27         try {
28             SecretKey secretKey = new SecretKeySpec(secret.getBytes("UTF-8"),"HmacMD5");
29             Mac mac = Mac.getInstance(secretKey.getAlgorithm());
30             mac.init(secretKey);
31             bytes = mac.doFinal(data.getBytes("UTF-8"));
32         } catch (GeneralSecurityException e) {
33             e.printStackTrace();
34         }
35         return bytes;
36     }
37 
38     public static String byteTohex(byte[] b) {
39         StringBuffer sb = new StringBuffer();
40         String stmp = "";
41 
42         for (int n = 0; n < b.length; n++) {
43             stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
44 
45             if (stmp.length() == 1)
46                 sb.append("0").append(stmp);
47             else
48                 sb.append(stmp);
49         }
50 
51         return sb.toString().toUpperCase();
52     }

 

转载于:https://www.cnblogs.com/pyrebot/p/4911836.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值