use of com.microsoft.azure.keyvault.models.KeyOperationResult 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.models.KeyOperationResult 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());
}
}
use of com.microsoft.azure.keyvault.models.KeyOperationResult in project mssql-jdbc by Microsoft.
the class SQLServerColumnEncryptionAzureKeyVaultProvider method AzureKeyVaultUnWrap.
/**
* Encrypt the text using specified Azure Key Vault key.
*
* @param masterKeyPath
* - Azure Key Vault key url.
* @param encryptionAlgorithm
* - Encrypted Column Encryption Key.
* @param encryptedColumnEncryptionKey
* - Encrypted Column Encryption Key.
* @return Returns the decrypted plaintext Column Encryption Key or throws an exception if there are any errors.
* @throws SQLServerException
*/
private byte[] AzureKeyVaultUnWrap(String masterKeyPath, String encryptionAlgorithm, byte[] encryptedColumnEncryptionKey) throws SQLServerException {
if (null == encryptedColumnEncryptionKey) {
throw new SQLServerException(SQLServerException.getErrString("R_EncryptedCEKNull"), null);
}
if (0 == encryptedColumnEncryptionKey.length) {
throw new SQLServerException(SQLServerException.getErrString("R_EmptyEncryptedCEK"), null);
}
JsonWebKeyEncryptionAlgorithm jsonEncryptionAlgorithm = new JsonWebKeyEncryptionAlgorithm(encryptionAlgorithm);
KeyOperationResult unwrappedKey = keyVaultClient.unwrapKey(masterKeyPath, jsonEncryptionAlgorithm, encryptedColumnEncryptionKey);
return unwrappedKey.result();
}
use of com.microsoft.azure.keyvault.models.KeyOperationResult in project ranger by apache.
the class RangerAzureKeyVaultKeyGenerator method encryptZoneKey.
@Override
public byte[] encryptZoneKey(Key zoneKey) throws Exception {
JsonWebKeyEncryptionAlgorithm keyEncryptionAlgo = getZoneKeyEncryptionAlgo();
KeyOperationResult encryptResult = null;
if (masterKeyBundle == null) {
masterKeyBundle = keyVaultClient.getKey(keyVaultURL, azureMasterKey);
}
try {
encryptResult = keyVaultClient.encryptAsync(masterKeyBundle.key().kid(), keyEncryptionAlgo, zoneKey.getEncoded(), null).get();
} catch (Exception e) {
throw new Exception("Error while encrypting zone key." + e);
}
return encryptResult.result();
}
Aggregations