package com.fkxpjj.mikoto.util;

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

import com.fkxpjj.mikoto.Mikoto;

public class Dev {
	public boolean validate(String signature, String timestamp, String nonce){
		String token = Mikoto.token;
		String[] arr = new String[] { token, timestamp, nonce };  
		Arrays.sort(arr);  
		StringBuilder content = new StringBuilder();  
		for (int i = 0; i < arr.length; i++) {  
			content.append(arr[i]);  
			}  
		MessageDigest md = null;  
		String tmpStr = null;  
		try {  
			md = MessageDigest.getInstance("SHA-1");  
			byte[] digest = md.digest(content.toString().getBytes());  
			tmpStr = byteToStr(digest);  
			} catch (NoSuchAlgorithmException e) {  
				e.printStackTrace();  
				}  
		content = null;  
		return tmpStr != null ? tmpStr.equals(signature.toUpperCase()) : false;  
		
	}
	
	private static String byteToStr(byte[] byteArray) {  
        String strDigest = "";  
        for (int i = 0; i < byteArray.length; i++) {  
            strDigest += byteToHexStr(byteArray[i]);  
        }  
        return strDigest;  
    }  
	
	private static String byteToHexStr(byte mByte) {  
        char[] Digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };  
        char[] tempArr = new char[2];  
        tempArr[0] = Digit[(mByte >>> 4) & 0X0F];  
        tempArr[1] = Digit[mByte & 0X0F];  
  
        String s = new String(tempArr);  
        return s;  
    }  
}

@@ -24,24 +24,82 @@
 to.util;%0D%0A%0D%0A
+import java.io.IOException;%0D%0Aimport java.io.PrintWriter;%0D%0A
 import java.
@@ -195,24 +195,121 @@
 .Arrays;%0D%0A%0D%0A
+import javax.servlet.http.HttpServletRequest;%0D%0Aimport javax.servlet.http.HttpServletResponse;%0D%0A%0D%0A
 import com.f
@@ -360,16 +360,23 @@
 %09public 
+static 
 boolean 
@@ -388,58 +388,277 @@
 ate(
-String signature, String timestamp, String nonce)%7B
+HttpServletRequest req, HttpServletResponse resp) throws IOException%7B%0D%0A%09%09String signature = req.getParameter(%22signature%22);%0D%0A%09%09String timestamp = req.getParameter(%22timestamp%22);%0D%0A%09%09String nonce = req.getParameter(%22nonce%22);%0D%0A%09%09String echostr = req.getParameter(%22echostr%22);
 %0D%0A%09%09
@@ -1222,22 +1222,29 @@
 l;  %0D%0A%09%09
-return
+boolean val =
  tmpStr 
@@ -1300,18 +1300,130 @@
 : false;
-  
+%0D%0A%09%09if(val) %7B%0D%0A%09%09%09PrintWriter out = resp.getWriter();%0D%0A%09%09%09out.print(echostr);%0D%0A%09%09%09out.close();%0D%0A%09%09%7D%0D%0A%09%09return val;
 %0D%0A%09%09%0D%0A%09%7D

change validate