package com.huimv.manage.eartag.controller;

import com.huimv.manage.util.CodeUtil;
import com.huimv.manage.util.HexUtils;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import sun.misc.BASE64Decoder;

import javax.xml.bind.DatatypeConverter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.util.Base64;//注意：这是jdk8才有的
//import org.apache.commons.codec.binary.Base64;

/**
 * @Project : huimv.shiwan
 * @Package : com.huimv.biosafety.uface.controller
 * @Description : TODO
 * @Version : 1.0
 * @Author : ZhuoNing
 * @Create : 2020-12-25
 **/
@SpringBootTest
public class ByteTest {

    @Test
    public void ByteArrayToBinary(){
//        String byte1 = "paCJTEwGOANoAXgAYAKgAbADaAIgBXUFkh4iNEHek8Y=";
//        Byte by1 = new Byte("paCJTEwGOANoAXgAYAKgAbADaAIgBXUFkh4iNEHek8Y=");
//        System.out.println("二进制数据>>");
//        String str = System.Text.Encoding.ASCII.GetString ( "paCJTEwGOANoAXgAYAKgAbADaAIgBXUFkh4iNEHek8Y=" );
//        System.out.println("2进制："   + binary(by1, 2));

        String res = new String("paCJTEwGOANoAXgAYAKgAbADaAIgBXUFkh4iNEHek8Y=");
        System.out.println("res>>"+res);
    }

    @Test
    public void testBase64() throws UnsupportedEncodingException {
        String text = "jb7L12gAeAFYAXgBcALAAZgDAAAQAiinnZH+VIu9684=";
////        String text = "119";
//        byte[] asBytes = Base64.getDecoder().decode(text);
//        System.out.println(new String(asBytes, "utf-8")); //这是Base64编码前的原始数据！！！
//        int a=0;
//        for(Byte b:asBytes){
//            System.out.println((a++)+","+b);
//        }
    }

    @Test
    public void test2() throws Exception{
        String code_format = "UTF-8";
//        String sourceData = "这是Base64编码前的原始数据！！！";
        String sourceData = "a5a0894c4c063803680178006002a001b003680220057505921e223441de93c6";
//        String sourceData = "211011500094511";
        byte[] bytes = sourceData.getBytes(code_format);
        System.out.println("最初的数据:"+sourceData+",length="+sourceData.length());
        System.out.println("base64编码=YTVhMDg5NGM0YzA2MzgwMzY4MDE3ODAwNjAwMmEwMDFiMDAzNjgwMjIwMDU3NTA1OTIxZTIyMzQ0MWRlOTNjNg==,length=88");
        // 编码
        String asB64 = Base64.getEncoder().encodeToString(bytes);
//        String asB64 = Base64.encodeBase64String(bytes);
        System.out.println("base64编码:"+asB64+",length="+asB64.length()); //6L+Z5pivQmFzZTY057yW56CB5YmN55qE5Y6f5aeL5pWw5o2u77yB77yB77yB
        System.out.println("从接口获取:nboVwAgESANAAqgAUACoAagBQABwArUGQl1pe0Vtu9g=,length="+"5ahSd9wDqAFoA0gDKALIA6gBcAKwBiWGZ45Ego3T084=".length());
        // 解码
        byte[] asBytes = Base64.getDecoder().decode(asB64);
//        byte[] asBytes =Base64.decodeBase64(asB64);
        System.out.println("解码后数据="+new String(asBytes, code_format)); //这是Base64编码前的原始数据！！！
    }

