package com.qq.weixin.mp.aes;

import java.security.MessageDigest;
import java.util.Arrays;

/**
 * SHA1算法计算公众平台的消息签名接口
 *
 * @author Tencent
 * @since 2014/11/4
 */
public class SHA1 {

	/**
     * 用SHA1算法生成安全签名
     * @param params [token, timestamp, nonce, encrypt]
     * @return 安全签名
     * @throws com.qq.weixin.mp.aes.AesException
     */
        public static String calculate(String... params) throws AesException {
		try {
            String[] array = params;
			StringBuffer sb = new StringBuffer();
			// 字符串排序
			Arrays.sort(array);
			int len = params.length;
			for (int i = 0; i < len; i++) {
				sb.append(array[i]);
			}
			String str = sb.toString();
			// SHA1签名生成
			MessageDigest md = MessageDigest.getInstance("SHA-1");
			md.update(str.getBytes());
			byte[] digest = md.digest();

			StringBuffer hexstr = new StringBuffer();
			String shaHex = "";
			for (int i = 0; i < digest.length; i++) {
				shaHex = Integer.toHexString(digest[i] & 0xFF);
				if (shaHex.length() < 2) {
					hexstr.append(0);
				}
				hexstr.append(shaHex);
			}
			return hexstr.toString();
		} catch (Exception e) {
			throw new AesException(AesException.ComputeSignatureError);
		}
	}
}

@@ -84,16 +84,44 @@
 .Arrays;
+%0Aimport java.util.Formatter;
 %0A%0A/**%0A *
@@ -208,17 +208,20 @@
 SHA1 %7B%0A%0A
-%09
+    
 /**%0A    
@@ -233,24 +233,31 @@
 HA1%E7%AE%97%E6%B3%95%E7%94%9F%E6%88%90%E5%AE%89%E5%85%A8%E7%AD%BE%E5%90%8D%0A
+     *%0A
      * @para
@@ -377,20 +377,16 @@
      */%0A
-    
     publ
@@ -452,18 +452,24 @@
 ption %7B%0A
-%09%09
+        
 try %7B%0A  
@@ -503,19 +503,28 @@
 params;%0A
-%09%09%09
+            
 StringBu
@@ -557,23 +557,41 @@
 ();%0A
-%09%09%09// %E5%AD%97%E7%AC%A6%E4%B8%B2%E6%8E%92%E5%BA%8F%0A%09%09%09
+            // %E5%AD%97%E7%AC%A6%E4%B8%B2%E6%8E%92%E5%BA%8F%0A            
 Arra
@@ -606,19 +606,28 @@
 array);%0A
-%09%09%09
+            
 int len 
@@ -643,19 +643,28 @@
 length;%0A
-%09%09%09
+            
 for (int
@@ -687,20 +687,32 @@
  i++) %7B%0A
-%09%09%09%09
+                
 sb.appen
@@ -728,62 +728,59 @@
 %5D);%0A
-%09%09%09%7D%0A%09%09%09String str = sb.toString();%0A%09%09%09// SHA1%E7%AD%BE%E5%90%8D%E7%94%9F%E6%88%90%0A%09%09%09
+            %7D%0A%0A            // SHA1%E7%AD%BE%E5%90%8D%E7%94%9F%E6%88%90%0A            
 Mess
@@ -834,54 +834,134 @@
 %22);%0A
-%09%09%09md.update(str.getBytes());%0A%09%09%09
+            md.reset();%0A            md.update(new String(sb).getBytes(%22UTF-8%22));%0A%0A            // HEX%E8%BE%93%E5%87%BA%0A            
 byte%5B%5D 
-digest
+hash
  = m
@@ -976,297 +976,262 @@
 ();%0A
-%0A%09%09%09StringBuffer hexstr = new StringBuffer();%0A%09%09%09String shaHex = %22%22;%0A%09%09%09for (int i = 0; i %3C digest.length; i++) %7B%0A%09%09%09%09shaHex = Integ
+            Formatter formatter = new Formatter();%0A            for (byte b : hash) %7B%0A                formatter.format(%22%2502x%22, b);%0A            %7D%0A            String hex = formatt
 er.to
-Hex
 String(
-digest%5Bi%5D & 0xFF);%0A%09%09%09%09if (shaHex.length() %3C 2) %7B%0A%09%09%09%09%09hexstr.append(0);%0A%09%09%09%09%7D%0A%09%09%09%09hexstr.append(shaHex);%0A%09%09%09%7D%0A%09%09%09return hexstr.toString();%0A%09%09
+);%0A            formatter.close();%0A            return hex;%0A        
 %7D ca
@@ -1254,11 +1254,20 @@
 ) %7B%0A
-%09%09%09
+            
 thro
@@ -1326,13 +1326,22 @@
 r);%0A
-%09%09%7D%0A%09
+        %7D%0A    
 %7D%0A%7D%0A

更新SHA1签名算法