Search in sources :

Example 1 with RsaKey

use of com.microsoft.azure.keyvault.cryptography.RsaKey in project azure-sdk-for-java by Azure.

the class RsaKeyTest method getTestRsaKey.

private RsaKey getTestRsaKey() throws Exception {
    String jwkString = "{\"kty\":\"RSA\",\"n\":\"rZ8pnmXkhfmmgNWVVdtNcYy2q0OAcCGIpeFzsN9URqJsiBEiWQfxlUxFTbM4kVWPqjauKt6byvApBGEeMA7Qs8kxwRVP-BD4orXRe9VPgliM92rH0UxQWHmCHUe7G7uUAFPwbiDVhWuFzELxNa6Kljg6Z9DuUKoddmQvlYWj8uSunofCtDi_zzlZKGYTOYJma5IYScHNww1yjLp8-b-Be2UdHbrPkCv6Nuwi6MVIKjPpEeRQgfefRmxDBJQKY3OfydMXZmEwukYXVkUcdIP8XwG2OxnfdRK0oAo0NDebNNVuT89k_3AyZLTr1KbDmx1nnjwa8uB8k-uLtcOC9igbTw\",\"e\":\"AQAB\",\"d\":\"H-z7hy_vVJ9yeZBMtIvt8qpQUK_J51STPwV085otcgud72tPKJXoW2658664ASl9kGwbnLBwb2G3-SEunuGqiNS_PGUB3niob6sFSUMRKsPDsB9HfPoOcCZvwZiWFGRqs6C7vlR1TuJVqRjKJ_ffbf4K51oo6FZPspx7j4AShLAwLUSQ60Ld5QPuxYMYZIMpdVbMVIVHJ26pR4Y18e_0GYmEGnbF5N0HkwqQmfmTiIK5aoGnD3GGgqHeHmWBwh6_WAq90ITLcX_zBeqQUgBSj-Z5v61SroO9Eang36T9mMoYrcPpYwemtAOb4HhQYDj8dCCfbeOcVmvZ9UJKWCX2oQ\",\"dp\":\"HW87UpwPoj3lPI9B9K1hJFeuGgarpakvtHuk1HpZ5hXWFGAJiXoWRV-jvYyjoM2k7RpSxPyuuFFmYHcIxiGFp2ES4HnP0BIhKVa2DyugUxIEcMK53C43Ub4mboJPZTSC3sapKgAmA2ue624sapWmshTPpx9qnUP2Oj3cSMkgMGE\",\"dq\":\"RhwEwb5FYio0GS2tmul8FAYsNH7JDehwI1yUApnTiakhSenFetml4PYyVkKR4csgLZEi3RY6J3R8Tg-36zrZuF7hxhVJn80L5_KETSpfEI3jcrXMVg4SRaMsWLY9Ahxflt2FJgUnHOmWRLmP6_hmaTcxxSACjbyUd_HhwNavD5E\",\"qi\":\"wYPZ4lKIslA1w3FaAzQifnNLABYXXUZ_KAA3a8T8fuxkdE4OP3xIFX7WHhnmBd6uOFiEcGoeq2jNQqDg91rV5661-5muQKcvp4uUsNId5rQw9EZw-kdDcwMtVFTEBfvVuyp83X974xYAHn1Jd8wWohSwrpi1QuH5cQMR5Fm6I1A\",\"p\":\"74Ot7MgxRu4euB31UWnGtrqYPjJmvbjYESS43jfDfo-s62ggV5a39P_YPg6oosgtGHNw0QDxunUOXNu9iriaYPf_imptRk69bKN8Nrl727Y-AaBYdLf1UZuwz8X07FqHAH5ghYpk79djld8QvkUUJLpx6rzcW8BJLTOi46DtzZE\",\"q\":\"uZJu-qenARIt28oj_Jlsk-p_KLnqdczczZfbRDd7XNp6csGLa8R0EyYqUB4xLWELQZsX4tAu9SaAO62tuuEy5wbOAmOVrq2ntoia1mGQSJdoeVq6OqtN300xVnaBc3us0rm8C6-824fEQ1PWXoulXLKcSqBhFT-hQahsYi-kat8\"}";
    ObjectMapper mapper = new ObjectMapper();
    JsonWebKey jwk = null;
    jwk = mapper.readValue(jwkString, JsonWebKey.class);
    return new RsaKey("foo", jwk.toRSA(true, _provider));
}
Also used : RsaKey(com.microsoft.azure.keyvault.cryptography.RsaKey) JsonWebKey(com.microsoft.azure.keyvault.webkey.JsonWebKey) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 2 with RsaKey

use of com.microsoft.azure.keyvault.cryptography.RsaKey in project azure-sdk-for-java by Azure.

the class RsaKeyTest method testRsa15.