    @Test
    public void test3() {
//        String hexStr = "e5";
//        String data = hexUtils.convertHexToString(hexStr);
//        System.out.println("data="+data);
//        String[] hex2=new String[32];
        String d1 = "";
        for(int a=0;a<16;a++){
//            hex2[a]="e5";
            d1+=hexUtils.convertHexToString("a5");
        }
//        System.out.println("d1="+d1+",length="+d1.length());
//        String encode = d1;
//        String str = "a5a0894c4c063803680178006002a001b003680220057505921e223441de93c6";
//        System.out.println("0="+str+" length="+str.length());
//        //BASE64编码与解码
//        String encode = DatatypeConverter.printBase64Binary(str.getBytes());
//        System.out.println("1="+encode+" length="+encode.length());
//
//        String encode2 = DatatypeConverter.printBase64Binary(encode.getBytes());
//        System.out.println("2="+encode2+" length="+encode2.length());
//
//        String encode3 = DatatypeConverter.printBase64Binary(encode2.getBytes());
//        System.out.println("3="+encode3+" length="+encode3.length());

//        String encode = "haQcTLQHKACAACADYAKoAegBUABoBwMy20ruynTJi94=";
//        System.out.println("base64="+encode);
//
//        //一次解码
//        byte[] decode1= DatatypeConverter.parseBase64Binary(encode);
//        String text1 = new String(decode1);
//        System.out.println("text1="+text1);
//
//        //二次解码
//        byte[] decode2= DatatypeConverter.parseBase64Binary(text1);
//        System.out.println("text2="+new String(decode2));

        //16进制编码与解码
//        String encode1 = DatatypeConverter.printHexBinary(str.getBytes());
//        System.out.println("3="+encode1);
//        byte[] decode1= DatatypeConverter.parseHexBinary(encode1);
//        System.out.println("4="+new String(decode1));
    }

    @Test
    public void test4() throws IOException {
//        String data = "a5a0894c4c063803680178006002a001b003680220057505921e223441de93c6";
//        BASE64Encoder encoder = new BASE64Encoder();
//        String e1 = encoder.encode(data.getBytes("UTF-8"));
//        System.out.println("e1="+e1);

        String e1 = "tbrxwmAGGAFAADACIADQA+ADSADwBQCGfzde9xKKm9Q=";
        BASE64Decoder decode = new BASE64Decoder();
        byte[] d1 = decode.decodeBuffer(e1);
        System.out.println("d1="+new String(d1));
        String hexStr = hexUtils.parseByte2HexStr(d1);
        System.out.println("二进制转成="+hexStr.toLowerCase()+" length="+hexStr.length());
        String sourceData = "b5baf1c260061801400030022000d003e0034800f00500867f375ef7128a9bd4";
        if(hexStr.trim().equalsIgnoreCase(sourceData)){
            System.out.println("相等");
        }else{
            System.out.println("不等");
        }

        byte[] d2 = decode.decodeBuffer(new String(d1));
        System.out.println("d2="+new String(d2));
        //+++++++++++++++++++++++++++++++++++++++++++++++++++++
        String hexStr1 = hexUtils.parseByte2HexStr(d2);
        System.out.println("二次解码hexStr1="+hexStr1);
    }

    @Test
    public void test42() throws IOException {
//        String data = "a5a0894c4c063803680178006002a001b003680220057505921e223441de93c6";
//        BASE64Encoder encoder = new BASE64Encoder();
//        String e1 = encoder.encode(data.getBytes("UTF-8"));
//        System.out.println("e1="+e1);

        String e1 = "jb7L12gAeAFYAXgBcALAAZgDAAAQAiinnZH+VIu9684=";
        BASE64Decoder decode = new BASE64Decoder();
        byte[] d1 = decode.decodeBuffer(e1);
        System.out.println("d1="+new String(d1));
        String hexStr = hexUtils.parseByte2HexStr(d1);
        System.out.println("二进制转成="+hexStr.toLowerCase()+" length="+hexStr.length());
        String sourceData = "b5baf1c260061801400030022000d003e0034800f00500867f375ef7128a9bd4";
        if(hexStr.trim().equalsIgnoreCase(sourceData)){
            System.out.println("相等");
        }else{
            System.out.println("不等");
        }

        byte[] d2 = decode.decodeBuffer(new String(d1));
        System.out.println("d2="+new String(d2));
        //+++++++++++++++++++++++++++++++++++++++++++++++++++++
        String hexStr1 = hexUtils.parseByte2HexStr(d2);
        System.out.println("二次解码hexStr1="+hexStr1);
    }
    @Autowired
    private CodeUtil codeUtil;

