Java SHA-256 签名生成 - ZNZW API 对接示例
import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.google.common.base.Charsets; import com.google.common.hash.HashFunction; import com.google.common.hash.Hashing;
/**
-
@ClassName SHA1SignForZNZW
-
@Description 生成 SHA-256 签名用于 ZNZW API 对接
-
@Author liweihy
-
@Date 2022/9/22 16:01 */ public class Sha1SignForZNZW {
private static HashFunction sha256 = Hashing.sha256();
/**
- 生成 SHA-256 签名
- @param str 待签名的字符串
- @return 生成的 SHA-256 签名
- @since JDK 1.6 */ public static String getSha256(String str) { return sha256.newHasher().putString(str, Charsets.UTF_8).hash().toString(); }
public static void main(String[] args) { // 对接和家亲 健全powkey 开发-测试环境 String powkey = 'hjqCore'; // 鉴权token 生产环境 // String token = 'RMg6srI0qFqKtaOKpVbOFz41G6HWsXnL'; /** * postman中header中的timeStamp填这个值 / long timeStamp = System.currentTimeMillis(); System.out.println('timeStamp:' + timeStamp); /* * 构造请求body数据,与postman中请求的body一致 */ JSONObject requestBody = new JSONObject(true); JSONArray deviceInfos = new JSONArray(); JSONObject deviceInfo = new JSONObject(true); deviceInfo.put('mac', '28:23:F5:36:CD:48'); deviceInfos.add(deviceInfo); deviceInfo = new JSONObject(true); deviceInfo.put('mac', '28:23:F5:36:CD:48'); deviceInfos.add(deviceInfo); requestBody.put('deviceInfos', deviceInfos); requestBody.put('mac', '28:23:F5:36:CD:48'); // 生成数据签名 postma // n中header中的signature填这个值 String sign; sign = getSha256(powkey + timeStamp + requestBody.toJSONString()); System.out.println('signature:' + sign); }
}
原文地址: https://www.cveoy.top/t/topic/opzP 著作权归作者所有。请勿转载和采集!