package com.hr.util;

import com.google.common.base.Charsets;
import com.google.common.hash.Hashing;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 腾讯云认证签名
 */
public class SignatureUtil {


    public static String generateSign2(String appId, String userId, String nonce, String version, String ticket) {
        List<String> params = new ArrayList<>();
        params.add(version);
        params.add(appId);
        params.add(nonce);
        params.add(userId);
        // 使用SHA-1算法进行编码
        return sign2(params, ticket);
    }


    public static String sign2(List<String> values, String ticket) { //values传ticket外的其他参数
        if (values == null) {
            throw new NullPointerException("values is null");
        }
        values.removeAll(Collections.singleton(null));// remove null
        values.add(ticket);
        Collections.sort(values);

        StringBuilder sb = new StringBuilder();
        for (String s : values) {
            sb.append(s);
        }
        return Hashing.sha1().hashString(sb, Charsets.UTF_8).toString().toUpperCase();
    }


    public static String generateSignFaceResult(String WBappid, String orderNo, String version, String ticket, String nonce) {
        List<String> params = new ArrayList<>();
        params.add(WBappid);
        params.add(orderNo);
        params.add(version);
        params.add(nonce);
        // 使用SHA-1算法进行编码
        return sign2(params, ticket);
    }



    //    version appId  ticket nonce userId
    public static void main(String[] args) {
        //人脸认证前置参数 加签算法
//        String appId = "TIDAwSF9";
//        String userId = "435913477533760";
//        String nonce = "kHoSxvLZGxSoFsjxlbzEoUzh5PAnTU7T";
//        String version = "1.0.0";
//        String ticket = "xxjB6gWMQvLJaWsFu5VPkvcUuTAH4CWoRKKzxAEY9eDAiebn3D70DQg19B1ZaGSk";
//
//        String sign = generateSign2(appId, userId, nonce, version, ticket);
//        System.out.println("Sign: " + sign);

        //人脸认证 获取结果 加签算法
        String WBappid = "TIDAwSF9";
        String orderNo = "bde9369935d9416e9f93323e8e8c6814";
        String version = "1.0.0";
        String ticket = "xxjB6gWMQvLJaWsFu5VPkvcUuTAH4CWoRKKzxAEY9eDAiebn3D70DQg19B1ZaGSk";
        String nonce = "kHoSxvLZGxSoFsjxlbzEoUzh5PAnTU7T";

        System.out.println(generateSignFaceResult(WBappid,orderNo,version,ticket,nonce));
    }
}