Search in sources :

Example 1 with DSAParameterGenerationParameters

use of com.github.zhenwei.core.crypto.params.DSAParameterGenerationParameters in project LinLong-Java by zhenwei1108.

the class AlgorithmParameterGeneratorSpi method engineGenerateParameters.

protected AlgorithmParameters engineGenerateParameters() {
    DSAParametersGenerator pGen;
    if (strength <= 1024) {
        pGen = new DSAParametersGenerator();
    } else {
        pGen = new DSAParametersGenerator(new SHA256Digest());
    }
    if (random == null) {
        random = CryptoServicesRegistrar.getSecureRandom();
    }
    int certainty = PrimeCertaintyCalculator.getDefaultCertainty(strength);
    if (strength == 1024) {
        params = new DSAParameterGenerationParameters(1024, 160, certainty, random);
        pGen.init(params);
    } else if (strength > 1024) {
        params = new DSAParameterGenerationParameters(strength, 256, certainty, random);
        pGen.init(params);
    } else {
        pGen.init(strength, certainty, random);
    }
    DSAParameters p = pGen.generateParameters();
    AlgorithmParameters params;
    try {
        params = createParametersInstance("DSA");
        params.init(new DSAParameterSpec(p.getP(), p.getQ(), p.getG()));
    } catch (Exception e) {
        throw new RuntimeException(e.getMessage());
    }
    return params;
}
Also used : DSAParametersGenerator(com.github.zhenwei.core.crypto.generators.DSAParametersGenerator) DSAParameterSpec(java.security.spec.DSAParameterSpec) SHA256Digest(com.github.zhenwei.core.crypto.digests.SHA256Digest) DSAParameterGenerationParameters(com.github.zhenwei.core.crypto.params.DSAParameterGenerationParameters) DSAParameters(com.github.zhenwei.core.crypto.params.DSAParameters) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) InvalidParameterException(java.security.InvalidParameterException) AlgorithmParameters(java.security.AlgorithmParameters)

Example 2 with DSAParameterGenerationParameters

use of com.github.zhenwei.core.crypto.params.DSAParameterGenerationParameters in project LinLong-Java by zhenwei1108.

the class KeyPairGeneratorSpi method generateKeyPair.

public KeyPair generateKeyPair() {
    if (!initialised) {
        Integer paramStrength = Integers.valueOf(strength);
        if (params.containsKey(paramStrength)) {
            param = (DSAKeyGenerationParameters) params.get(paramStrength);
        } else {
            synchronized (lock) {
                // our key size.
                if (params.containsKey(paramStrength)) {
                    param = (DSAKeyGenerationParameters) params.get(paramStrength);
                } else {
                    DSAParametersGenerator pGen;
                    DSAParameterGenerationParameters dsaParams;
                    int certainty = PrimeCertaintyCalculator.getDefaultCertainty(strength);
                    // For legacy keysize that is less than 1024-bit, we just use the 186-2 style parameters
                    if (strength == 1024) {
                        pGen = new DSAParametersGenerator();
                        if (Properties.isOverrideSet("org.bouncycastle.dsa.FIPS186-2for1024bits")) {
                            pGen.init(strength, certainty, random);
                        } else {
                            dsaParams = new DSAParameterGenerationParameters(1024, 160, certainty, random);
                            pGen.init(dsaParams);
                        }
                    } else if (strength > 1024) {
                        dsaParams = new DSAParameterGenerationParameters(strength, 256, certainty, random);
                        pGen = new DSAParametersGenerator(new SHA256Digest());
                        pGen.init(dsaParams);
                    } else {
                        pGen = new DSAParametersGenerator();
                        pGen.init(strength, certainty, random);
                    }
                    param = new DSAKeyGenerationParameters(random, pGen.generateParameters());
                    params.put(paramStrength, param);
                }
            }
        }
        engine.init(param);
        initialised = true;
    }
    AsymmetricCipherKeyPair pair = engine.generateKeyPair();
    DSAPublicKeyParameters pub = (DSAPublicKeyParameters) pair.getPublic();
    DSAPrivateKeyParameters priv = (DSAPrivateKeyParameters) pair.getPrivate();
    return new KeyPair(new BCDSAPublicKey(pub), new BCDSAPrivateKey(priv));
}
Also used : DSAParametersGenerator(com.github.zhenwei.core.crypto.generators.DSAParametersGenerator) DSAPublicKeyParameters(com.github.zhenwei.core.crypto.params.DSAPublicKeyParameters) KeyPair(java.security.KeyPair) AsymmetricCipherKeyPair(com.github.zhenwei.core.crypto.AsymmetricCipherKeyPair) SHA256Digest(com.github.zhenwei.core.crypto.digests.SHA256Digest) DSAParameterGenerationParameters(com.github.zhenwei.core.crypto.params.DSAParameterGenerationParameters) DSAKeyGenerationParameters(com.github.zhenwei.core.crypto.params.DSAKeyGenerationParameters) DSAPrivateKeyParameters(com.github.zhenwei.core.crypto.params.DSAPrivateKeyParameters) AsymmetricCipherKeyPair(com.github.zhenwei.core.crypto.AsymmetricCipherKeyPair)

Aggregations

SHA256Digest (com.github.zhenwei.core.crypto.digests.SHA256Digest)2 DSAParametersGenerator (com.github.zhenwei.core.crypto.generators.DSAParametersGenerator)2 DSAParameterGenerationParameters (com.github.zhenwei.core.crypto.params.DSAParameterGenerationParameters)2 AsymmetricCipherKeyPair (com.github.zhenwei.core.crypto.AsymmetricCipherKeyPair)1 DSAKeyGenerationParameters (com.github.zhenwei.core.crypto.params.DSAKeyGenerationParameters)1 DSAParameters (com.github.zhenwei.core.crypto.params.DSAParameters)1 DSAPrivateKeyParameters (com.github.zhenwei.core.crypto.params.DSAPrivateKeyParameters)1 DSAPublicKeyParameters (com.github.zhenwei.core.crypto.params.DSAPublicKeyParameters)1 AlgorithmParameters (java.security.AlgorithmParameters)1 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)1 InvalidParameterException (java.security.InvalidParameterException)1 KeyPair (java.security.KeyPair)1 DSAParameterSpec (java.security.spec.DSAParameterSpec)1