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

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

import java.io.File;

public class RsaTest {

    //@Test
    public void generate() throws Exception {
        RsaKeyPair pair = RSA.generateKeyPair();
        System.out.println("public:" + pair.getPublicKey().toBase64());
        System.out.println("private:" + pair.getPrivateKey().toBase64());
        pair.getPublicKey().toFile(new File("/Users/luoshi/Downloads/public.key"));
        pair.getPrivateKey().toFile(new File("/Users/luoshi/Downloads/private.key"));
    }

    @Test
    public void test() throws Exception {
        RsaKeyPair pair = RSA.generateKeyPair();
        String content = RandomStringUtils.random(65536, true, true);
        ByteArray result = RSA.getPublicKey(pair.getPublicKey().value()).encrypt(ByteArray.fromString(content).value());
        Assert.assertNotNull(result.value());
        Assert.assertEquals(content,
                RSA.getPrivateKey(pair.getPrivateKey().value()).decrypt(result.value()).toString());

        result = RSA.getPrivateKey(pair.getPrivateKey().value()).encrypt(ByteArray.fromString(content).value());
        Assert.assertNotNull(result.value());
        Assert.assertEquals(content, RSA.getPublicKey(pair.getPublicKey().value()).decrypt(result.value()).toString());
    }

}
