package com.crm.rely.backend.util;

import java.security.MessageDigest;

/**
 * @Author: houn
 * @Date: 2020/11/11 16:35
 * @Description:
 */
public class ShaUtil {

    public final static String SHA = "SHA";
    public final static String SHA256 = "SHA-256";
    public final static String SHA384 = "SHA-384";
    public final static String SHA512 = "SHA-512";

    public static String sha1(String inStr) throws Exception {
        return sha(SHA, inStr);
    }

    public static String sha256(String inStr) throws Exception {
        return sha(SHA256, inStr);
    }

    public static String sha384(String inStr) throws Exception {
        return sha(SHA384, inStr);
    }


    public static String sha512(String inStr) throws Exception {
        return sha(SHA512, inStr);
    }

    public static String sha(String algorithm, String inStr) throws Exception {

        byte[] md5Bytes = SHAByte(algorithm, inStr);
        StringBuffer hexValue = new StringBuffer();
        for (int i = 0; i < md5Bytes.length; i++) {
            int val = ((int) md5Bytes[i]) & 0xff;
            if (val < 16) {
                hexValue.append("0");
            }
            hexValue.append(Integer.toHexString(val));
        }
        return hexValue.toString();
    }

    public static byte[] SHAByte(String algorithm, String inStr) throws Exception {
        MessageDigest sha = null;

        sha = MessageDigest.getInstance(algorithm);

        byte[] byteArray = inStr.getBytes("UTF-8");
        byte[] md5Bytes = sha.digest(byteArray);

        return md5Bytes;
    }

    public static void main(String[] args) throws Exception {
        //"signature"="Base64(SHA-256(requestId+merchantId+amount+currency))"
//        System.out.println(sha1("amount6.17351598appidfpQvw7wu76NjgW8yorderidPD202011142002171597_66703004rmb40
//        .56status125ed00a3106c308d0eb7c8905b719a5f"));
//        System.out.println(sha("SHA-512", "amount=18000&currency=RMB&customer=10001&merchant=ME2333&txcode" +
//                "=TX3888bipipaykey"));

//        System.out.println(sha256("123123"));


        /**
         * merchant_id
         * application_key
         * timestamp
         * intent
         * cid
         * order_id
         *
         *{
         *     "merchant_id": "API-cwgmarkets",
         *     "application_key": "cwgmarkets.com",
         *     "intent": "payment",
         *     "currency": "USD",
         *     "amount": 100,
         *     "cid": "1",
         *     "locale": "en-GB",
         *     "customer_token": null,
         *     "customer_data": {
         *         "country": "GB",
         *         "first_name": "John",
         *         "last_name": "Johnson",
         *         "dob": "12\/31\/1980",
         *         "email": "johnjohnson@example.com",
         *         "phone": "44201112222",
         *         "zip": "WC2N 5DU",
         *         "state": "JS",
         *         "city": "London",
         *         "address": "Random st., 12\/3"
         *     },
         *     "payment_method": null,
         *     "gateway": null,
         *     "validation_url": null,
         *     "notification_url": "https:\/\/api.merchant.com\/v1\/deposits\/tx-1560610955",
         *     "return_url": "https:\/\/merchant.com\/payment_result\/tx-1560610955",
         *     "order_id": "test-1560610955",
         *     "variable1": "your variable",
         *     "variable2": "if that is not enough, you can pass even one more variable",
         *     "variable3": null,
         *     "version": "1.3",
         *     "timestamp": 1590611635
         * }
         */
        String key = "I8K0MFtcLtOcPzuKZVV3RHnF4xnQM5XU";
        String content = "API-cwgmarketscgwmarkets.com1716519401payment700101test-1560610955" + key;
        System.out.println(sha384(content));
    }
}
