Appearance
签名
签名代码示例
data
字段内容
java
// 生成签名字符串
private static String getSign(Map<String, String> data)
{
// 获取关键字列表
List<String> keys = new ArrayList<>(data.keySet());
// 关键字列表排序
keys.sort(Comparator.naturalOrder());
StringBuilder sb = new StringBuilder();
for (String key : keys)
{
// 取各个字段内容拼接字符串
sb.append(data.get(key));
}
// 加上双方约定随机字符串
String txt = sb.toString() + nonce;
// 计算哈希值
return getMD5(txt);
}
// md5加密
private static String getMD5(String password) {
MessageDigest md5 = null;
try {
md5 = MessageDigest.getInstance("MD5");
} catch (Exception e) {
throw new RuntimeException(e);
}
byte[] byteArray = password.getBytes(StandardCharsets.UTF_8);
byte[] md5Bytes = md5.digest(byteArray);
StringBuilder hexValue = new StringBuilder();
for (byte md5Byte : md5Bytes) {
int val = ((int) md5Byte) & 0xff;
if (val < 16) {
hexValue.append("0");
}
hexValue.append(Integer.toHexString(val));
}
return hexValue.toString();
}
php
function getSign(array $datas)
{
// 按关键字排序
ksort($datas);
$tmp = "";
foreach ($datas as $key => $value) {
// 取各个字段内容拼接字符串
$tmp .= $value;
}
// 加上双方约定随机字符串
global $nonce;
$tmp .= $nonce;
// 计算哈希值
return md5($tmp);
}
c#
private static string getSign(Dictionary<string, string> data)
{
// 获取关键字列表
List<string> keys = new List<string>();
foreach (string key in data.Keys)
{
keys.Add(key);
}
// 关键字列表排序
keys.Sort();
StringBuilder sb = new StringBuilder();
foreach (string key in keys)
{
// 取各个字段内容拼接字符串
sb.Append(data[key]);
}
// 加上双方约定随机字符串
string txt = sb.ToString() + nonce;
// 计算哈希值
return getMD5(txt);
}
// md5加密
private static string getMD5(string txt)
{
using (MD5 mi = MD5.Create())
{
byte[] buffer = Encoding.ASCII.GetBytes(txt);
//开始加密
byte[] newBuffer = mi.ComputeHash(buffer);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < newBuffer.Length; i++)
{
sb.Append(newBuffer[i].ToString("x2"));
}
return sb.ToString();
}
}
python
def getSign(data):
# 按关键字排序
items = sorted(data.items())
# 取各个字段内容拼接字符串,加上双方约定随机字符串
values = [str(v) for (k, v) in items]
strData = ''.join(values) + nonce
# 计算哈希值
return hashlib.md5(strData.encode(encoding='UTF-8')).hexdigest()
验签示例
java
private boolean checkSign(String response_content, String timestamp, String sign) {
// 随机字符串 后台获取
String nonce = "XOfX547SeCIlhufeeBBwgZINx";
String buf = response_content + timestamp + nonce;
String encode = getMD5(buf);
return encode.equals(sign);
}
private String getMD5(String password) {
MessageDigest md5 = null;
try {
md5 = MessageDigest.getInstance("MD5");
} catch (Exception e) {
throw new RuntimeException(e);
}
byte[] byteArray = password.getBytes(StandardCharsets.UTF_8);
byte[] md5Bytes = md5.digest(byteArray);
StringBuilder hexValue = new StringBuilder();
for (byte md5Byte : md5Bytes) {
int val = ((int) md5Byte) & 0xff;
if (val < 16) {
hexValue.append("0");
}
hexValue.append(Integer.toHexString(val));
}
return hexValue.toString();
}
php
// 验签
function checkSign($response_content, $timestamp, $sign) {
// 随机字符串 后台获取
$nonce = "XOfX547SeCIlhufeeBBwgZIN";
$buf = $response_content.$timestamp.$nonce;
$encode = md5($buf);
return $encode == $sign;
}
c#
private static bool checkSign(string response_content, string timestamp, string sign)
{
// 随机字符串 后台获取
string nonce = "XOfX547SeCIlhufeeBBwgZINx";
string buf = response_content + timestamp + nonce;
string encode = getMD5(buf);
return encode.Equals(sign);
}
private static string getMD5(string txt)
{
using (MD5 mi = MD5.Create())
{
byte[] buffer = Encoding.ASCII.GetBytes(txt);
//开始加密
byte[] newBuffer = mi.ComputeHash(buffer);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < newBuffer.Length; i++)
{
sb.Append(newBuffer[i].ToString("x2"));
}
return sb.ToString();
}
}
python
# 验签
def checkSign(response_content, timestamp, sign):
# 随机字符串 后台获取
nonce = "XOfX547SeCIlhufeeBBwgZIN"
buf = response_content + timestamp + nonce
encode = hashlib.md5(buf.encode(encoding='UTF-8')).hexdigest()
return encode == sign