SHA1签名工具类java
生活随笔
收集整理的這篇文章主要介紹了
SHA1签名工具类java
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
SHA1簽名工具類java package com.net.util;import java.security.MessageDigest;
import java.util.Iterator;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.SortedMap;/*
'============================================================================
'api說明:
'createSHA1Sign創建簽名SHA1
'getSha1()Sha1簽名
'============================================================================
'*/
public class Sha1Util {public static String getNonceStr() {Random random = new Random();return MD5Util.MD5Encode(String.valueOf(random.nextInt(10000)), "UTF-8");}public static String getTimeStamp() {return String.valueOf(System.currentTimeMillis() / 1000);}//創建簽名SHA1public static String createSHA1Sign(SortedMap<String, String> 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之前:" + params);
// System.out.println("SHA1簽名為:"+getSha1(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("UTF-8"));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;}}
}
?
posted on 2017-04-22 19:54 < /> 閱讀(...) 評論(...) 編輯 收藏轉載于:https://www.cnblogs.com/zhangdiIT/p/6748959.html
總結
以上是生活随笔為你收集整理的SHA1签名工具类java的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 电商5个流程的用户体验
- 下一篇: 巧用自媒体平台,让你事半功倍