Search in sources :

Example 16 with AbstractPublicKey

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

the class RSAOAEPPublicKeyTest method updateFromHashGoodRSASpec.

/**
 * Test {@link RSAOAEPPublicKey#updateFromHash} on success scenarios,
 * using RSA spec vectors.
 */
@Test
public void updateFromHashGoodRSASpec() throws Exception {
    Map mapRSA = new HashMap<String, Object>() {

        {
            put("n", oaepSpec.n);
            put("e", oaepSpec.e);
            put("hash", "SHA-1");
            put("mgf1Hash", "SHA-1");
        }
    };
    AbstractPublicKey rsaPublicKey = new RSAOAEPPublicKey();
    rsaPublicKey.updateFromHash(mapRSA);
    // We've read the public key from hash;
    // but, to test it over the RSA spec vectors,
    // we need to hack into the state.rng (and substitute it with our custom one),
    // even though it is declared as final.
    RSAOAEPPublicKey.State state = ((RSAOAEPPublicKey) rsaPublicKey).state;
    Field rngField = RSAOAEPPublicKey.State.class.getDeclaredField("rng");
    rngField.setAccessible(true);
    rngField.set(state, oaepSpec.getRandSeed());
    // Now we can even test the encryption on the RSA spec vectors.
    assertTrue(rsaPublicKey.isInitialized());
    assertEquals(rsaPublicKey.getBitStrength(), 1024);
    ((RSAOAEPPublicKey) rsaPublicKey).resetEncryptor();
    assertArrayEquals(rsaPublicKey.encrypt(oaepSpec.M), oaepSpec.C);
}
Also used : Field(java.lang.reflect.Field) HashMap(java.util.HashMap) HashMap(java.util.HashMap) Map(java.util.Map) AbstractPublicKey(com.icodici.crypto.AbstractPublicKey) 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