com.lorne.weixin.pay.util.Sha1Util Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of weixin-api Show documentation
Show all versions of weixin-api Show documentation
weixin-api project for Spring Project
package com.lorne.weixin.pay.util;
import com.lorne.core.framework.utils.encode.MD5Util;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.util.*;
/*
'============================================================================
'api说明:
'createSHA1Sign创建签名SHA1
'getSha1()Sha1签名
'============================================================================
'*/
public class Sha1Util {
public static String getNonceStr() {
Random random = new Random();
try {
return MD5Util.md5(String.valueOf(random.nextInt(10000)).getBytes("utf-8"));
} catch (UnsupportedEncodingException e) {
return null;
}
}
public static String getTimeStamp() {
return String.valueOf(System.currentTimeMillis() / 1000);
}
//创建签名SHA1
public static String createSHA1Sign(SortedMap signParams) throws Exception {
StringBuffer sb = new StringBuffer();
Set es = signParams.entrySet();
Iterator it = es.iterator();
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
String k = (String) entry.getKey();
String v = (String) entry.getValue();
sb.append(k + "=" + v + "&");
//要采用URLENCODER的原始值!
}
String params = sb.substring(0, sb.lastIndexOf("&"));
//System.out.println("sha1 sb:" + params);
return getSha1(params);
}
//Sha1签名
public static String getSha1(String str) {
if (str == null || str.length() == 0) {
return null;
}
char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f' };
try {
MessageDigest mdTemp = MessageDigest.getInstance("SHA1");
mdTemp.update(str.getBytes("GBK"));
byte[] md = mdTemp.digest();
int j = md.length;
char buf[] = new char[j * 2];
int k = 0;
for (int i = 0; i < j; i++) {
byte byte0 = md[i];
buf[k++] = hexDigits[byte0 >>> 4 & 0xf];
buf[k++] = hexDigits[byte0 & 0xf];
}
return new String(buf);
} catch (Exception e) {
return null;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy