package com.example.demo.test;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.ChineseDate;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.watch.WatchMonitor;
import cn.hutool.core.io.watch.Watcher;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.lang.Console;
import cn.hutool.core.util.*;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.asymmetric.KeyType;
import cn.hutool.crypto.asymmetric.RSA;
import com.example.demo.domain.ExporTexcel;
import org.jsoup.helper.StringUtil;

import java.io.File;
import java.lang.reflect.Method;
import java.nio.file.Path;
import java.nio.file.WatchEvent;
import java.security.KeyPair;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.ArrayList;


/**
 * @Project : huimv.shiwan
 * @Package : com.example.demo.test
 * @Description : TODO
 * @Author : yuxuexuan
 * @Create : 2021/2/4 0004 14:11
 **/
public class HuToolUtil {

//    public static void main(String[] args) {
//       /* String a = "2017-05-06";
//        Date value = Convert.toDate(a);
//        System.out.println(value);*/
//     /*   String a = "我是一个小小的可爱的字符串";
//        String hex = Convert.toHex(a, CharsetUtil.CHARSET_UTF_8);
//        System.out.println(hex);
//
//        String raw = Convert.hexToStr("e68891e698af",CharsetUtil.CHARSET_UTF_8);
//        System.out.println(raw);*/
//        /*long a = 75L;
//        long minutes = Convert.convertTime(a, TimeUnit.HOURS,TimeUnit.DAYS);
//        System.out.println(minutes);*/
//
//        /*DateTime dateTime = DateUtil.parseDate("2020-08-28");
//        System.out.println(dateTime);
//
//        ChineseDate chineseDate = new ChineseDate(DateUtil.parseDate("2020-08-28"));
//        System.out.println(chineseDate);*/
//
      /*  String content = "ZZZaaabbbccc中文1234";
        System.out.println(ReUtil.getFirstNumber(content));
        Method method = ReflectUtil.getMethod(ExporTexcel.class,"getId");
        System.out.println(method);
//
//
//
//        File file = FileUtil.file("example.properties");
////这里只监听文件或目录的修改事件
//        WatchMonitor watchMonitor = WatchMonitor.create(file, WatchMonitor.ENTRY_MODIFY);
//        watchMonitor.setWatcher(new Watcher(){
//            @Override
//            public void onCreate(WatchEvent<?> event, Path currentPath) {
//                Object obj = event.context();
//                Console.log("创建：{}-> {}", currentPath, obj);
//            }
//
//            @Override
//            public void onModify(WatchEvent<?> event, Path currentPath) {
//                Object obj = event.context();
//                Console.log("修改：{}-> {}", currentPath, obj);
//            }
//
//            @Override
//            public void onDelete(WatchEvent<?> event, Path currentPath) {
//                Object obj = event.context();
//                Console.log("删除：{}-> {}", currentPath, obj);
//            }
//
//            @Override
//            public void onOverflow(WatchEvent<?> event, Path currentPath) {
//                Object obj = event.context();
//                Console.log("Overflow：{}-> {}", currentPath, obj);
//            }
//        });
//
////设置监听目录的最大深入，目录层级大于制定层级的变更将不被监听，默认只监听当前层级目录
//        watchMonitor.setMaxDepth(3);
////启动监听
//        watchMonitor.start();*/
//
//
//
//
//    }

    public static void main(String[] args) {

      count(1);
    }
    public static void count(int x){

        int random=(int)((Math.random()*9+1)*100000);
        long timestamp=System.currentTimeMillis();
        String str = random+";"+timestamp;
        System.out.println("原文："+str);


        RSA rsa = new RSA();
        String privateKeyBase64 = rsa.getPrivateKeyBase64();
        String publicKeyBase64 = rsa.getPublicKeyBase64();
        System.out.println("私钥:"+privateKeyBase64);
        System.out.println("公钥:"+publicKeyBase64);

        byte[] encrypt = rsa.encrypt(StrUtil.bytes(str, CharsetUtil.CHARSET_UTF_8), KeyType.PublicKey);

        byte[] decrypt = rsa.decrypt(encrypt,KeyType.PrivateKey );
        String str1 = StrUtil.str(decrypt, CharsetUtil.CHARSET_UTF_8);
        System.out.println(str1);





/*//获得私钥
        PrivateKey privateKey = rsa.getPrivateKey();
        String privateKeyBase64 = rsa.getPrivateKeyBase64();
        System.out.println(privateKeyBase64);
//获得公钥
        PublicKey publicKey = rsa.getPublicKey();
        String publicKeyBase64 = rsa.getPublicKeyBase64();
        System.out.println(publicKeyBase64);*/

       /* int random=(int)((Math.random()*9+1)*100000);
        long timestamp=System.currentTimeMillis();

        String str = Convert.toStr(random)+";"+Convert.toStr(timestamp);
        System.out.println(str);


        byte[] encrypt = rsa.encrypt(StrUtil.bytes(str, CharsetUtil.CHARSET_UTF_8), KeyType.PublicKey);
        for (byte b : encrypt) {
            System.out.print(b);
        }
        //
        System.out.println();
        byte[] decrypt = rsa.decrypt(encrypt, KeyType.PrivateKey);
        System.out.println(Convert.toStr(decrypt));*/
        //6994101612516721812
//        byte[] encrypt = rsa.encrypt(StrUtil.bytes("我是一段测试aaaa", CharsetUtil.CHARSET_UTF_8), KeyType.PublicKey);
//        byte[] decrypt = rsa.decrypt(encrypt, KeyType.PrivateKey);
//        for (byte b : decrypt) {
//            System.out.print(b);
//        }
//        System.out.println();
//        for (byte b : encrypt) {
//            System.out.print(b);
//        }



//        int  y = (x*3+1)*3+x;
//        String str  = (y+"");
//
//        System.out.println(str.substring(0,str.length()-1));
//        String str ="" ;
//        System.out.println( StrUtil.hasBlank(str));
/*
String Str = "ssssssss";
        KeyPair keyPair = SecureUtil.generateKeyPair(Str);
        System.out.println(keyPair);
*/

//私钥加密，公钥解密
      /*  byte[] encrypt2 = rsa.encrypt(StrUtil.bytes("我是一段测试aaaa", CharsetUtil.CHARSET_UTF_8), KeyType.PrivateKey);
        byte[] decrypt2 = rsa.decrypt(encrypt2, KeyType.PublicKey);*/

//Junit单元测试
//Assert.assertEquals("我是一段测试aaaa", StrUtil.str(decrypt2, CharsetUtil.CHARSET_UTF_8));


    }

}
