package com.friday.custody.service.trustService.haitong.utils;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;

/**
 * @author zhangxiang
 * @date
 */
public class SignatureUtil {

    private static final String ALGORITHM = "SHA-256";

    public static String sha256(String data, String salt) throws Exception {
        return sha256(data + salt);
    }

	//签名示例
	public static void main(String[] args) throws Exception {
        //appSecret
        String appSecret = "edf51c55754163cd7ea22e15add4209149d1ff25f51c75cd94d2572cbc5313a7";
        //签名拼接字符串
        String sigStr = "clientid=RmiFLQO8&bizParam=xxx&businessLicense=123456789&timeStamp=1639719304482";
        //生成签名字符串
        String appSigned = SignatureUtil.sha256(sigStr,appSecret);

        System.out.println("生成后签名：" + appSigned);
    }

    private static String sha256(String str) throws Exception {
        MessageDigest messageDigest;
        messageDigest = MessageDigest.getInstance(ALGORITHM);
        messageDigest.update(str.getBytes(StandardCharsets.UTF_8));
        return byte2Hex(messageDigest.digest());
    }

    private static String byte2Hex(byte[] bytes) {
        StringBuffer stringBuffer = new StringBuffer();
        String temp = null;
        for (int i = 0; i < bytes.length; i++) {
            temp = Integer.toHexString(bytes[i] & 0xFF);
            if (temp.length() == 1) {
                stringBuffer.append("0");
            }
            stringBuffer.append(temp);
        }
        return stringBuffer.toString();
    }
}
