码迷,mamicode.com
首页 > 编程语言 > 详细

KONGA下的HAMC插件功能 --JAVA代码实现

时间:2019-12-18 10:52:25      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:exce   sha   strong   ring   EDA   指定   led   mic   dad   

 

设置HAMC插件


技术图片

 

 

 

postman模拟请发发送:

技术图片

 

 

 Java代码:

 

HMAC-SHA-256工具类

 

 1 import java.security.InvalidKeyException;
 2 import java.security.NoSuchAlgorithmException;
 3 import java.util.Formatter;
 4 import javax.crypto.Mac;
 5 import javax.crypto.spec.SecretKeySpec;
 6 
 7 public class HmacSha256Util {
 8 
 9     private static final String HMAC_SHA1_ALGORITHM = "HmacSHA256";
10     private static String toHexString(byte[] bytes) {
11         Formatter formatter = new Formatter();
12         for (byte b : bytes) {
13             formatter.format("%02x", b);
14         }
15         return formatter.toString();
16     }
17  
18     public static String signature(String data, String key) throws NoSuchAlgorithmException, InvalidKeyException {
19         SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), HMAC_SHA1_ALGORITHM);
20         Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
21         mac.init(signingKey);
22         return toHexString(mac.doFinal(data.getBytes()));
23     }
24  
25     public static byte[] signatureReturnBytes(String data, String key) throws NoSuchAlgorithmException, InvalidKeyException {
26         SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), HMAC_SHA1_ALGORITHM);
27         Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
28         mac.init(signingKey);
29         return mac.doFinal(data.getBytes());
30     }
31 
32 }

 

SHA-256 工具方法:

public static String SHA256(String param) {
        if (StringUtils.isBlank(param)) {
            throw new IllegalArgumentException("param can not be null");
        }
        try {
            byte[] bytes = param.getBytes("utf-8");
            final MessageDigest md = MessageDigest.getInstance("SHA-256");
            md.reset();
            md.update(bytes);
            final Base64 base64 = new Base64();
            final byte[] enbytes = base64.encode(md.digest());
            return new String(enbytes);
        } catch (final NoSuchAlgorithmException e) {
            throw new IllegalArgumentException("unknown algorithm SHA-256");
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }

测试:

 1 public class Test01 {
 2     
 3     @Test
 4     public void getSHA256() throws UnsupportedEncodingException, InvalidKeyException, NoSuchAlgorithmException{
 5          StringBuilder stb = new StringBuilder();
 6          String content = stb.append("Date: ").append(getTime()).append("\n").append("abc: ").append("123456").append("\n").append("Digest: SHA-256=").append(getBody()).toString();
 7          System.out.println("签名前的数据:"+content);
 8          String secret="kOI6ZHLGdgd2GB8osYZSl7QDCrHzAFg1";
 9          String signature2 = new String(Base64.getEncoder().encode(HmacSha256Util.signatureReturnBytes(content, secret)), "US-ASCII");    
10         System.out.println("显示指定编码[推荐] signature:"+signature2);
11        
12     }
13 public String getTime() {
14         Date d=new Date();
15         DateFormat format=new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
16         format.setTimeZone(TimeZone.getTimeZone("GMT"));
17         String hdate = format.format(d);
18         return hdate;
19     }
20     
21     
22     public String getBody() throws UnsupportedEncodingException {
23         String body = "{\"item\":[{\"address\":\"hpSg3NXvBAdaDcaeGQiPFF9tciW9BmcQv8Qm\"}]}";        
24         String sha2562 = Sha256Util.SHA256(body);
25         return sha2562;
26     }  
27     
28 }

技术图片

 

 最经过postman调试成功访问

技术图片

KONGA下的HAMC插件功能 --JAVA代码实现

标签:exce   sha   strong   ring   EDA   指定   led   mic   dad   

原文地址:https://www.cnblogs.com/vieta/p/12058357.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!