package cn.hmsoft.weixin.entity.request;

import java.math.BigInteger;
import java.security.MessageDigest;

/*****************************
 * 验证是否为可用的Web服务器
 * @author: Zhang XiaoQing
 * @date: 2016-08-13  18:02:41
 * @version: 2.0.0
 * @email: revisit@126.com
 * @Company: www.hmsoft.cn
 */
public class WxRequestEntity {
	private String token;
	private String signature;
	private String timestamp;
	private String nonce;
	private String echostr;

	public String getToken() {
		return token;
	}

	public void setToken(String token) {
		this.token = token;
	}

	public String getSignature() {
		return signature;
	}

	public void setSignature(String signature) {
		this.signature = signature;
	}

	public String getTimestamp() {
		return timestamp;
	}

	public void setTimestamp(String timestamp) {
		this.timestamp = timestamp;
	}

	public String getNonce() {
		return nonce;
	}

	public void setNonce(String nonce) {
		this.nonce = nonce;
	}

	public String getEchostr() {
		return echostr;
	}

	public void setEchostr(String echostr) {
		this.echostr = echostr;
	}
	public boolean verify() {
		String[] array = new String[] { token, timestamp, nonce };
		// 对参数进行排序
		java.util.Arrays.sort(array);
		String total = "";
		for (String string : array) {
			total = total + string;
		}
		// SHA-1加密实例
		MessageDigest sha1 = null;
		try {
			sha1 = MessageDigest.getInstance("SHA-1");
		} catch (Exception e) {
			return false;
		}
		sha1.update(total.getBytes());
		byte[] codedBytes = sha1.digest();
		// 将加密后的字节数组转换成字符串。
		String codedString = new BigInteger(1, codedBytes).toString(16);
		// 将加密的结果与请求参数中的signature比对，如果相同，原样返回echostr参数内容
		if (codedString.equals(signature))
			return true;
		return false;
	}
}
