use of io.nem.symbol.core.crypto.PrivateKey in project nem2-sdk-java by nemtech.
the class Ed25519BlockCipherVectorTester method testEncrypt.
@ParameterizedTest
@MethodSource("cipherVector")
void testEncrypt(String privateKey, String otherPublicKey, String iv, String tag, String cipherText, String clearText) {
PrivateKey privateKeyObject = PrivateKey.fromHexString(privateKey);
PublicKey otherPublicKeyObject = PublicKey.fromHexString(otherPublicKey);
Ed25519BlockCipher cipher = new Ed25519BlockCipher(KeyPair.fromPrivate(privateKeyObject), KeyPair.onlyPublic(otherPublicKeyObject));
AuthenticatedCipherText encodedData = cipher.encode(ConvertUtils.fromHexToBytes(clearText), ConvertUtils.fromHexToBytes(iv));
Assertions.assertEquals(iv, ConvertUtils.toHex(encodedData.getIv()));
Assertions.assertEquals(cipherText, ConvertUtils.toHex(encodedData.getCipherText()));
Assertions.assertEquals(tag, ConvertUtils.toHex(encodedData.getAuthenticationTag()));
byte[] encryptData = cipher.encrypt(ConvertUtils.fromHexToBytes(clearText), ConvertUtils.fromHexToBytes(iv));
Assertions.assertEquals(tag + iv + cipherText, ConvertUtils.toHex(encryptData));
}
use of io.nem.symbol.core.crypto.PrivateKey in project nem2-sdk-java by nemtech.
the class Ed25519BlockCipherVectorTester method testDecrypt.
@ParameterizedTest
@MethodSource("cipherVector")
void testDecrypt(String privateKey, String otherPublicKey, String iv, String tag, String cipherText, String clearText) {
PrivateKey privateKeyObject = PrivateKey.fromHexString(privateKey);
PublicKey otherPublicKeyObject = PublicKey.fromHexString(otherPublicKey);
Ed25519BlockCipher cipher = new Ed25519BlockCipher(KeyPair.onlyPublic(otherPublicKeyObject), KeyPair.fromPrivate(privateKeyObject));
byte[] decrypted = cipher.decode(ConvertUtils.fromHexToBytes(tag), ConvertUtils.fromHexToBytes(iv), ConvertUtils.fromHexToBytes(cipherText));
Assertions.assertNotNull(decrypted);
Assertions.assertEquals(clearText.toUpperCase(), ConvertUtils.toHex(decrypted).toUpperCase());
}
use of io.nem.symbol.core.crypto.PrivateKey in project nem2-sdk-java by nemtech.
the class Ed25519UtilsTest method prepareForScalarMultiplyReturnsClampedValue.
// region prepareForScalarMultiply
@Test
public void prepareForScalarMultiplyReturnsClampedValue() {
// Arrange:
final PrivateKey privateKey = new PrivateKey(RandomUtils.generateRandomBytes(32));
// Act:
final byte[] a = Ed25519Utils.prepareForScalarMultiply(privateKey).getRaw();
// Assert:
MatcherAssert.assertThat(a[31] & 0x40, IsEqual.equalTo(0x40));
MatcherAssert.assertThat(a[31] & 0x80, IsEqual.equalTo(0x0));
MatcherAssert.assertThat(a[0] & 0x7, IsEqual.equalTo(0x0));
}
use of io.nem.symbol.core.crypto.PrivateKey in project nem2-sdk-java by nemtech.
the class Ed25519BlockDeriveVectorTester method testResolveSharedKey.
@ParameterizedTest
@MethodSource("testResolveSharedKey")
void testResolveSharedKey(String privateKey, String otherPublicKey, String scalarMulResult, String sharedKey) {
PrivateKey privateKeyObject = PrivateKey.fromHexString(privateKey);
PublicKey otherPublicKeyObject = PublicKey.fromHexString(otherPublicKey);
byte[] resolvedSharedKey = Ed25519BlockCipher.getSharedKey(privateKeyObject, otherPublicKeyObject);
byte[] resolvedSharedSecret = Ed25519BlockCipher.getSharedSecret(privateKeyObject, otherPublicKeyObject);
Assertions.assertEquals(sharedKey.toUpperCase(), ConvertUtils.toHex(resolvedSharedKey).toUpperCase());
Assertions.assertEquals(scalarMulResult.toUpperCase(), ConvertUtils.toHex(resolvedSharedSecret).toUpperCase());
}
use of io.nem.symbol.core.crypto.PrivateKey in project nem2-sdk-java by nemtech.
the class Ed25519UtilsTest method shouldPrepareForScalarMultiply.
@ParameterizedTest
@MethodSource("params")
public void shouldPrepareForScalarMultiply(String input, String expected) {
// Arrange:
final PrivateKey privateKey = PrivateKey.fromHexString(input);
Assertions.assertEquals(input.toUpperCase(), privateKey.toHex().toUpperCase());
Assertions.assertEquals(expected.toUpperCase(), ConvertUtils.toHex(Ed25519Utils.prepareForScalarMultiply(privateKey).getRaw()));
}
Aggregations