    @Test
    public void test42_1() throws IOException {
        String encode = "tbrxwmAGGAFAADACIADQA+ADSADwBQCGfzde9xKKm9Q=";
        System.out.println("加密密文:"+encode);
        String hexData = codeUtil.convertBase64BinaryToHex(encode);
        System.out.println("解密明文:"+hexData);

//        BASE64Decoder decode = new BASE64Decoder();
//        byte[] d1 = decode.decodeBuffer(e1);
//        System.out.println("d1="+new String(d1));
//        String hexStr = hexUtils.parseByte2HexStr(d1);
//        System.out.println("二进制转成="+hexStr.toLowerCase()+" length="+hexStr.length());
        String sourceData = "b5baf1c260061801400030022000d003e0034800f00500867f375ef7128a9bd4";
        System.out.println("原始数据:"+sourceData);
        if(hexData.trim().equalsIgnoreCase(sourceData)){
            System.out.println("相等");
        }else{
            System.out.println("不等");
        }

//        byte[] d2 = decode.decodeBuffer(new String(d1));
//        System.out.println("d2="+new String(d2));
//        //+++++++++++++++++++++++++++++++++++++++++++++++++++++
//        String hexStr1 = hexUtils.parseByte2HexStr(d2);
//        System.out.println("二次解码hexStr1="+hexStr1);
    }

    @Test
    public void test41() throws IOException {
        String base64encodedString = "tbrxwmAGGAFAADACIADQA+ADSADwBQCGfzde9xKKm9Q=";
        // 解码
        byte[] base64decodedBytes = org.apache.commons.codec.binary.Base64.decodeBase64(base64encodedString);
        String data1= new String(base64decodedBytes, "utf-8");
        System.out.println("字符串1: " + data1);

        // 2次解码
        byte[] base64decodedBytes2 = org.apache.commons.codec.binary.Base64.decodeBase64(data1);
        System.out.println("字符串2: " + new String(base64decodedBytes2, "utf-8"));

    }



    public static String binary(byte[] bytes, int radix) {
        return new BigInteger(1, bytes).toString(radix);// 这里的1代表正数
    }

    @Autowired
    private HexUtils hexUtils;

    @Test
    public void hexUtils(){
        String data = "a5a0894c4c063803680178006002a001b003680220057505921e223441de93c6";

        // byte[] 转换为 16进制字符串
//        String hex = HexUtils.bytes2Hex(data.getBytes());
//        System.out.println(hex);                    // 输出: 48656c6c6f20576f726c64

        String hex = "vbYmrvwFiAAwAmADYALYAdgDMAIoB0OSODyqJceg88A=";
        // 16进制字符串 转换为 byte[]
        byte[] bytes = HexUtils.hex2Bytes(hex);
        System.out.println(new String(bytes));      // 输出: Hello World
    }

    @Test
    public void parse1(){
        String base64_str = "MDQw";
        System.out.println("base64:" + base64_str);
        String hex = DatatypeConverter.printHexBinary(DatatypeConverter.parseBase64Binary(base64_str));

        for (int i = 0; i < hex.length(); i+=6) {
            String bytes = hex.substring(i, i+6);

            System.out.println("hex: " + bytes);

            StringBuilder binary = new StringBuilder();

            int byte3_int = Integer.parseInt(bytes.substring(4, 6), 16);
            String byte3_str = Integer.toBinaryString(byte3_int);
            byte3_int = Integer.valueOf(byte3_str);
            binary.append(String.format("%08d", byte3_int));

            int byte2_int = Integer.parseInt(bytes.substring(2, 4), 16);
            String byte2_str = Integer.toBinaryString(byte2_int);
            byte2_int = Integer.valueOf(byte2_str);
            binary.append(String.format("%08d", byte2_int));

            int byte1_int = Integer.parseInt(bytes.substring(0, 2), 16);
            String byte1_str = Integer.toBinaryString(byte1_int);
            byte1_int = Integer.valueOf(byte1_str);
            binary.append(String.format("%08d", byte1_int));

            System.out.println("binary: " + binary);
        }
    }

    @Test
    public void testParseHexStr2Byte(){
        String hexString = "a5a0894c4c063803680178006002a001b003680220057505921e223441de93c6";
        byte[] b2 = hexUtils.parseHexStr2Byte(hexString);
        System.out.println(">>"+new String(b2));
        System.out.println("b2.length="+b2.length);
        for(int a=0;a<b2.length;a++){
            System.out.println(""+b2[a]);
        }
//        a5a0894c4c063803680178006002a001b003680220057505921e223441de93c6
        byte[] bytes2={65,66,67,68};
        System.out.println(new String(bytes2));
    }

