package com.m2pool.pool.utils;

import com.m2pool.common.core.utils.StringUtils;
import com.m2pool.pool.entity.TempMiner;
import org.apache.commons.codec.digest.Blake3;


/**
 * @Description TODO
 * @Date 2025/3/11 13:48
 * @Author 杜懿
 */
public class Demo2 {

    private final static char[] digits = "0123456789abcdef".toCharArray();

    public static void main(String[] args) {
        //Blake3 hasher = Blake3.initHash();
        String header ="0007b49c0e58baeb41210e9df4152cde2e55e1f4805e8e44c7ad3685b198197e9ce5639aad9be58f4278eb6c5323673c7b8050182f15f2f4cd7c1c18d9d0c2c482ba3297300d77dcc730e671441a2d625598da6ec549a719e7754112173afd3abe9f00000001fc048896d8cb49801427e6d000dc8b90b32aca0ab895b9ae473832600b7f0f2126cd66f9b64ad33db4d2349e698248ab9217dd2a3b3a8ec2f9d22f4189bc709c94bcc6b8c1fdaedd543a7a7ba5a2b8d68da2a3fd449d1595a55a2ac27c25bcd2019bf81da613a561fe098e49cc5b6cf02a2b743c6d2a1d441284af43af9176ba91d5509c9e3cb2b76f3993c88783a0d1f53e27a6619db3bbbaf2a991777df922b241da7b46a37824ee1fab5e71aece49e4c11048576ca28a9703987800000195fa8c66bf1d08f849";
        String nonce = "bb57294afe0218a928d9e4fd1856c53e578066c0";
        //byte[] hash = new byte[32];
        //hasher.update(hexStringToBytes(a.concat(b)));
        //hasher.doFinalize(hash);
        //System.out.println(hash.length);
        //String result1 = bytesToHexString(hash);
        //System.out.println(result1);
        //System.out.println(result1.length());
        //
        //hasher.reset();
        //byte[] hash2 = new byte[32];
        //hasher.update(hexStringToBytes(result1));
        //hasher.doFinalize(hash2);
        //String result2 = bytesToHexString(hash2);
        //System.out.println(result2);



        //Blake3Digest digest = new Blake3Digest();
        //byte[] dateBytes = hexStringToBytes(data);
        //digest.update(dateBytes,0,data.length());
        //byte[] hash2 = new byte[digest.getDigestSize()];
        //digest.doFinal(hash2,0);
        //System.out.println(hash2);
        //System.out.println(bytesToHex(hash2));
        hashHandle(header.concat(nonce));
    }

    private static String hashHandle(String in)     {
        //字符串判空
        if(StringUtils.isBlank(in)){
            return null;
        }
        Blake3 hasher = Blake3.initHash();
        byte[] hash = new byte[32];
        hasher.update(hexStringToBytes(in));
        hasher.doFinalize(hash);
        String result1 = bytesToHexString(hash);
        System.out.println(result1);

        hasher.reset();
        byte[] hash2 = new byte[32];
        hasher.update(hexStringToBytes(result1));
        hasher.doFinalize(hash2);
        String result2 = bytesToHexString(hash2);
        System.out.println(result2);
        return  result2;
    }


    /**
     * 高效写法 16进制字符串转成byte数组
     *
     * @param hex 16进制字符串，支持大小写
     * @return byte数组
     */
    public static byte[] hexStringToBytes(String hex) {
        byte[] result = new byte[hex.length() / 2];
        char[] chars = hex.toCharArray();
        for (int i = 0, j = 0; i < result.length; i++) {
            result[i] = (byte) (toByte(chars[j++]) << 4 | toByte(chars[j++]));
        }
        return result;
    }

    private static int toByte(char c) {
        if (c >= '0' && c <= '9') return (c - '0');
        if (c >= 'A' && c <= 'F') return (c - 'A' + 0x0A);
        if (c >= 'a' && c <= 'f') return (c - 'a' + 0x0a);
        throw new RuntimeException("invalid hex char '" + c + "'");
    }


    /**
     * 高效写法 byte数组转成16进制字符串
     *
     * @param bytes byte数组
     * @return 16进制字符串
     */
    public static String bytesToHexString(byte[] bytes) {
        char[] buf = new char[bytes.length * 2];
        int c = 0;
        for (byte b : bytes) {
            buf[c++] = digits[(b >> 4) & 0x0F];
            buf[c++] = digits[b & 0x0F];
        }
        return new String(buf);
    }

}
