himanshug commented on a change in pull request #8992: druid extension for OpenID Connect auth using pac4j lib URL: https://github.com/apache/druid/pull/8992#discussion_r396814002
########## File path: core/src/main/java/org/apache/druid/crypto/CryptoService.java ########## @@ -0,0 +1,215 @@ +/* + * 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.druid.crypto; + +import com.google.common.base.Preconditions; +import org.apache.druid.java.util.common.StringUtils; + +import javax.annotation.Nullable; +import javax.crypto.BadPaddingException; +import javax.crypto.Cipher; +import javax.crypto.IllegalBlockSizeException; +import javax.crypto.NoSuchPaddingException; +import javax.crypto.SecretKey; +import javax.crypto.SecretKeyFactory; +import javax.crypto.spec.IvParameterSpec; +import javax.crypto.spec.PBEKeySpec; +import javax.crypto.spec.SecretKeySpec; +import java.nio.ByteBuffer; +import java.security.InvalidAlgorithmParameterException; +import java.security.InvalidKeyException; +import java.security.NoSuchAlgorithmException; +import java.security.SecureRandom; +import java.security.spec.InvalidKeySpecException; +import java.security.spec.InvalidParameterSpecException; +import java.security.spec.KeySpec; + +/** + * Utility class for symmetric key encryption (i.e. same secret is used for encryption and decryption) of byte[] + * using javax.crypto package. + * + * To learn about possible algorithms supported and their names, + * See https://docs.oracle.com/javase/8/docs/technotes/guides/security/StandardNames.html + */ +public class CryptoService +{ + // User provided secret phrase used for encrypting data + private final char[] passPhrase; + + // Variables for algorithm used to generate a SecretKey based on user provided passPhrase + private final String secretKeyFactoryAlg; + private final int saltSize; + private final int iterationCount; + private final int keyLength; + + // Cipher algorithm information + private final String cipherAlgName; + private final String cipherAlgMode; + private final String cipherAlgPadding; + + // transformation = "cipherAlgName/cipherAlgMode/cipherAlgPadding" used in Cipher.getInstance(transformation) + private final String transformation; + + public CryptoService( + String passPhrase, + @Nullable String cipherAlgName, + @Nullable String cipherAlgMode, + @Nullable String cipherAlgPadding, + @Nullable String secretKeyFactoryAlg, + @Nullable Integer saltSize, + @Nullable Integer iterationCount, + @Nullable Integer keyLength + ) + { + Preconditions.checkArgument( + passPhrase != null && !passPhrase.isEmpty(), + "null/empty passPhrase" + ); + this.passPhrase = passPhrase.toCharArray(); + + this.cipherAlgName = cipherAlgName == null ? "AES" : cipherAlgName; + this.cipherAlgMode = cipherAlgMode == null ? "CBC" : cipherAlgMode; + this.cipherAlgPadding = cipherAlgPadding == null ? "PKCS5Padding" : cipherAlgPadding; + this.transformation = StringUtils.format("%s/%s/%s", this.cipherAlgName, this.cipherAlgMode, this.cipherAlgPadding); + + this.secretKeyFactoryAlg = secretKeyFactoryAlg == null ? "PBKDF2WithHmacSHA256" : secretKeyFactoryAlg; + this.saltSize = saltSize == null ? 8 : saltSize; + this.iterationCount = iterationCount == null ? 65536 : iterationCount; + this.keyLength = keyLength == null ? 128 : keyLength; + + // encrypt/decrypt a test string to ensure all params are valid + String testString = "duh! !! !!!"; + Preconditions.checkState( + testString.equals(StringUtils.fromUtf8(decrypt(encrypt(StringUtils.toUtf8(testString))))), + "decrypt(encrypt(testString)) failed" + ); + } + + public byte[] encrypt(byte[] plain) + { + try { + byte[] salt = new byte[saltSize]; + SecureRandom rnd = new SecureRandom(); Review comment: yeah, changed ---------------------------------------------------------------- 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: [email protected] With regards, Apache Git Services --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
