Example usage for com.amazonaws.services.s3.model KMSEncryptionMaterials KMSEncryptionMaterials

List of usage examples for com.amazonaws.services.s3.model KMSEncryptionMaterials KMSEncryptionMaterials

Introduction

In this page you can find the example usage for com.amazonaws.services.s3.model KMSEncryptionMaterials KMSEncryptionMaterials.

Prototype

public KMSEncryptionMaterials(String defaultCustomerMasterKeyId) 

Source Link

Usage

From source file:org.apache.nifi.processors.aws.s3.encryption.service.StandardS3ClientSideEncryptionService.java

License:Apache License

private EncryptionMaterials encryptionMaterials() {
    if (!StringUtils.isBlank(kmsCmkId)) {
        return new KMSEncryptionMaterials(kmsCmkId);
    }/*from ww  w.  j a  va2 s . c o m*/

    if (!StringUtils.isBlank(secretKey)) {
        return new EncryptionMaterials(new SecretKeySpec(secretKey.getBytes(),
                secretKeyAlgorithm != null ? publicKeyAlgorithm : "RSA"));
    }

    if (!StringUtils.isBlank(publicKey) && !StringUtils.isBlank(privateKey)) {
        try {
            KeyFactory publicKeyFactory = KeyFactory
                    .getInstance(publicKeyAlgorithm != null ? publicKeyAlgorithm : "RSA");
            KeyFactory privateKeyFactory = KeyFactory
                    .getInstance(privateKeyAlgorithm != null ? privateKeyAlgorithm : "RSA");
            X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(publicKey.getBytes());
            PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(privateKey.getBytes());
            KeyPair keyPair = new KeyPair(publicKeyFactory.generatePublic(publicKeySpec),
                    privateKeyFactory.generatePrivate(privateKeySpec));
            return new EncryptionMaterials(keyPair);
        } catch (Exception e) {
            getLogger().info("Failed to create key pair based encryption materials: reason={}",
                    new Object[] { e.getMessage() });
            return null;
        }
    }

    return null;
}