public EncryptionMaterials onEncrypt()

in src/main/java/software/amazon/encryption/s3/materials/S3Keyring.java [55:87]


    public EncryptionMaterials onEncrypt(EncryptionMaterials materials) {
        EncryptDataKeyStrategy encryptStrategy = encryptDataKeyStrategy();

        // Allow encrypt strategy to modify the materials if necessary
        materials = encryptStrategy.modifyMaterials(materials);

        if (materials.plaintextDataKey() == null) {
            materials = generateDataKeyStrategy().generateDataKey(materials);
        }

        // Return materials if they already have an encrypted data key.
        if (!materials.encryptedDataKeys().isEmpty()) {
            return materials;
        }

        try {
            byte[] encryptedDataKeyCiphertext = encryptStrategy.encryptDataKey(_secureRandom, materials);
            EncryptedDataKey encryptedDataKey = EncryptedDataKey.builder()
                    .keyProviderId(S3Keyring.KEY_PROVIDER_ID)
                    .keyProviderInfo(encryptStrategy.keyProviderInfo().getBytes(StandardCharsets.UTF_8))
                    .encryptedDataKey(encryptedDataKeyCiphertext)
                    .build();

            List<EncryptedDataKey> encryptedDataKeys = new ArrayList<>(materials.encryptedDataKeys());
            encryptedDataKeys.add(encryptedDataKey);

            return materials.toBuilder()
                    .encryptedDataKeys(encryptedDataKeys)
                    .build();
        } catch (Exception e) {
            throw new S3EncryptionClientException("Unable to " + encryptStrategy.keyProviderInfo() + " wrap", e);
        }
    }