当前位置:   article > 正文

java http接口 加签,验签

java 实体类 加签

加签:

  1. Random random = new Random();
  2. int rannum = (int) (random.nextDouble() * (99999 - 10000 + 1)) + 10000;//5位随即数
  3. String nonce = rannum + "";
  4. String signature = HMACSHA1Util.getHmacSHA1(createtime+nonce, appKey);//appKey =公共密钥
  5. String smsUrl="http://"+sjyUrl+":"+sjyPort+"}/rest/sendMessage?appid="+appId+"&timestamp="+createtime+"&nonce="+nonce+"&signature="+signature;

解签

  1. String timestamp = map != null ? (String)map.get("timestamp") : null;//验签参数(时间戳)
  2. String nonce = map != null ? (String)map.get("nonce") : null;//验签参数(随机数)
  3. String sjySignature = map != null ? (String)map.get("signature") : null;//验签参数(签名)
  4. String mySignature=HMACSHA1Util.getHmacSHA1(timestamp + nonce, appKey);

工具类

  1. package com.paic.umap.ucm.common.utils;
  2. import java.io.UnsupportedEncodingException;
  3. import java.net.URLEncoder;
  4. import java.security.InvalidKeyException;
  5. import java.security.NoSuchAlgorithmException;
  6. import javax.crypto.Mac;
  7. import javax.crypto.spec.SecretKeySpec;
  8. import sun.misc.BASE64Encoder;
  9. public class HMACSHA1Util {
  10. /**
  11. * HMAC-SHA1签名
  12. *
  13. * @param message
  14. * @param key
  15. * @return
  16. */
  17. public static String getHmacSHA1(String message, String key) {
  18. String hmacSha1 = null;
  19. try {
  20. // url encode
  21. message = URLEncoder.encode(message, "UTF-8");
  22. // hmac-sha1加密
  23. Mac mac = Mac.getInstance("HmacSHA1");
  24. SecretKeySpec spec = new SecretKeySpec(key.getBytes(), "HmacSHA1");
  25. mac.init(spec);
  26. byte[] byteHMAC = mac.doFinal(message.getBytes());
  27. // base64 encode
  28. hmacSha1 = new BASE64Encoder().encode(byteHMAC);
  29. } catch (NoSuchAlgorithmException e) {
  30. } catch (InvalidKeyException e) {
  31. } catch (UnsupportedEncodingException e) {
  32. }
  33. return hmacSha1;
  34. }
  35. }

  

转载于:https://www.cnblogs.com/hailei/p/5238776.html

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/732656
推荐阅读
相关标签
  

闽ICP备14008679号