package com.crm.rely.backend.util;

import org.apache.commons.codec.binary.Base64;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;


/**
 * @program: cwg-my-base
 * @description:
 * @author: houn
 * @create: 2019-05-07 11:08
 */
public class HAMCSHA256Util {


    private static final String HMAC_SHA1_ALGORITHM = "HmacSHA256";

    public static String HMACSHA256(String data, String key) {
        try {
            Mac sha256_HMAC = Mac.getInstance(HMAC_SHA1_ALGORITHM);
            SecretKeySpec secretKey = new SecretKeySpec(key.getBytes("utf-8"), HMAC_SHA1_ALGORITHM);
            sha256_HMAC.init(secretKey);
            byte[] hash = sha256_HMAC.doFinal(data.getBytes("utf-8"));
            return Base64.encodeBase64String(hash);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        }

        return null;
    }

    public static String sha256_HMAC(String message, String secret) {
        return sha256_HMAC(message, secret.getBytes());
    }

    public static String sha256_HMAC(String message, byte[] secret) {
        String hash = "";
        try {
            Mac sha256_HMAC = Mac.getInstance(HMAC_SHA1_ALGORITHM);
            SecretKeySpec secret_key = new SecretKeySpec(secret, HMAC_SHA1_ALGORITHM);
            sha256_HMAC.init(secret_key);
            byte[] bytes = sha256_HMAC.doFinal(message.getBytes());
            StringBuilder sb = new StringBuilder();
            for (byte item : bytes) {
                sb.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1, 3));
            }
            hash = sb.toString();
        } catch (Exception e) {
            System.out.println("Error HmacSHA256 ===========" + e.getMessage());
        }
        return hash;
    }

    /**
     * 测试
     *
     * @param args
     */
    public static void main(String[] args) throws Exception {

//        String genHMAC = HMACSHA256("apiKey%3Dc8de0fbc398643a59043f561ec37c578%26signVersion%3D1%26timestamp%3D2021
//        -09-09T14%3A47%3A44",
//                "a2699ccbb4c442c2866a13b274924a99f0530f2b1a3b777eedbfcd11131d03c6");

//        String data = "2021-12-14T07:33:16.421542ZeyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9
//        .eyJ0b2tlbl90eXBlIjoicmVmcmVzaCIsImV4cCI6MTYzOTQ4ODc5NiwianRpIjoiNTEyNzdlMzJjYjVjNGFiN2JhYmEyY2M5OWYzNjNjOTMiLCJ1c2VyX2lkIjo0MjZ9.JhXN3R1F6jGhDrSbKbH0qoTMeB9N5nL45hc-3P3aFvQ";
//        String secretString = "pHXivvW5pA9Y58t3eK7QnuZVSTMqXyip";
//
//        String genHMAC = HMACSHA256(data, secretString);
//        String sha256_HMAC = sha256_HMAC(data, secretString);
////        System.out.println(genHMAC);
//        System.out.println(sha256_HMAC);


//        System.out.println(HMACSHA256("buy=7.2216&legal_currency_max_amount=50000.00&legal_currency_min_amount=100" +
//                        ".00&sell=7.10861&symbol=USDT&timestamp=1586487661938",
//                "02d82ba350d6f95e4a6005f9c17d5c0a7dc4c013ed09c4fd04971660f1d0d8c299"));
//

//        System.out.println(HMACSHA256("count=100&created_time=2024-04-25%2015%3A26%3A16&currency=USDT&legal_amount" +
//                        "=737.00000000&legal_currency=CNY&merchant_order_no=PD202404251527169849_66753851" +
//                        "&merchant_profit=0.00000000&mobile=null&nonce=1714030080312&pass_time=2024-04-25%2015%3A27" +
//                        "%3A51&platform_fee=0.60000000&side=BUY&status=COMPLETE",
//                "02ad904d8600b1f7d33823ba54b2f533983659c68c55da075f6d0948e4931625e9"));
//
//        System.out.println(HMACSHA256("count=100&created_time=2024-04-25%2015%3A26%3A16&currency=USDT" +
//                        "&merchant_order_no=PD202404251527169849_66753851&merchant_profit=0" +
//                        ".00000000&nonce=1714030080312&platform_fee=0.60000000&side=BUY&status=COMPLETE",
//                "02d82ba350d6f95e4a6005f9c17d5c0a7dc4c013ed09c4fd04971660f1d0d8c299"));

        System.out.println(HMACSHA256("count=100&created_time=2024-04-25%2015%3A26%3A16&currency=USDT&legal_amount" +
                        "=737.00000000&legal_currency=CNY&merchant_order_no=PD202404251527169849_66753851" +
                        "&merchant_profit=0.00000000&mobile=null&nonce=1714030080312&pass_time=2024-04-25%2015%3A27" +
                        "%3A51&platform_fee=0.60000000&side=BUY&status=COMPLETE",
                "0329a81f77955ec6a8b697e25d413c56fd71d084d7882280ff5ebdf3c1c6c67dc7"));
        //yJghhncCqfqhcDZw16G5BughLVRVvEbS8bo8NW3lKj4=
    }
}
