package com.zjrs.ggfw.yjt.util;


import java.security.MessageDigest;
import java.util.Map;
import java.util.UUID;

public class SignUtil {
    /**
     * @param data 字符串信息
     * @return 将字符串进行 sha1 散列，得到长度为 40 的签名。
     * @throws Exception
     */
    public static String sha1(String data) throws Exception {
        MessageDigest md = MessageDigest.getInstance("SHA1");
        md.update(data.getBytes());
        StringBuilder buf = new StringBuilder();
        byte[] bits = md.digest();
        for (int bit : bits) {
            int a = bit;
            if (a < 0){
                a += 256;
            }
            if (a < 16){
                buf.append("0");
            }
            buf.append(Integer.toHexString(a));
        }
        return buf.toString();
    }
    public static String getUUID(){
        String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");
        return uuid;
    }
    public static String sign(Map map) throws Exception {
        String str="version="+map.get("version")+"&asktime="+map.get("asktime")+"&rnd=" +map.get("rnd")+
                "&userid="+map.get("userid");
        return sha1(str).toUpperCase();
    }
    public static String privateKeySign(YjtConfig config,Map map) throws Exception {
        String content=sign(map);
        return RSASignature.sign(content,config.getPrivateKey());
    }
}
