use of com.icodici.crypto.AbstractPublicKey in project universa by UniversaBlockchain.
the class RSAOAEPPrivateKeyTest method getPublicKey.
/**
* Test {@link RSAOAEPPrivateKey#getPublicKey}.
*/
@Test
public void getPublicKey() throws Exception {
AbstractPublicKey randomPublicKey4096 = randomPrivateKey4096.getPublicKey();
assertTrue(randomPublicKey4096 instanceof RSAOAEPPublicKey);
AbstractPrivateKey rsaPrivateKey = oaepSpec.getPrivateKey();
AbstractPublicKey rsaPublicKey = rsaPrivateKey.getPublicKey();
((RSAOAEPPublicKey) rsaPublicKey).resetEncryptor();
assertArrayEquals(rsaPublicKey.encrypt(oaepSpec.M), oaepSpec.C);
}
use of com.icodici.crypto.AbstractPublicKey in project universa by UniversaBlockchain.
the class RSAOAEPPrivateKeyTest method toHashDefaultData.
/**
* Test {@link RSAOAEPPrivateKey#toHash} where some data is default.
*/
@Test
public void toHashDefaultData() throws Exception {
// Test random key pair (4096 bit, SHA-256/256).
AbstractPublicKey randomPublicKey4096SHA256 = randomPrivateKey4096SHADefault.getPublicKey();
Map mapPrivate4096SHA256 = randomPrivateKey4096SHADefault.toHash(), mapPublic4096SHA256 = randomPublicKey4096SHA256.toHash();
RSAPrivateCrtKeyParameters privateKeyParameters4096SHA256 = ((RSAOAEPPrivateKey) randomPrivateKey4096SHADefault).state.keyParameters;
RSAKeyParameters publicKeyParameters4096 = ((RSAOAEPPublicKey) (randomPrivateKey4096SHADefault.getPublicKey())).state.keyParameters;
// Check private key serialization.
assertArrayEquals((byte[]) mapPrivate4096SHA256.get("e"), BigIntegers.asUnsignedByteArray(privateKeyParameters4096SHA256.getPublicExponent()));
assertArrayEquals((byte[]) mapPrivate4096SHA256.get("p"), BigIntegers.asUnsignedByteArray(privateKeyParameters4096SHA256.getP()));
assertArrayEquals((byte[]) mapPrivate4096SHA256.get("q"), BigIntegers.asUnsignedByteArray(privateKeyParameters4096SHA256.getQ()));
// With the default values (SHA-1), hash and mgf1Hash fields should be missing from the hash.
assertFalse(mapPrivate4096SHA256.containsKey("mgf1Hash"));
// Check public key serialization.
assertArrayEquals((byte[]) mapPublic4096SHA256.get("n"), BigIntegers.asUnsignedByteArray(publicKeyParameters4096.getModulus()));
assertArrayEquals((byte[]) mapPublic4096SHA256.get("e"), BigIntegers.asUnsignedByteArray(publicKeyParameters4096.getExponent()));
// With the default values (SHA-256), hash and mgf1Hash fields should be missing from the hash.
assertFalse(mapPublic4096SHA256.containsKey("mgf1Hash"));
}
use of com.icodici.crypto.AbstractPublicKey in project universa by UniversaBlockchain.
the class RSAOAEPPublicKeyTest method toHashWithDefaultData.
/**
* Test {@link RSAOAEPPublicKey#toHash} where some data is default.
*/
@Test
public void toHashWithDefaultData() throws Exception {
// Test random public key.
AbstractPublicKey goodPublicKey1 = new RSAOAEPPublicKey(BigIntegers.asUnsignedByteArray(randomPublicKey1.getModulus()), BigIntegers.asUnsignedByteArray(randomPublicKey1.getExponent()), HashType.SHA1, HashType.SHA1, new SecureRandom());
Map map1 = goodPublicKey1.toHash();
assertArrayEquals((byte[]) map1.get("n"), BigIntegers.asUnsignedByteArray(randomPublicKey1.getModulus()));
assertArrayEquals((byte[]) map1.get("e"), BigIntegers.asUnsignedByteArray(randomPublicKey1.getExponent()));
// With the default values (SHA-256 for hash, SHA-1 for MGF1),
// hash and mgf1Hash fields should be missing from the hash.
assertFalse(map1.containsKey("hash"));
assertFalse(map1.containsKey("mgf1Hash"));
}
use of com.icodici.crypto.AbstractPublicKey in project universa by UniversaBlockchain.
the class RSAOAEPPublicKeyTest method basicMethods.
/**
* Test {@link RSAOAEPPublicKey} basic methods:
* {@link RSAOAEPPublicKey#canEncrypt},
* {@link RSAOAEPPublicKey#getBitStrength},
* {@link RSAOAEPPublicKey#isInitialized},
* {@link RSAOAEPPublicKey#algorithmTag}.
*/
@Test
public void basicMethods() throws Exception {
AbstractPublicKey goodPublicKey1 = oaepSpec.getPublicKey();
assertEquals(goodPublicKey1.canEncrypt(), true);
assertEquals(goodPublicKey1.getBitStrength(), 1024);
assertEquals(goodPublicKey1.isInitialized(), true);
assertEquals(goodPublicKey1.algorithmTag(), "r1");
}
use of com.icodici.crypto.AbstractPublicKey in project universa by UniversaBlockchain.
the class RSAOAEPPublicKeyTest method updateFromHashBad.
/**
* Test {@link RSAOAEPPublicKey#updateFromHash} on failure scenarios.
*/
@Test
public void updateFromHashBad() throws Exception {
// No data in map
Map badMapNoData = new HashMap<String, byte[]>();
AbstractPublicKey publicKeyNoData = new RSAOAEPPublicKey();
try {
publicKeyNoData.updateFromHash(badMapNoData);
} catch (Exception e) {
assertTrue(e instanceof Hashable.Error);
}
assertFalse(publicKeyNoData.isInitialized());
// Partial data in map
Map badMapPartialData = new HashMap<String, byte[]>() {
{
put("hash", Hex.decode("5348412d353132"));
put("mgf1Hash", Hex.decode("5348412d323536"));
}
};
AbstractPublicKey publicKeyPartialData = new RSAOAEPPublicKey();
try {
publicKeyNoData.updateFromHash(badMapPartialData);
} catch (Exception e) {
assertTrue(e instanceof Hashable.Error);
}
assertFalse(publicKeyPartialData.isInitialized());
// Bad string
Map badMapBadString = new HashMap<String, byte[]>() {
{
put("n", Hex.decode("ad9a9c60d98ee0463f7629675703b23ed4b330ae851c278233a105d120713a6286945f71e6d46f8d9ad0d30e4551a5de8bb9d9a74750645c579c3902266aefecdc111e032048a84ea76220bc570c5cade835909546029a65baff6d29c4207c4b918ff524d80812ecfda388c8cd4ac1878699193040a075bcd4b3987fcbe749bbdfc44665616ff6789f7363b765eb72530e17698d9bd778fd476b3aefc6bce6cec5d44c52e4acb8981390af5174bcb84e3a17e719ba58b93d13bc929d48dbb78d8f5d85002b281159f10e4b801627d3f8acfab100bd2c6380d04ca08bcb9227d84fe282150d71f0660fbbff744800dc6466d47cf5c22d03bac3c0d73dac9bc2a020cb51ca229448c317e73e8f97125a5ff8568b6af0b493b822096021ee1c7eed6d0ad6164e0798055b3b24404983728c2923e9d959c655b2e138650de0ffb5a74b7e612e764fb0e3104ceb5b8df3aaae80a57dbb8f7c305503a51253fc0f1f7a82057c6af262a484f7243f69046c946fd8fc93d71ad90315bfb42467c826dad0253ec83e0ed9b3fa3dca8162f5c691a9e91f69b890b04138de12e0657d8b044f77b455d3ff4257c52e1a33e6792c7a64d9bd8009d79a5fe73dadcd9a80a2f5f51b1c80e6ee41928f0b08a1e9f5d552ec2e5380de554dac97d76aeac2000c4f992eb582532448641d89bbee2334e51d76a3952119093eb5baa545bf1beabd49f5"));
put("e", Hex.decode("010001"));
put("hash", Hex.decode("5348412d353132"));
put("mgf1Hash", Hex.decode("00112233"));
}
};
AbstractPublicKey publicKeyBadString = new RSAOAEPPublicKey();
try {
publicKeyNoData.updateFromHash(badMapBadString);
} catch (Exception e) {
assertTrue(e instanceof Hashable.Error);
}
assertFalse(publicKeyBadString.isInitialized());
}
Aggregations