Github user alopresto commented on a diff in the pull request:

    https://github.com/apache/nifi/pull/1686#discussion_r113066789
  
    --- Diff: 
nifi-commons/nifi-data-provenance-utils/src/main/java/org/apache/nifi/provenance/CryptoUtils.java
 ---
    @@ -0,0 +1,228 @@
    +/*
    + * 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.provenance;
    +
    +import java.io.BufferedReader;
    +import java.io.ByteArrayOutputStream;
    +import java.io.File;
    +import java.io.FileReader;
    +import java.io.IOException;
    +import java.security.KeyManagementException;
    +import java.security.NoSuchAlgorithmException;
    +import java.util.Arrays;
    +import java.util.Base64;
    +import java.util.HashMap;
    +import java.util.Map;
    +import java.util.regex.Pattern;
    +import javax.crypto.BadPaddingException;
    +import javax.crypto.Cipher;
    +import javax.crypto.IllegalBlockSizeException;
    +import javax.crypto.SecretKey;
    +import javax.crypto.spec.SecretKeySpec;
    +import org.apache.commons.lang3.StringUtils;
    +import org.apache.nifi.security.util.EncryptionMethod;
    +import org.apache.nifi.security.util.crypto.AESKeyedCipherProvider;
    +import org.apache.nifi.util.NiFiProperties;
    +import org.bouncycastle.util.encoders.Hex;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +public class CryptoUtils {
    +    private static final Logger logger = 
LoggerFactory.getLogger(StaticKeyProvider.class);
    +    private static final String STATIC_KEY_PROVIDER_CLASS_NAME = 
"org.apache.nifi.provenance.StaticKeyProvider";
    +    private static final String FILE_BASED_KEY_PROVIDER_CLASS_NAME = 
"org.apache.nifi.provenance.FileBasedKeyProvider";
    +    private static final Pattern HEX_PATTERN = 
Pattern.compile("(?i)^[0-9a-f]+$");
    +
    +    public static final int IV_LENGTH = 16;
    +
    +    public static boolean isUnlimitedStrengthCryptoAvailable() {
    +        try {
    +            return Cipher.getMaxAllowedKeyLength("AES") > 128;
    +        } catch (NoSuchAlgorithmException e) {
    +            logger.warn("Tried to determine if unlimited strength crypto 
is available but the AES algorithm is not available");
    +            return false;
    +        }
    +    }
    +
    +    /**
    +     * Utility method which returns true if the string is null, empty, or 
entirely whitespace.
    +     *
    +     * @param src the string to evaluate
    +     * @return true if empty
    +     */
    +    public static boolean isEmpty(String src) {
    +        return src == null || src.trim().isEmpty();
    +    }
    +
    +    /**
    +     * Concatenates multiple byte[] into a single byte[]. Because it uses 
a {@link ByteArrayOutputStream}
    +     * rather than {@link System#arraycopy(Object, int, Object, int, int)} 
the performance is much better
    +     * with an arbitrary number of input byte[]s.
    +     *
    +     * @param arrays the component byte[] in order
    +     * @return a concatenated byte[]
    +     * @throws IOException this should never be thrown
    +     */
    +    public static byte[] concatByteArrays(byte[]... arrays) throws 
IOException {
    +        ByteArrayOutputStream boas = new ByteArrayOutputStream();
    --- End diff --
    
    Yep, looks like with the higher iteration count the array copy is faster. 
    
    ```
    Ran 1000 iterations in 34702232 ns to warm up the JVM
    Calculating small/small -- 2 arrays with avg length 5
    Ran 1000000 of small/small (traditional) with a total wall time of 
1339514066 ns and average run of 131 ns
    Ran 1000000 of small/small (BAOS) with a total wall time of 1008624897 ns 
and average run of 83 ns
    Calculating small/large -- 5 arrays with avg length 484
    Ran 1000000 of small/large (traditional) with a total wall time of 
1699096744 ns and average run of 805 ns
    Ran 1000000 of small/large (BAOS) with a total wall time of 2978547636 ns 
and average run of 1975 ns
    Calculating large/small -- 173 arrays with avg length 8
    Ran 1000000 of large/small (traditional) with a total wall time of 
2692231144 ns and average run of 1521 ns
    Ran 1000000 of large/small (BAOS) with a total wall time of 3514575357 ns 
and average run of 2582 ns
    Calculating large/large -- 147 arrays with avg length 579
    Ran 1000000 of large/large (traditional) with a total wall time of 
16443696576 ns and average run of 15418 ns
    Ran 1000000 of large/large (BAOS) with a total wall time of 61356984740 ns 
and average run of 59737 ns
    ```


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

Reply via email to