abhishekagarwal87 commented on code in PR #15648: URL: https://github.com/apache/druid/pull/15648#discussion_r1446276888
########## extensions-core/druid-basic-security/src/main/java/org/apache/druid/security/basic/authentication/validator/PasswordHashGenerator.java: ########## @@ -0,0 +1,156 @@ +/* + * 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.security.basic.authentication.validator; + +import com.google.common.cache.Cache; +import com.google.common.cache.CacheBuilder; +import com.google.common.hash.Hashing; +import org.apache.druid.error.DruidException; +import org.apache.druid.java.util.common.RE; +import org.apache.druid.java.util.common.StringUtils; +import org.apache.druid.java.util.common.logger.Logger; +import org.apache.druid.security.basic.BasicAuthUtils; + +import javax.crypto.SecretKey; +import javax.crypto.SecretKeyFactory; +import javax.crypto.spec.PBEKeySpec; +import java.security.NoSuchAlgorithmException; +import java.security.spec.InvalidKeySpecException; +import java.time.Duration; +import java.util.Arrays; +import java.util.Objects; +import java.util.concurrent.ExecutionException; + +/** + * Generates a hash of passwords using the {@link #HASH_ALGORITHM}. Multiple + * iterations may be used to enhance security of the generated hash. + * <p> + * Hashes once computed are cached for an hour so that they need not be recomputed + * for every API invocation. + */ +public class PasswordHashGenerator +{ + private static final Logger log = new Logger(PasswordHashGenerator.class); + + public static final int KEY_LENGTH = 512; + public static final String HASH_ALGORITHM = "PBKDF2WithHmacSHA512"; + + /** + * Salt used to compute a quick sha-256 hash of the password for the cache. + */ + private final byte[] shaSalt = BasicAuthUtils.generateSalt(); + + private final Cache<CacheKey, byte[]> cache = CacheBuilder.newBuilder() + .maximumSize(1000) + .expireAfterAccess(Duration.ofMinutes(60)) + .build(); + + /** + * Hashes the given password using the {@link #HASH_ALGORITHM}. + */ + public byte[] getOrComputePasswordHash(char[] password, byte[] salt, int numIterations) Review Comment: did you run some local test to how much runs-per-second you get with "getOrComputePasswordHash" and with "computePasswordHash"? Just to make sure that we do see perf gains. By benchmark, I really mean just a simple test method calling these functions in a loop -- 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. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
