Search in sources :

Example 1 with PrivateKey

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));
}
Also used : PrivateKey(io.nem.symbol.core.crypto.PrivateKey) PublicKey(io.nem.symbol.core.crypto.PublicKey) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 2 with PrivateKey

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());
}
Also used : PrivateKey(io.nem.symbol.core.crypto.PrivateKey) PublicKey(io.nem.symbol.core.crypto.PublicKey) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 3 with PrivateKey

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));
}
Also used : PrivateKey(io.nem.symbol.core.crypto.PrivateKey) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 4 with PrivateKey

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());
}
Also used : PrivateKey(io.nem.symbol.core.crypto.PrivateKey) PublicKey(io.nem.symbol.core.crypto.PublicKey) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 5 with PrivateKey

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()));
}
Also used : PrivateKey(io.nem.symbol.core.crypto.PrivateKey) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Aggregations

PrivateKey (io.nem.symbol.core.crypto.PrivateKey)6 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)5 PublicKey (io.nem.symbol.core.crypto.PublicKey)4 MethodSource (org.junit.jupiter.params.provider.MethodSource)4 BlockCipher (io.nem.symbol.core.crypto.BlockCipher)1 CryptoEngine (io.nem.symbol.core.crypto.CryptoEngine)1 KeyPair (io.nem.symbol.core.crypto.KeyPair)1 Test (org.junit.jupiter.api.Test)1