package com.sjyt.icom.asst.service;

import cn.hutool.crypto.Mode;
import cn.hutool.crypto.Padding;
import cn.hutool.crypto.digest.DigestAlgorithm;
import cn.hutool.crypto.digest.DigestUtil;
import cn.hutool.crypto.digest.Digester;
import cn.hutool.crypto.symmetric.DES;
import cn.hutool.json.JSON;
import com.sjyt.icom.asst.common.util.DESUtils;
import com.sjyt.icom.asst.common.util.JsonUtils;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>TODO</p>
 *
 * @author renyanru
 * @version V1.0.0
 * @date 2022/6/27 16:23
 */
public class SymmetricCryptoTest {
    @Test
    public void des(){
        Map<String,String> m=new HashMap<>();
        m.put("uid","7kwaBAqAGxLE9wk6VbkzfVpgQkfy2BxCzSMQ5J2HjHT");
        m.put("rid","1234567891234");
        String text = JsonUtils.pojoToJson(m);
        String key="12345678";
        String iv="12345678";
        DES des=new DES(Mode.CBC, Padding.PKCS5Padding,key.getBytes(),iv.getBytes());
        String s = des.encryptBase64(text);
        System.out.println(s);
        String s1 = des.decryptStr(s);
        System.out.println(s1);
    }
    @Test
    public void des1() throws Exception {
        Map<String,String> m=new HashMap<>();
        m.put("uid","7kwaBAqAGxLE9wk6VbkzfVpgQkfy2BxCzSMQ5J2HjHT");
        m.put("rid","1234567891234");
        String text = JsonUtils.pojoToJson(m);
        String encryption = DESUtils.encryption(text, "123");
        System.out.println(encryption);
        String decryption = DESUtils.decryption(encryption, "123");
        System.out.println(decryption);
    }
    @Test
    public void md5() {
        String text = "HelloWorld";

        // 第一种：创建Digester对象，执行加密
        Digester md5 = new Digester(DigestAlgorithm.MD5);
        String digestHex = md5.digestHex(text);
        System.out.println(digestHex);

        // 第二种：执行使用DigestUtil
        String md5Hex = DigestUtil.md5Hex(text);
        System.out.println(md5Hex);
    }

    @Test
    public void sha1() {
       // String text = "HelloWorld";
        Map<String,String> m=new HashMap<>();
        m.put("uid","7kwaBAqAGxLE9wk6VbkzfVpgQkfy2BxCzSMQ5J2HjHT");
        m.put("rid","1234567891234");
        String text = JsonUtils.pojoToJson(m);

        // 第一种：创建Digester对象，执行加密
        Digester md5 = new Digester(DigestAlgorithm.SHA1);
        String digestHex = md5.digestHex(text);
        System.out.println(digestHex);

        // 第二种：执行使用DigestUtil
        String md5Hex = DigestUtil.sha1Hex(text);
        System.out.println(md5Hex);
    }
}
