You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@nifi.apache.org by GitBox <gi...@apache.org> on 2021/02/09 13:40:25 UTC

[GitHub] [nifi] Lehel44 commented on a change in pull request #4809: NIFI-7668 Implemented support for additional AEAD property encryption methods

Lehel44 commented on a change in pull request #4809:
URL: https://github.com/apache/nifi/pull/4809#discussion_r572890946



##########
File path: nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-components/src/main/java/org/apache/nifi/encrypt/KeyedCipherPropertyEncryptor.java
##########
@@ -0,0 +1,160 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.nifi.encrypt;
+
+import org.apache.nifi.security.kms.CryptoUtils;
+import org.apache.nifi.security.util.EncryptionMethod;
+import org.apache.nifi.security.util.crypto.KeyedCipherProvider;
+
+import javax.crypto.BadPaddingException;
+import javax.crypto.Cipher;
+import javax.crypto.IllegalBlockSizeException;
+import javax.crypto.SecretKey;
+import java.security.SecureRandom;
+import java.util.Arrays;
+import java.util.Objects;
+
+/**
+ * Property Encryptor implementation using Keyed Cipher Provider
+ */
+class KeyedCipherPropertyEncryptor extends CipherPropertyEncryptor {
+    private static final int INITIALIZATION_VECTOR_LENGTH = 16;
+
+    private static final int ARRAY_START = 0;
+
+    private static final boolean ENCRYPT = true;
+
+    private static final boolean DECRYPT = false;
+
+    private final SecureRandom secureRandom;
+
+    private final KeyedCipherProvider cipherProvider;
+
+    private final EncryptionMethod encryptionMethod;
+
+    private final SecretKey secretKey;
+
+    protected KeyedCipherPropertyEncryptor(final KeyedCipherProvider cipherProvider,
+                                           final EncryptionMethod encryptionMethod,
+                                           final SecretKey secretKey) {
+        this.cipherProvider = cipherProvider;
+        this.encryptionMethod = encryptionMethod;
+        this.secretKey = secretKey;
+        this.secureRandom = new SecureRandom();
+    }
+
+    /**
+     * Encrypt binary and return enciphered binary prefixed with initialization vector
+     *
+     * @param binary Binary to be encrypted
+     * @return Enciphered binary prefixed with initialization vector
+     */
+    @Override
+    protected byte[] encrypt(final byte[] binary) {
+        final byte[] initializationVector = createInitializationVector();
+        final Cipher cipher = getCipher(initializationVector, ENCRYPT);
+        try {
+            final byte[] encrypted = cipher.doFinal(binary);
+            return CryptoUtils.concatByteArrays(initializationVector, encrypted);
+        } catch (final BadPaddingException | IllegalBlockSizeException e) {
+            final String message = String.format("Encryption Failed with Algorithm [%s]", cipher.getAlgorithm());
+            throw new EncryptionException(message, e);
+        }

Review comment:
       Hi,
   This code snippet also can be found PasswordBasedCipherPropertyEncryptor. Do you think it'd worth to extract this method into their common PropertyEncryptor interface as a default one?




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org