package com.bonus.canteen.core.config;

import cn.hutool.core.codec.Base64;
import cn.hutool.core.codec.Base64Encoder;
import cn.hutool.crypto.SmUtil;
import cn.hutool.crypto.asymmetric.SM2;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.junit.runner.RunWith;
import org.springframework.test.context.junit4.SpringRunner;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import java.io.PrintStream;
import java.nio.charset.StandardCharsets;
import java.security.NoSuchAlgorithmException;

@RunWith(SpringRunner.class)
public class SmUtilsTest {

    public static void main(String[] args) throws NoSuchAlgorithmException {
        sm2();
//        sm4();
//        sm4WithoutIV();
    }

    private static void sm2() {
        System.out.println("=================sm2===================");
        String privateKeyStr = "D55F4709BE51FCDC71D6385885A5CAEE70A09438F862BEB4E56F64A70C76EF5F";
        String pubKeyStr = "04768E8E44656FFD4BA58C0270002A28365A5F6B0F6D40E88B9221CDFAAA8E82C8CCEDBA5FC2D03F20B11492EBE90CC04782682AFE326363A503F086C04A14092C";
        SM2 sm2 = SmUtil.sm2(privateKeyStr, pubKeyStr);
        String data = "liolay123466789";
        byte[] encrypt = sm2.encrypt("2AD2252000231631".getBytes(StandardCharsets.UTF_8));
        System.out.println("encrypt:" + Base64.encode(encrypt));
        PrintStream var10000 = System.out;
        byte[] var10001 = sm2.sign(data.getBytes(StandardCharsets.UTF_8));
        var10000.println("sign:" + Base64Encoder.encode(var10001));
        var10000 = System.out;
        String var5 = new String(sm2.decrypt(encrypt));
        var10000.println("decrypt:" + var5);
    }

    private static void sm4() throws NoSuchAlgorithmException {
        System.out.println("================sm4==================");
        BouncyCastleProvider provider = new BouncyCastleProvider();
        KeyGenerator generator = KeyGenerator.getInstance("SM4", provider);
        SecretKey secretKey = generator.generateKey();
        byte[] encoded = secretKey.getEncoded();
        String key = Base64Encoder.encode(encoded);
        System.out.println("key: " + key);
        String data = "liolay";
        String encryptBySm4 = SmUtils.encryptBySm4(data, encoded);
        System.out.println("encrypt: " + encryptBySm4);
        PrintStream var10000 = System.out;
        String var10001 = SmUtils.decryptBySm4(encryptBySm4, encoded);
        var10000.println("decrypt: " + var10001);
        var10000 = System.out;
        var10001 = SmUtils.decryptBySm4(encryptBySm4, key);
        var10000.println("decrypt: " + var10001);
    }

    private static void sm4WithoutIV() throws NoSuchAlgorithmException {
        System.out.println("================sm4WithoutIV==================");
        BouncyCastleProvider provider = new BouncyCastleProvider();
        KeyGenerator generator = KeyGenerator.getInstance("SM4", provider);
        SecretKey secretKey = generator.generateKey();
        byte[] key = secretKey.getEncoded();
        String keyEncode = Base64Encoder.encode(key);
        System.out.println("key: " + keyEncode);
        String plainData = "liolay";
        String encryptedData = SmUtils.encryptBySm4(plainData, key);
        System.out.println("encrypt: " + encryptedData);
        String decryptData = SmUtils.decryptBySm4(encryptedData, key);
        System.out.println("decrypt: " + decryptData);
    }

}
