Java http 加签(验签)工具类:采用SHA-1算法
什么是加签验签
加签验签,发送消息方,对消息加签名;接受消息方,验证签名是否正确。
为什么要做加签验签
做加签验签的目的主要目的就是,验证消息的完整性
如何做加签验签
简单来说, 发送消息方: 1、根据消息内容形成摘要 2、根据摘要形成签名字段 3、发送消息 接受消息方: 1、接受消息 2、根据消息内容形成摘要 3、根据摘要去验证签名是否正确
加签验签具体作用说明,可以自行搜索这里只是简单描述,而且加签验签的方式也有很多种,本文章仅供参考
加签规则
加签规则: 涉及参数: nonce:随机串; timestamp:时间戳; body:post请求的参数json串 secretKey:秘钥; (秘钥:a8f0eca7bb2511e8ada152543a77b4af 随机) signature:加签串;
参数的话可以根据场景需要,自己加一些,比如 xx渠道专用字段:privatefield: 111000 固定码
加签规则:nonce, timestamp,secretKey、body(post的参数json串)三个字段进行字典排序后,采用SHA-1算法获取并得到signature
具体代码(可以直接Ctrl C+V)
本工具使用采用SHA-1算法 ,仅供参考,简单方便,不要刨根问底(因为本人没研究过签名算法 ) 代码片 .
package com.xxxx.channel.util;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.security.DigestException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;
public class Sha1SignUtil {
public static String sign(Map<String,Object> maps) throws Exception{
String decrypt = getOrderByLexicographic(maps);
try {
MessageDigest digest = MessageDigest.getInstance("SHA-1");
digest.update(decrypt.getBytes());
byte[] messageDigest = digest.digest();
StringBuffer hexString = new StringBuffer();
for (int i = 0; i < messageDigest.length; i++) {
String shaHex = Integer.toHexString(messageDigest[i] & 0xFF);
if (shaHex.length() < 2) {
hexString.append(0);
}
hexString.append(shaHex);
}
return hexString.toString().toLowerCase();
} catch (NoSuchAlgorithmException e) {
throw new DigestException("签名错误!");
}
}
private static String getOrderByLexicographic(Map<String,Object> maps){
return splitParams(lexicographicOrder(getParamsName(maps)),maps);
}
private static List<String> getParamsName(Map<String,Object> maps){
List<String> values = new ArrayList<String>();
for(Map.Entry<String,Object> entry : maps.entrySet()){
if(entry.getValue() != null){
values.add(entry.getValue().toString());
}
}
return values;
}
private static List<String> lexicographicOrder(List<String> paramNames){
Collections.sort(paramNames);
return paramNames;
}
private static String splitParams(List<String> paramNames,Map<String,Object> maps){
StringBuilder paramStr = new StringBuilder();
for(String paramName : paramNames){
paramStr.append(String.valueOf(paramName));
}
return paramStr.toString();
}
public static void main(String[]args) throws Exception{
String nonce = RandomStringUtils.randomAlphanumeric(16);
String timestamp = String.valueOf(System.currentTimeMillis());
String secretKey="a8f0eca7bb2511e8ada152543a77b4af";
HashMap signMap = new HashMap<>( );
signMap.put("nonce", nonce);
signMap.put("timestamp", timestamp);
signMap.put("secretKey",secretKey);
HashMap mapWb = new HashMap();
mapWb.put("name","张三");
mapWb.put("age",24);
mapWb.put("phoneNumber", "13910010002");
String params=new Gson().toJson(mapWb);
signMap.put("data", params);
System.out.println( "==调用接口生成签名使用参数=={}"+new Gson().toJson(signMap));
String sign = Sha1SignUtil.sign(signMap);
System.out.println( "==生成签名==" + sign);
Boolean check = checkSign(mapWb,secretKey,nonce,timestamp,sign);
if(check){
System.out.println( "验证成功 sign:" + sign);
}
}
private static Boolean checkSign(Object body, String secretKey, String nonce, String timestamp, String sign) {
Gson gson = new GsonBuilder().disableHtmlEscaping().create();
String params= gson.toJson(body);
Map<String, Object> signMap = new HashMap<String, Object>();
signMap.put("nonce", nonce);
signMap.put("timestamp", timestamp);
signMap.put("secretKey", secretKey);
signMap.put("data",params);
String sign2 = null;
try {
sign2 = Sha1SignUtil.sign(signMap);
if (sign.equals(sign2)){
return true;
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
}
感谢查看,记得点赞哦 ~ ~
|