Search in sources :

Example 11 with AbstractPublicKey

use of com.icodici.crypto.AbstractPublicKey in project universa by UniversaBlockchain.

the class RSAOAEPPublicKeyTest method encrypt.

/**
 * Test {@link RSAOAEPPublicKey#encrypt}.
 */
@Test
public void encrypt() throws Exception {
    // Test on RSA vectors.
    AbstractPublicKey rsaPublicKey = oaepSpec.getPublicKey();
    ((RSAOAEPPublicKey) rsaPublicKey).resetEncryptor();
    assertArrayEquals(rsaPublicKey.encrypt(oaepSpec.M), oaepSpec.C);
}
Also used : AbstractPublicKey(com.icodici.crypto.AbstractPublicKey) Test(org.junit.Test)

Example 12 with AbstractPublicKey

use of com.icodici.crypto.AbstractPublicKey in project universa by UniversaBlockchain.

the class RSAOAEPPublicKeyTest method toHash.

/**
 * Test {@link RSAOAEPPublicKey#toHash}.
 */
@Test
public void toHash() throws Exception {
    // Test sample RSA vectors.
    AbstractPublicKey rsaPublicKey = oaepSpec.getPublicKey();
    Map mapRSA = rsaPublicKey.toHash();
    assertArrayEquals((byte[]) mapRSA.get("n"), oaepSpec.n);
    assertArrayEquals((byte[]) mapRSA.get("e"), oaepSpec.e);
    // SHA-1 is the default value
    assertFalse(mapRSA.containsKey("mgf1Hash"));
    // Test random public key.
    AbstractPublicKey goodPublicKey1 = new RSAOAEPPublicKey(BigIntegers.asUnsignedByteArray(randomPublicKey1.getModulus()), BigIntegers.asUnsignedByteArray(randomPublicKey1.getExponent()), HashType.SHA512, HashType.SHA512, new SecureRandom());
    Map map1 = goodPublicKey1.toHash();
    assertArrayEquals((byte[]) map1.get("n"), BigIntegers.asUnsignedByteArray(randomPublicKey1.getModulus()));
    assertArrayEquals((byte[]) map1.get("e"), BigIntegers.asUnsignedByteArray(randomPublicKey1.getExponent()));
    assertEquals(map1.get("mgf1Hash"), "SHA-512");
}
Also used : SecureRandom(java.security.SecureRandom) AbstractPublicKey(com.icodici.crypto.AbstractPublicKey) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 13 with AbstractPublicKey

use of com.icodici.crypto.AbstractPublicKey in project universa by UniversaBlockchain.

the class RSAOAEPPublicKeyTest method updateFromHashGoodSample.

/**
 * Test {@link RSAOAEPPublicKey#updateFromHash} on success scenarios,
 * on some sample vector.
 */
@Test
public void updateFromHashGoodSample() throws Exception {
    Map map = new HashMap<String, Object>() {

        {
            put("n", Hex.decode("ad9a9c60d98ee0463f7629675703b23ed4b330ae851c278233a105d120713a6286945f71e6d46f8d9ad0d30e4551a5de8bb9d9a74750645c579c3902266aefecdc111e032048a84ea76220bc570c5cade835909546029a65baff6d29c4207c4b918ff524d80812ecfda388c8cd4ac1878699193040a075bcd4b3987fcbe749bbdfc44665616ff6789f7363b765eb72530e17698d9bd778fd476b3aefc6bce6cec5d44c52e4acb8981390af5174bcb84e3a17e719ba58b93d13bc929d48dbb78d8f5d85002b281159f10e4b801627d3f8acfab100bd2c6380d04ca08bcb9227d84fe282150d71f0660fbbff744800dc6466d47cf5c22d03bac3c0d73dac9bc2a020cb51ca229448c317e73e8f97125a5ff8568b6af0b493b822096021ee1c7eed6d0ad6164e0798055b3b24404983728c2923e9d959c655b2e138650de0ffb5a74b7e612e764fb0e3104ceb5b8df3aaae80a57dbb8f7c305503a51253fc0f1f7a82057c6af262a484f7243f69046c946fd8fc93d71ad90315bfb42467c826dad0253ec83e0ed9b3fa3dca8162f5c691a9e91f69b890b04138de12e0657d8b044f77b455d3ff4257c52e1a33e6792c7a64d9bd8009d79a5fe73dadcd9a80a2f5f51b1c80e6ee41928f0b08a1e9f5d552ec2e5380de554dac97d76aeac2000c4f992eb582532448641d89bbee2334e51d76a3952119093eb5baa545bf1beabd49f5"));
            put("e", Hex.decode("010001"));
            put("mgf1Hash", "SHA-512");
        }
    };
    AbstractPublicKey publicKey = new RSAOAEPPublicKey();
    publicKey.updateFromHash(map);
    assertTrue(publicKey.isInitialized());
    assertEquals(publicKey.getBitStrength(), 4096);
    RSAOAEPPublicKey.State publicKeyState = ((RSAOAEPPublicKey) publicKey).state;
    assertEquals(publicKeyState.oaepHashType, HashType.SHA1);
    /* Default for now */
    assertEquals(publicKeyState.mgf1HashType, HashType.SHA512);
}
Also used : HashMap(java.util.HashMap) HashMap(java.util.HashMap) Map(java.util.Map) AbstractPublicKey(com.icodici.crypto.AbstractPublicKey) Test(org.junit.Test)