    @Test
    public void test1() throws UnsupportedEncodingException {
        byte[] arrB = "a5a0894c4c063803680178006002a001b003680220057505921e223441de93c6".getBytes("UTF-8");
        int a=0;
        for (byte b : arrB) {
            a++;
            System.out.println(a+" >>"+b);
        }

//        String asB64 = Base64.getEncoder().encodeToString(arrB);
//        System.out.println("base64编码:"+asB64+",length="+asB64.length());
        String b2 = hexUtils.byteArrToBinStr(arrB);
        System.out.println(b2);
        System.out.println("b2.length>>"+b2.length());
        System.out.println("");
        // 两个16进制字符表示一个字节长度
        // 因为一个字节等于8bit（bit = 二进制位）
        // 一个16进制字符 需要 4个二进制才能表示
        // 所以两个16进制字符等于一个字节
        //
        int len = arrB.length;
        byte[] arrOut = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            // 上面得到的字节数组中，一个字符是一个长度，两个长度作为一个字符
            String s = new String(arrB, i, 2, "UTF-8");
            System.out.println((i+1)+","+s);
            arrOut[i / 2] = (byte) Integer.parseInt(s, 16);
        }
//        System.out.println((byte)Integer.parseInt("D0", 16));
        // System.out.println((byte)
    }

    @Test
    public void test2a() throws UnsupportedEncodingException {
//        int[] hex=new int[32];
//        for(int a=0;a<32;a++){
//            hex[a]=0xe5;
//        }
//        System.out.println("hex.length="+hex.length);
//        for(int a=0;a<64;a++){
//            System.out.println((a+1)+" "+hex[a]);
//            byte[] bytes = String.valueOf(hex[a]).getBytes("UTF-8");
//            // 编码
//            String asB64 = Base64.getEncoder().encodeToString(bytes);
//            System.out.println("asB64="+asB64);
//            System.out.println("");
//        }
//a5a0894c4c063803680178006002a001b003680220057505921e223441de93c6
//        a5a0 894c 4c06 3803 6801 7800 6002 a001 b003 6802 2005 7505 921e 2234 41de 93c6
    String hexStr = "e5";
    String data = hexUtils.convertHexToString(hexStr);
        System.out.println("data="+data);
//        String[] hex2=new String[32];
        String d1 = "";
        for(int a=0;a<16;a++){
//            hex2[a]="e5";
            d1+=hexUtils.convertHexToString("a5");
        }
        System.out.println("d1="+d1+",length="+d1.length());
        byte[] bytes = d1.getBytes("UTF-8");
//        String asB64 = Base64.getEncoder().encodeToString(bytes);
//            System.out.println("asB64="+asB64+",length="+asB64.length());
//xyjMKNqDwjUUxmWHzgdXirQEhophUQccNiiLZTUFudpBA
//Dh4ceEBFAANPSyh2WssVvWvu0mKllS1gzAusgzc8gL4
    }

    @Test
    public void test5(){
        String hello = "haQcTLQHKACAACADYAKoAegBUABoBwMy20ruynTJi94=";

//        byte[] decoded = Base64.decodeBase64(hello.getBytes());
//
//        System.out.println(Arrays.toString(decoded));
    }

    @Test
    public void test6() throws UnsupportedEncodingException {
//        String data = "haQcTLQHKACAACADYAKoAegBUABoBwMy20ruynTJi94=";
//        String base64encodedString = Base64.getEncoder().encodeToString(data.getBytes("utf-8"));
//        System.out.println("Base64 编码字符串 (基本) :" + base64encodedString);

        String base64encodedString = "haQcTLQHKACAACADYAKoAegBUABoBwMy20ruynTJi94=";
        // 解码
        byte[] base64decodedBytes = Base64.getDecoder().decode(base64encodedString);
        System.out.println("原始字符串: " + new String(base64decodedBytes, "utf-8"));
        base64encodedString = Base64.getUrlEncoder().encodeToString("runoob?java8".getBytes("utf-8"));
        System.out.println("Base64 编码字符串 (URL) :" + base64encodedString);
    }
}
