Search in sources :

Example 16 with JsonWebKey

use of com.microsoft.azure.keyvault.webkey.JsonWebKey in project azure-sdk-for-java by Azure.

the class KeyOperationsTest method wrapUnwrapOperations.

@Test
public void wrapUnwrapOperations() throws Exception {
    JsonWebKey testKey = importTestKey();
    KeyIdentifier keyId = new KeyIdentifier(testKey.kid());
    // Test variables
    byte[] plainText = new byte[100];
    new Random(0x1234567L).nextBytes(plainText);
    byte[] cipherText;
    KeyOperationResult result;
    // wrap and unwrap using kid WO version
    {
        result = keyVaultClient.wrapKey(keyId.baseIdentifier(), JsonWebKeyEncryptionAlgorithm.RSA_OAEP, plainText);
        cipherText = result.result();
        result = keyVaultClient.unwrapKey(keyId.baseIdentifier(), JsonWebKeyEncryptionAlgorithm.RSA_OAEP, cipherText);
        Assert.assertArrayEquals(plainText, result.result());
    }
    // wrap and unwrap using full kid
    {
        result = keyVaultClient.wrapKey(testKey.kid(), JsonWebKeyEncryptionAlgorithm.RSA_OAEP, plainText);
        cipherText = result.result();
        result = keyVaultClient.unwrapKey(testKey.kid(), JsonWebKeyEncryptionAlgorithm.RSA_OAEP, cipherText);
        Assert.assertArrayEquals(plainText, result.result());
    }
}
Also used : KeyIdentifier(com.microsoft.azure.keyvault.KeyIdentifier) Random(java.util.Random) KeyOperationResult(com.microsoft.azure.keyvault.models.KeyOperationResult) JsonWebKey(com.microsoft.azure.keyvault.webkey.JsonWebKey) Test(org.junit.Test)

Example 17 with JsonWebKey

use of com.microsoft.azure.keyvault.webkey.JsonWebKey in project azure-sdk-for-java by Azure.

the class KeyOperationsTest method importTestKey.

private static JsonWebKey importTestKey() throws Exception {
    KeyBundle keyBundle = new KeyBundle();
    JsonWebKey key = JsonWebKey.fromRSA(getTestKeyMaterial());
    key.withKty(JsonWebKeyType.RSA);
    key.withKeyOps(Arrays.asList(JsonWebKeyOperation.ENCRYPT, JsonWebKeyOperation.DECRYPT, JsonWebKeyOperation.SIGN, JsonWebKeyOperation.VERIFY, JsonWebKeyOperation.WRAP_KEY, JsonWebKeyOperation.UNWRAP_KEY));
    keyBundle = keyVaultClient.importKey(new ImportKeyRequest.Builder(getVaultUri(), KEY_NAME, key).withHsm(false).build());
    validateRsaKeyBundle(keyBundle, getVaultUri(), KEY_NAME, JsonWebKeyType.RSA, null, null);
    return keyBundle.key();
}
Also used : KeyBundle(com.microsoft.azure.keyvault.models.KeyBundle) JsonWebKey(com.microsoft.azure.keyvault.webkey.JsonWebKey)

Example 18 with JsonWebKey

use of com.microsoft.azure.keyvault.webkey.JsonWebKey in project azure-sdk-for-java by Azure.

the class KeyOperationsTest method signVerifyOperations.

