com.firefly.wechat.utils.SHA1 Maven / Gradle / Ivy
package com.firefly.wechat.utils;
import com.firefly.utils.codec.HexUtils;
import java.security.MessageDigest;
import java.util.Arrays;
/**
* SHA1 class
*
* 计算公众平台的消息签名接口.
*/
public class SHA1 {
/**
* 用SHA1算法生成安全签名
*
* @param token 票据
* @param timestamp 时间戳
* @param nonce 随机字符串
* @param encrypt 密文
* @return 安全签名
* @throws AesException
*/
public static String getSHA1(String token, String timestamp, String nonce, String encrypt) throws AesException {
try {
String[] array = new String[]{token, timestamp, nonce, encrypt};
StringBuilder sb = new StringBuilder();
// 字符串排序
Arrays.sort(array);
for (int i = 0; i < 4; i++) {
sb.append(array[i]);
}
String str = sb.toString();
// SHA1签名生成
MessageDigest md = MessageDigest.getInstance("SHA-1");
md.update(str.getBytes());
byte[] digest = md.digest();
StringBuilder hex = new StringBuilder();
String shaHex;
for (byte aDigest : digest) {
shaHex = Integer.toHexString(aDigest & 0xFF);
if (shaHex.length() < 2) {
hex.append(0);
}
hex.append(shaHex);
}
return hex.toString();
} catch (Exception e) {
throw new AesException(AesException.ComputeSignatureError);
}
}
public static String getSHA1(String plainText, String sessionKey) {
try {
StringBuilder sb = new StringBuilder();
sb.append(plainText).append(sessionKey);
String str = sb.toString();
MessageDigest md = MessageDigest.getInstance("SHA-1");
md.update(str.getBytes());
byte[] digest = md.digest();
return HexUtils.bytesToHex(digest);
} catch (Exception e) {
throw new IllegalArgumentException(e);
}
}
public static void main(String[] args) {
String s = getSHA1("{\"nickName\":\"Band\",\"gender\":1,\"language\":\"zh_CN\",\"city\":\"Guangzhou\",\"province\":\"Guangdong\",\"country\":\"CN\",\"avatarUrl\":\"http://wx.qlogo.cn/mmopen/vi_32/1vZvI39NWFQ9XM4LtQpFrQJ1xlgZxx3w7bQxKARol6503Iuswjjn6nIGBiaycAjAtpujxyzYsrztuuICqIM5ibXQ/0\"}",
"HyVFkGl5F5OQWJZZaNzBBg==");
System.out.println(s);
System.out.println("75e81ceda165f4ffa64f4068af58c64b8f54b88c".equals(s));
}
}