package com.qs.mp.handler.auth; import com.qs.mp.common.utils.sign.Md5Utils; import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; /** * 签名工具类 * @author duota * */ public class SignUtils { protected static Logger logger = LoggerFactory.getLogger(SignUtils.class); @Value("${sign.md5key}") private static String md5key = "3Jr8S1K18rcC1wAfv8"; public static String md5(String string) { byte[] hash; try { hash = MessageDigest.getInstance("MD5").digest(string.getBytes("UTF-8")); } catch (UnsupportedEncodingException e) { throw new RuntimeException("UTF-8 is unsupported", e); } catch (NoSuchAlgorithmException e) { throw new RuntimeException("MessageDigest不支持MD5Util", e); } StringBuilder hex = new StringBuilder(hash.length * 2); for (byte b : hash) { if ((b & 0xFF) < 0x10) hex.append("0"); hex.append(Integer.toHexString(b & 0xFF)); } return hex.toString(); } public static boolean verifySign(String sign,String body,String timestamp) { String l = body+ "nonce"+ timestamp + md5key; logger.debug("sign str:"+l); String mySign = Md5Utils.hash(l); logger.debug("mySign:"+mySign+" sign:"+sign); return mySign.equals(sign); } }