@Test
public void signVerifyOperations() throws Exception {
    JsonWebKey testKey = importTestKey();
    KeyIdentifier keyId = new KeyIdentifier(testKey.kid());
    // Test variables
    byte[] plainText = new byte[100];
    new Random(0x1234567L).nextBytes(plainText);
    MessageDigest md = MessageDigest.getInstance("SHA-256");
    md.update(plainText);
    byte[] digest = md.digest();
    byte[] signature;
    KeyOperationResult result;
    KeyVerifyResult verifyResult;
    // Using kid WO version
    {
        result = keyVaultClient.sign(keyId.baseIdentifier(), JsonWebKeySignatureAlgorithm.RS256, digest);
        signature = result.result();
        verifyResult = keyVaultClient.verify(keyId.baseIdentifier(), JsonWebKeySignatureAlgorithm.RS256, digest, signature);
        Assert.assertEquals(new Boolean(true), verifyResult.value());
    }
    // Using full kid
    {
        result = keyVaultClient.sign(testKey.kid(), JsonWebKeySignatureAlgorithm.RS256, digest);
        signature = result.result();
        verifyResult = keyVaultClient.verify(testKey.kid(), JsonWebKeySignatureAlgorithm.RS256, digest, signature);
        Assert.assertEquals(new Boolean(true), verifyResult.value());
    }
}
Also used : KeyIdentifier(com.microsoft.azure.keyvault.KeyIdentifier) Random(java.util.Random) KeyVerifyResult(com.microsoft.azure.keyvault.models.KeyVerifyResult) KeyOperationResult(com.microsoft.azure.keyvault.models.KeyOperationResult) JsonWebKey(com.microsoft.azure.keyvault.webkey.JsonWebKey) MessageDigest(java.security.MessageDigest) Test(org.junit.Test)

Example 19 with JsonWebKey

use of com.microsoft.azure.keyvault.webkey.JsonWebKey in project azure-sdk-for-java by Azure.

the class KeyOperationsTest method encryptDecryptOperations.

@Test
public void encryptDecryptOperations() throws Exception {
    JsonWebKey testKey = importTestKey();
    KeyIdentifier keyId = new KeyIdentifier(testKey.kid());
    // Test variables
    byte[] plainText = new byte[100];
    new Random(0x1234567L).nextBytes(plainText);
    byte[] cipherText;
    KeyOperationResult result;
    // encrypt and decrypt using kid WO version
    {
        result = keyVaultClient.encrypt(keyId.baseIdentifier(), JsonWebKeyEncryptionAlgorithm.RSA_OAEP, plainText);
        cipherText = result.result();
        result = keyVaultClient.decrypt(keyId.baseIdentifier(), JsonWebKeyEncryptionAlgorithm.RSA_OAEP, cipherText);
        Assert.assertArrayEquals(plainText, result.result());
    }
    // encrypt and decrypt using full kid
    {
        result = keyVaultClient.encrypt(testKey.kid(), JsonWebKeyEncryptionAlgorithm.RSA_OAEP, plainText);
        cipherText = result.result();
        result = keyVaultClient.decrypt(testKey.kid(), JsonWebKeyEncryptionAlgorithm.RSA_OAEP, cipherText);
        Assert.assertArrayEquals(plainText, result.result());
    }
}
Also used : KeyIdentifier(com.microsoft.azure.keyvault.KeyIdentifier) Random(java.util.Random) KeyOperationResult(com.microsoft.azure.keyvault.models.KeyOperationResult) JsonWebKey(com.microsoft.azure.keyvault.webkey.JsonWebKey) Test(org.junit.Test)

Aggregations

JsonWebKey (com.microsoft.azure.keyvault.webkey.JsonWebKey)19 Test (org.junit.Test)14 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)7 KeyIdentifier (com.microsoft.azure.keyvault.KeyIdentifier)3 KeyBundle (com.microsoft.azure.keyvault.models.KeyBundle)3 KeyOperationResult (com.microsoft.azure.keyvault.models.KeyOperationResult)3 KeyPair (java.security.KeyPair)3 Random (java.util.Random)3 JsonWebKeyOperation (com.microsoft.azure.keyvault.webkey.JsonWebKeyOperation)2 RsaKey (com.microsoft.azure.keyvault.cryptography.RsaKey)1 Attributes (com.microsoft.azure.keyvault.models.Attributes)1 KeyAttributes (com.microsoft.azure.keyvault.models.KeyAttributes)1 KeyVerifyResult (com.microsoft.azure.keyvault.models.KeyVerifyResult)1 ImportKeyRequest (com.microsoft.azure.keyvault.requests.ImportKeyRequest)1 MessageDigest (java.security.MessageDigest)1 PrivateKey (java.security.PrivateKey)1 PublicKey (java.security.PublicKey)1 HashMap (java.util.HashMap)1 KeyGenerator (javax.crypto.KeyGenerator)1 SecretKey (javax.crypto.SecretKey)1