package com.qmth.boot.test.tools.crypto;

import com.qmth.boot.tools.crypto.AES;
import com.qmth.boot.tools.models.ByteArray;
import org.apache.commons.lang3.RandomStringUtils;
import org.junit.Assert;
import org.junit.Test;

public class AesTest {

    @Test
    public void testECB() throws Exception {
        String key = RandomStringUtils.random(16, true, true);
        String content = RandomStringUtils.random(128, true, true);
        ByteArray result = AES.encrypt(ByteArray.fromString(content).value(), key);
        Assert.assertNotNull(result.value());
        Assert.assertEquals(content, AES.decrypt(result.value(), key).toString());
    }

    @Test
    public void testCBC() throws Exception {
        String key = RandomStringUtils.random(16, true, true);
        String vector = RandomStringUtils.random(16, true, true);
        String content = RandomStringUtils.random(128, true, true);
        ByteArray result = AES.encrypt(ByteArray.fromString(content).value(), key, vector);
        Assert.assertNotNull(result.value());
        Assert.assertEquals(content, AES.decrypt(result.value(), key, vector).toString());
    }
}