Example 14 with AbstractPublicKey

use of com.icodici.crypto.AbstractPublicKey in project universa by UniversaBlockchain.

the class RSAOAEPPublicKeyTest method updateFromHashGoodSampleWithDefaultData.

/**
 * Test {@link RSAOAEPPublicKey#updateFromHash} on success scenarios,
 * on some sample vector where some data is default.
 */
@Test
public void updateFromHashGoodSampleWithDefaultData() throws Exception {
    Map map = new HashMap<String, byte[]>() {

        {
            put("n", Hex.decode("ad9a9c60d98ee0463f7629675703b23ed4b330ae851c278233a105d120713a6286945f71e6d46f8d9ad0d30e4551a5de8bb9d9a74750645c579c3902266aefecdc111e032048a84ea76220bc570c5cade835909546029a65baff6d29c4207c4b918ff524d80812ecfda388c8cd4ac1878699193040a075bcd4b3987fcbe749bbdfc44665616ff6789f7363b765eb72530e17698d9bd778fd476b3aefc6bce6cec5d44c52e4acb8981390af5174bcb84e3a17e719ba58b93d13bc929d48dbb78d8f5d85002b281159f10e4b801627d3f8acfab100bd2c6380d04ca08bcb9227d84fe282150d71f0660fbbff744800dc6466d47cf5c22d03bac3c0d73dac9bc2a020cb51ca229448c317e73e8f97125a5ff8568b6af0b493b822096021ee1c7eed6d0ad6164e0798055b3b24404983728c2923e9d959c655b2e138650de0ffb5a74b7e612e764fb0e3104ceb5b8df3aaae80a57dbb8f7c305503a51253fc0f1f7a82057c6af262a484f7243f69046c946fd8fc93d71ad90315bfb42467c826dad0253ec83e0ed9b3fa3dca8162f5c691a9e91f69b890b04138de12e0657d8b044f77b455d3ff4257c52e1a33e6792c7a64d9bd8009d79a5fe73dadcd9a80a2f5f51b1c80e6ee41928f0b08a1e9f5d552ec2e5380de554dac97d76aeac2000c4f992eb582532448641d89bbee2334e51d76a3952119093eb5baa545bf1beabd49f5"));
            put("e", Hex.decode("010001"));
        // Not putting any of the optional fields.
        }
    };
    AbstractPublicKey publicKey = new RSAOAEPPublicKey();
    publicKey.updateFromHash(map);
    assertTrue(publicKey.isInitialized());
    assertEquals(publicKey.getBitStrength(), 4096);
    RSAOAEPPublicKey.State publicKeyState = ((RSAOAEPPublicKey) publicKey).state;
    assertEquals(publicKeyState.oaepHashType, HashType.SHA1);
    assertEquals(publicKeyState.mgf1HashType, HashType.SHA1);
}
Also used : HashMap(java.util.HashMap) HashMap(java.util.HashMap) Map(java.util.Map) AbstractPublicKey(com.icodici.crypto.AbstractPublicKey) Test(org.junit.Test)

Example 15 with AbstractPublicKey

use of com.icodici.crypto.AbstractPublicKey in project universa by UniversaBlockchain.

the class RSAOAEPPublicKeyTest method checkSignature.

/**
 * Test {@link RSAOAEPPublicKey#checkSignature}.
 */
@Test
public void checkSignature() throws Exception {
    // Test sample RSA vectors.
    AbstractPublicKey rsaPublicKey = pssSpec.getPublicKey();
    AbstractPrivateKey rsaPrivateKey = pssSpec.getPrivateKey();
    assertArrayEquals(rsaPrivateKey.sign(pssSpec.M, HashType.SHA1, RSASSAPSSTestVectors.salt), pssSpec.S);
    assertTrue(rsaPublicKey.checkSignature(pssSpec.M, rsaPrivateKey.sign(pssSpec.M, HashType.SHA1, RSASSAPSSTestVectors.salt), HashType.SHA1, RSASSAPSSTestVectors.salt.length));
}
Also used : AbstractPublicKey(com.icodici.crypto.AbstractPublicKey) AbstractPrivateKey(com.icodici.crypto.AbstractPrivateKey) Test(org.junit.Test)

Aggregations

AbstractPublicKey (com.icodici.crypto.AbstractPublicKey)16 Test (org.junit.Test)16 HashMap (java.util.HashMap)11 Map (java.util.Map)11 AbstractPrivateKey (com.icodici.crypto.AbstractPrivateKey)7 SecureRandom (java.security.SecureRandom)3 Field (java.lang.reflect.Field)2 RSAKeyParameters (org.spongycastle.crypto.params.RSAKeyParameters)2 RSAPrivateCrtKeyParameters (org.spongycastle.crypto.params.RSAPrivateCrtKeyParameters)2