@Test
public void testRsa15() throws Exception {
    RsaKey key = getTestRsaKey();
    // Wrap and Unwrap
    Pair<byte[], String> wrapped = key.wrapKeyAsync(CEK, Rsa15.ALGORITHM_NAME).get();
    byte[] unwrapped = key.unwrapKeyAsync(wrapped.getLeft(), wrapped.getRight()).get();
    // Assert
    assertEquals(Rsa15.ALGORITHM_NAME, wrapped.getRight());
    assertArrayEquals(CEK, unwrapped);
    // Encrypt and Decrypt
    Triple<byte[], byte[], String> encrypted = key.encryptAsync(CEK, null, null, Rsa15.ALGORITHM_NAME).get();
    byte[] decrypted = key.decryptAsync(encrypted.getLeft(), null, null, null, encrypted.getRight()).get();
    // Assert
    assertEquals(Rsa15.ALGORITHM_NAME, encrypted.getRight());
    assertArrayEquals(CEK, decrypted);
    key.close();
}
Also used : RsaKey(com.microsoft.azure.keyvault.cryptography.RsaKey) Test(org.junit.Test)

Example 3 with RsaKey

use of com.microsoft.azure.keyvault.cryptography.RsaKey in project azure-sdk-for-java by Azure.

the class RsaKeyTest method testRsaOaep.

@Test
public void testRsaOaep() throws Exception {
    RsaKey key = getTestRsaKey();
    // Wrap and Unwrap
    Pair<byte[], String> wrapped = key.wrapKeyAsync(CEK, RsaOaep.ALGORITHM_NAME).get();
    byte[] unwrapped = key.unwrapKeyAsync(wrapped.getLeft(), wrapped.getRight()).get();
    // Assert
    assertEquals(RsaOaep.ALGORITHM_NAME, wrapped.getRight());
    assertArrayEquals(CEK, unwrapped);
    // Encrypt and Decrypt
    Triple<byte[], byte[], String> encrypted = key.encryptAsync(CEK, null, null, RsaOaep.ALGORITHM_NAME).get();
    byte[] decrypted = key.decryptAsync(encrypted.getLeft(), null, null, null, encrypted.getRight()).get();
    // Assert
    assertEquals(RsaOaep.ALGORITHM_NAME, encrypted.getRight());
    assertArrayEquals(CEK, decrypted);
    key.close();
}
Also used : RsaKey(com.microsoft.azure.keyvault.cryptography.RsaKey) Test(org.junit.Test)

Example 4 with RsaKey

use of com.microsoft.azure.keyvault.cryptography.RsaKey in project azure-sdk-for-java by Azure.

the class RsaKeyTest method testDefaultAlgorithm.

@Test
public void testDefaultAlgorithm() throws Exception {
    RsaKey key = getTestRsaKey();
    assertEquals(RsaOaep.ALGORITHM_NAME, key.getDefaultEncryptionAlgorithm());
    assertEquals(RsaOaep.ALGORITHM_NAME, key.getDefaultKeyWrapAlgorithm());
    assertEquals(Rs256.ALGORITHM_NAME, key.getDefaultSignatureAlgorithm());
    // Wrap and Unwrap
    Pair<byte[], String> wrapped = key.wrapKeyAsync(CEK, key.getDefaultKeyWrapAlgorithm()).get();
    byte[] unwrapped = key.unwrapKeyAsync(wrapped.getLeft(), wrapped.getRight()).get();
    // Assert
    assertEquals(RsaOaep.ALGORITHM_NAME, wrapped.getRight());
    assertArrayEquals(CEK, unwrapped);
    // Encrypt and Decrypt
    Triple<byte[], byte[], String> encrypted = key.encryptAsync(CEK, null, null, key.getDefaultEncryptionAlgorithm()).get();
    byte[] decrypted = key.decryptAsync(encrypted.getLeft(), null, null, null, encrypted.getRight()).get();
    // Assert
    assertEquals(RsaOaep.ALGORITHM_NAME, encrypted.getRight());
    assertArrayEquals(CEK, decrypted);
    key.close();
}
Also used : RsaKey(com.microsoft.azure.keyvault.cryptography.RsaKey) Test(org.junit.Test)

Example 5 with RsaKey

use of com.microsoft.azure.keyvault.cryptography.RsaKey in project azure-sdk-for-java by Azure.

the class RsaKeyTest method testSignVerify.

@Test
public void testSignVerify() throws Exception {
    RsaKey key = getTestRsaKey();
    MessageDigest digest = MessageDigest.getInstance("SHA-256");
    byte[] hash = digest.digest(CEK);
    byte[] crossPlatformHash = Base64.decodeBase64(CrossPlatformHash);
    byte[] crossPlatformSignature = Base64.decodeBase64(CrossPlatformSignature);
    // Check the hash
    assertNotNull(hash);
    assertEquals(32, hash.length);
    assertArrayEquals(hash, crossPlatformHash);
    Pair<byte[], String> signature = key.signAsync(hash, "RS256").get();
    boolean result = key.verifyAsync(hash, signature.getLeft(), "RS256").get();
    // Check the signature
    assertTrue(result);
    assertArrayEquals(crossPlatformSignature, signature.getLeft());
    // Now prove we can verify the cross platform signature
    result = key.verifyAsync(hash, Base64.decodeBase64(CrossPlatformSignature), "RS256").get();
    assertTrue(result);
    key.close();
}
Also used : RsaKey(com.microsoft.azure.keyvault.cryptography.RsaKey) MessageDigest(java.security.MessageDigest) Test(org.junit.Test)

Aggregations

RsaKey (com.microsoft.azure.keyvault.cryptography.RsaKey)5 Test (org.junit.Test)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 JsonWebKey (com.microsoft.azure.keyvault.webkey.JsonWebKey)1 MessageDigest (java.security.MessageDigest)1