首先摆上工具类。
public class SortAndEncryptUtils {
public static String sortAndEncrypt(String appSecret, String timestamp, String nonce) {
ArrayList<String> list = new ArrayList<String>();
list.add(appSecret);
list.add(timestamp);
list.add(nonce);
Collections.sort(list);
return DigestUtils.shaHex(list.get(0) + list.get(1) + list.get(2));
}
}
再提供上DigestUtils的jar包:免费下载通道
摆上一段用法 :String appSecret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; //appSecret
String timestamp = String.valueOf(System.currentTimeMillis() / 1000); //获取系统时间的10位时间戳
String nonce = ""; //随机数
//得到一个三位的随机数
for (int i = 0; i < 3; i++) { nonce += String.valueOf((int) (Math.random() * 10)); }
//得到字典排序并且SHA1加密后的signature。
String signature = SortAndEncryptUtils.sortAndEncrypt(appSecret, timestamp, nonce);
String appSecret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; //appSecret
String timestamp = String.valueOf(System.currentTimeMillis() / 1000); //获取系统时间的10位时间戳
String nonce = ""; //随机数
//得到一个三位的随机数
for (int i = 0; i < 3; i++) { nonce += String.valueOf((int) (Math.random() * 10)); }
//得到字典排序并且SHA1加密后的signature。
String signature = SortAndEncryptUtils.sortAndEncrypt(appSecret, timestamp, nonce);
如有问题请多指正,您的指正使我更正确的前行.