package riomhaire.lti.core.business.commands;

import org.junit.Assert;
import org.junit.Test;

import java.security.KeyFactory;
import java.security.spec.PKCS8EncodedKeySpec;
import java.util.Base64;

public class PrivateKeyTest {

    String pemBase64 = "LS0tLS1CRUdJTiBSU0EgUFJJVkFURSBLRVktLS0tLQpNSUlFb3dJQkFBS0NBUUVBNVZzZmpzNmtzSDFmRGd1U3pkUytIZHJNNDB0TEo3VGpjUHVhMVNiVmhaVFFYUDhqCk5zblkvWlRuOERLUkQ3Rmdab2NpR2dBU0Q2RXlCRkhSYWoyWWNLYlpLdDc0SVlrYUc0MW8wbTRJVFBHalJpc2QKVWxnM1E0NnBSU09sUTYyZTB0eExxbDNSVExvMU1QTjRLLzdjL3FYeWpKWEdKLzI1cTZCeU9xQUVnRHZaVERmUApDaVFpQXhoZWFPdmRWb1BFZUlkeVFJY21KUGZvOVNuVkQ4ZHNEOTE4V05KcFFkenR4d1E4K3NxMmc1SHllSFYyCmV3bWJ2U3gzNnNhOUVHNVZtZU5LTGVxVGNoY2wrUTliNGFnSjB0ZTFOSm4yYjRxcWtycjMrZWxqU3IrU0N5RGsKQTM2UldLZ2RCbzhOd0c2RGhZNFlBYkpQc2xpT0I2bGN5eTBZbFFJREFRQUJBb0lCQUhnQmxPS1JCeVQ5aEdZQgo4ajlXcUY5UW85SHVESlhyRlBLQ0E4S3h2aGxXQXl3VnAyUThleGc3K0p2NnFXSXZxa1FiUXVHZDFkVnR2NUQ1CkNuenZXN3JkNGxXSE13ZkJCL01RcTU4blNacVErS0IzWUxFOFRUWkJDQnFRQ3lIOVQxUC9SbXdQQ0x0LzF1TVYKRVhUYnR1QmJEaTAzRk9iSjRJT0h3UjBocXhrNHVlUUNDR3hCZkh0R203TW9QMHI5VWFJRWhjTDdYWXhCSzNVOAoyUVpkenRPQkNCRnM1anNLNXR6RGUvLzY1Q25LVlo2K0RYZG5MeXBuTEp3bGx6dlNPYVNwdHdEZkhUTWlOUnM4CmNWZTBrT3FURjVqbFJCWjNuaStkaE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zlkRnoyTmtMemd1VnhsYXk4MkZTRWk4dytuSUxIWEczVkF4WWJ2aGMKMlIvNVYva3R1NzJNUUFPSUh2THorcXhodkdneWczV0hiekJVbXl1T1ZqWDhFRjBnMkVXZ2J0c09UNmhMT2lqRQplOHpFOFYzVVRuWkRNRk1MQjg2SFMyZms0WEUrQlVaL1NtNEZPUDhlb0R6VzN4V1dCSEZRCi0tLS0tRU5EIFJTQSBQUklWQVRFIEtFWS0tLS0t";


    @Test
    public void testPem2PrivateKey() throws Exception{

        var privatePemString =  new String(Base64.getDecoder().decode(pemBase64));   // default to 1st
        privatePemString = privatePemString.replace("-----BEGIN RSA PRIVATE KEY-----", "");
        privatePemString = privatePemString.replace("-----END RSA PRIVATE KEY-----", "");
        privatePemString = privatePemString.replaceAll("\\s+","");

        var keySpec = new PKCS8EncodedKeySpec(Base64.getDecoder().decode(privatePemString));
        var keyfactory = KeyFactory.getInstance("RSA");
        var privateKey = keyfactory.generatePrivate(keySpec); // error?
        Assert.assertNotNull(privateKey);
    }

    static {
        java.security.Security.addProvider(
                new org.bouncycastle.jce.provider.BouncyCastleProvider()
        );
    }
}
