use of com.microsoft.azure.keyvault.models.KeyOperationResult in project azure-sdk-for-java by Azure.
the class KeyVaultClientImpl method signWithServiceResponseAsync.
/**
* Creates a signature from a digest using the specified key.
*
* @param vaultBaseUrl The vault name, for example https://myvault.vault.azure.net.
* @param keyName The name of the key.
* @param keyVersion The version of the key.
* @param algorithm The signing/verification algorithm identifier. For more information on possible algorithm types, see JsonWebKeySignatureAlgorithm. Possible values include: 'RS256', 'RS384', 'RS512', 'RSNULL'
* @param value the Base64Url value
* @return the observable to the KeyOperationResult object
*/
public Observable<ServiceResponse<KeyOperationResult>> signWithServiceResponseAsync(String vaultBaseUrl, String keyName, String keyVersion, JsonWebKeySignatureAlgorithm algorithm, byte[] value) {
if (vaultBaseUrl == null) {
throw new IllegalArgumentException("Parameter vaultBaseUrl is required and cannot be null.");
}
if (keyName == null) {
throw new IllegalArgumentException("Parameter keyName is required and cannot be null.");
}
if (keyVersion == null) {
throw new IllegalArgumentException("Parameter keyVersion is required and cannot be null.");
}
if (this.apiVersion() == null) {
throw new IllegalArgumentException("Parameter this.apiVersion() is required and cannot be null.");
}
if (algorithm == null) {
throw new IllegalArgumentException("Parameter algorithm is required and cannot be null.");
}
if (value == null) {
throw new IllegalArgumentException("Parameter value is required and cannot be null.");
}
KeySignParameters parameters = new KeySignParameters();
parameters.withAlgorithm(algorithm);
parameters.withValue(value);
String parameterizedHost = Joiner.on(", ").join("{vaultBaseUrl}", vaultBaseUrl);
return service.sign(keyName, keyVersion, this.apiVersion(), this.acceptLanguage(), parameters, parameterizedHost, this.userAgent()).flatMap(new Func1<Response<ResponseBody>, Observable<ServiceResponse<KeyOperationResult>>>() {
@Override
public Observable<ServiceResponse<KeyOperationResult>> call(Response<ResponseBody> response) {
try {
ServiceResponse<KeyOperationResult> clientResponse = signDelegate(response);
return Observable.just(clientResponse);
} catch (Throwable t) {
return Observable.error(t);
}
}
});
}
use of com.microsoft.azure.keyvault.models.KeyOperationResult in project azure-sdk-for-java by Azure.
the class KeyOperationsTest method checkEncryptDecryptSequence.
private void checkEncryptDecryptSequence(JsonWebKey importedKey, KeyBundle importedKeyBundle) throws Exception {
// Test variables
byte[] plainText = new byte[100];
new Random(0x1234567L).nextBytes(plainText);
byte[] cipherText;
// Encrypt in the service.
{
KeyOperationResult result = keyVaultClient.encrypt(importedKeyBundle.key().kid(), JsonWebKeyEncryptionAlgorithm.RSA_OAEP, plainText);
cipherText = result.result();
}
// Decrypt in the client, notice OAEP algorithm instance to use.
{
Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA1AndMGF1Padding");
cipher.init(Cipher.DECRYPT_MODE, importedKey.toRSA(true).getPrivate());
byte[] beforeEncrypt = plainText;
byte[] afterDecrypt = cipher.doFinal(cipherText);
Assert.assertArrayEquals(beforeEncrypt, afterDecrypt);
}
// Encrypt in the client, using the service provided material. Also use
// standard padding.
{
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.ENCRYPT_MODE, importedKeyBundle.key().toRSA().getPublic());
cipherText = cipher.doFinal(plainText);
}
// Decrypt in the service.
{
KeyOperationResult result = keyVaultClient.decrypt(importedKeyBundle.key().kid(), JsonWebKeyEncryptionAlgorithm.RSA1_5, cipherText);
byte[] beforeEncrypt = plainText;
byte[] afterDecrypt = result.result();
Assert.assertArrayEquals(beforeEncrypt, afterDecrypt);
}
}
use of com.microsoft.azure.keyvault.models.KeyOperationResult 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.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());
}
}
Aggregations