what-the-bank/sources/org/bouncycastle/jcajce/provider/symmetric/OpenSSLPBKDF.java

68 lines
3.0 KiB
Java
Raw Normal View History

2024-07-27 18:17:47 +07:00
package org.bouncycastle.jcajce.provider.symmetric;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;
import javax.crypto.SecretKey;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;
import org.bouncycastle.crypto.generators.OpenSSLPBEParametersGenerator;
import org.bouncycastle.crypto.params.KeyParameter;
import org.bouncycastle.jcajce.provider.config.ConfigurableProvider;
import org.bouncycastle.jcajce.provider.symmetric.util.BaseSecretKeyFactory;
import org.bouncycastle.jcajce.provider.util.AlgorithmProvider;
import org.bouncycastle.util.Strings;
/* loaded from: classes6.dex */
public final class OpenSSLPBKDF {
/* loaded from: classes6.dex */
public static class Mappings extends AlgorithmProvider {
private static final String PREFIX = OpenSSLPBKDF.class.getName();
@Override // org.bouncycastle.jcajce.provider.util.AlgorithmProvider
public void configure(ConfigurableProvider configurableProvider) {
StringBuilder sb = new StringBuilder();
sb.append(PREFIX);
sb.append("$PBKDF");
configurableProvider.addAlgorithm("SecretKeyFactory.PBKDF-OPENSSL", sb.toString());
}
}
/* loaded from: classes6.dex */
public static class PBKDF extends BaseSecretKeyFactory {
@Override // org.bouncycastle.jcajce.provider.symmetric.util.BaseSecretKeyFactory, javax.crypto.SecretKeyFactorySpi
public SecretKey engineGenerateSecret(KeySpec keySpec) throws InvalidKeySpecException {
if (!(keySpec instanceof PBEKeySpec)) {
throw new InvalidKeySpecException("Invalid KeySpec");
}
PBEKeySpec pBEKeySpec = (PBEKeySpec) keySpec;
if (pBEKeySpec.getSalt() == null) {
throw new InvalidKeySpecException("missing required salt");
}
if (pBEKeySpec.getIterationCount() <= 0) {
StringBuilder sb = new StringBuilder("positive iteration count required: ");
sb.append(pBEKeySpec.getIterationCount());
throw new InvalidKeySpecException(sb.toString());
}
if (pBEKeySpec.getKeyLength() <= 0) {
StringBuilder sb2 = new StringBuilder("positive key length required: ");
sb2.append(pBEKeySpec.getKeyLength());
throw new InvalidKeySpecException(sb2.toString());
}
if (pBEKeySpec.getPassword().length == 0) {
throw new IllegalArgumentException("password empty");
}
OpenSSLPBEParametersGenerator openSSLPBEParametersGenerator = new OpenSSLPBEParametersGenerator();
openSSLPBEParametersGenerator.init(Strings.toByteArray(pBEKeySpec.getPassword()), pBEKeySpec.getSalt());
return new SecretKeySpec(((KeyParameter) openSSLPBEParametersGenerator.generateDerivedParameters(pBEKeySpec.getKeyLength())).getKey(), "OpenSSLPBKDF");
}
public PBKDF() {
super("PBKDF-OpenSSL", null);
}
}
private OpenSSLPBKDF() {
}
}