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());
}
}
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();
}
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());
}
}
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());
}
}
Aggregations