Skip to content

Commit

Permalink
DigestFactory based refactors.
Browse files Browse the repository at this point in the history
  • Loading branch information
dghgit committed Dec 22, 2016
1 parent 085e94e commit cd8ae5a
Show file tree
Hide file tree
Showing 24 changed files with 209 additions and 241 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import org.bouncycastle.crypto.DataLengthException;
import org.bouncycastle.crypto.Digest;
import org.bouncycastle.crypto.InvalidCipherTextException;
import org.bouncycastle.crypto.digests.SHA1Digest;
import org.bouncycastle.crypto.params.ParametersWithRandom;
import org.bouncycastle.crypto.util.DigestFactory;
import org.bouncycastle.util.Arrays;

/**
Expand All @@ -27,7 +27,7 @@ public class OAEPEncoding
public OAEPEncoding(
AsymmetricBlockCipher cipher)
{
this(cipher, new SHA1Digest(), null);
this(cipher, DigestFactory.createSHA1(), null);
}

public OAEPEncoding(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
import org.bouncycastle.crypto.Digest;
import org.bouncycastle.crypto.InvalidCipherTextException;
import org.bouncycastle.crypto.Wrapper;
import org.bouncycastle.crypto.digests.SHA1Digest;
import org.bouncycastle.crypto.modes.CBCBlockCipher;
import org.bouncycastle.crypto.params.KeyParameter;
import org.bouncycastle.crypto.params.ParametersWithIV;
import org.bouncycastle.crypto.params.ParametersWithRandom;
import org.bouncycastle.crypto.util.DigestFactory;
import org.bouncycastle.util.Arrays;

/**
Expand Down Expand Up @@ -51,7 +51,7 @@ public class DESedeWrapEngine
//
// checksum digest
//
Digest sha1 = new SHA1Digest();
Digest sha1 = DigestFactory.createSHA1();
byte[] digest = new byte[20];

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
import org.bouncycastle.crypto.Digest;
import org.bouncycastle.crypto.InvalidCipherTextException;
import org.bouncycastle.crypto.Wrapper;
import org.bouncycastle.crypto.digests.SHA1Digest;
import org.bouncycastle.crypto.modes.CBCBlockCipher;
import org.bouncycastle.crypto.params.ParametersWithIV;
import org.bouncycastle.crypto.params.ParametersWithRandom;
import org.bouncycastle.crypto.util.DigestFactory;
import org.bouncycastle.util.Arrays;

/**
Expand Down Expand Up @@ -43,7 +43,7 @@ public class RC2WrapEngine
//
// checksum digest
//
Digest sha1 = new SHA1Digest();
Digest sha1 = DigestFactory.createSHA1();
byte[] digest = new byte[20];

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.bouncycastle.crypto.params.DSAParameterGenerationParameters;
import org.bouncycastle.crypto.params.DSAParameters;
import org.bouncycastle.crypto.params.DSAValidationParameters;
import org.bouncycastle.crypto.util.DigestFactory;
import org.bouncycastle.util.Arrays;
import org.bouncycastle.util.BigIntegers;
import org.bouncycastle.util.encoders.Hex;
Expand All @@ -31,7 +32,7 @@ public class DSAParametersGenerator

public DSAParametersGenerator()
{
this(new SHA1Digest());
this(DigestFactory.createSHA1());
}

public DSAParametersGenerator(Digest digest)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
public class OpenSSLPBEParametersGenerator
extends PBEParametersGenerator
{
private Digest digest = DigestFactory.getMD5();
private Digest digest = DigestFactory.createMD5();

/**
* Construct a OpenSSL Parameters generator.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
import org.bouncycastle.crypto.Digest;
import org.bouncycastle.crypto.Mac;
import org.bouncycastle.crypto.PBEParametersGenerator;
import org.bouncycastle.crypto.digests.SHA1Digest;
import org.bouncycastle.crypto.macs.HMac;
import org.bouncycastle.crypto.params.KeyParameter;
import org.bouncycastle.crypto.params.ParametersWithIV;
import org.bouncycastle.crypto.util.DigestFactory;
import org.bouncycastle.util.Arrays;

/**
Expand All @@ -29,7 +29,7 @@ public class PKCS5S2ParametersGenerator
*/
public PKCS5S2ParametersGenerator()
{
this(new SHA1Digest());
this(DigestFactory.createSHA1());
}

public PKCS5S2ParametersGenerator(Digest digest)
Expand Down
37 changes: 29 additions & 8 deletions core/src/main/java/org/bouncycastle/crypto/util/DigestFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.bouncycastle.crypto.digests.SHA224Digest;
import org.bouncycastle.crypto.digests.SHA256Digest;
import org.bouncycastle.crypto.digests.SHA384Digest;
import org.bouncycastle.crypto.digests.SHA3Digest;
import org.bouncycastle.crypto.digests.SHA512Digest;
import org.bouncycastle.crypto.digests.SHA512tDigest;

Expand All @@ -14,43 +15,63 @@
*/
public final class DigestFactory
{
public static Digest getMD5()
public static Digest createMD5()
{
return new MD5Digest();
}

public static Digest getSHA1()
public static Digest createSHA1()
{
return new SHA1Digest();
}

public static Digest getSHA224()
public static Digest createSHA224()
{
return new SHA224Digest();
}

public static Digest getSHA256()
public static Digest createSHA256()
{
return new SHA256Digest();
}

public static Digest getSHA384()
public static Digest createSHA384()
{
return new SHA384Digest();
}

public static Digest getSHA512()
public static Digest createSHA512()
{
return new SHA512Digest();
}

public static Digest getSHA512_224()
public static Digest createSHA512_224()
{
return new SHA512tDigest(224);
}

public static Digest getSHA512_256()
public static Digest createSHA512_256()
{
return new SHA512tDigest(256);
}

public static Digest createSHA3_224()
{
return new SHA3Digest(224);
}

public static Digest createSHA3_256()
{
return new SHA3Digest(256);
}

public static Digest createSHA3_384()
{
return new SHA3Digest(384);
}

public static Digest createSHA3_512()
{
return new SHA3Digest(512);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.bouncycastle.crypto.InvalidCipherTextException;
import org.bouncycastle.crypto.KeyEncoder;
import org.bouncycastle.crypto.agreement.DHBasicAgreement;
import org.bouncycastle.crypto.digests.SHA1Digest;
import org.bouncycastle.crypto.engines.AESEngine;
import org.bouncycastle.crypto.engines.DESedeEngine;
import org.bouncycastle.crypto.engines.IESEngine;
Expand All @@ -43,6 +42,7 @@
import org.bouncycastle.crypto.params.IESWithCipherParameters;
import org.bouncycastle.crypto.params.ParametersWithIV;
import org.bouncycastle.crypto.parsers.DHIESPublicKeyParser;
import org.bouncycastle.crypto.util.DigestFactory;
import org.bouncycastle.jcajce.provider.asymmetric.util.DHUtil;
import org.bouncycastle.jcajce.provider.asymmetric.util.IESUtil;
import org.bouncycastle.jcajce.provider.util.BadBlockException;
Expand Down Expand Up @@ -515,8 +515,8 @@ static public class IES
public IES()
{
super(new IESEngine(new DHBasicAgreement(),
new KDF2BytesGenerator(new SHA1Digest()),
new HMac(new SHA1Digest())));
new KDF2BytesGenerator(DigestFactory.createSHA1()),
new HMac(DigestFactory.createSHA1())));
}
}

Expand All @@ -526,8 +526,8 @@ static public class IESwithDESedeCBC
public IESwithDESedeCBC()
{
super(new IESEngine(new DHBasicAgreement(),
new KDF2BytesGenerator(new SHA1Digest()),
new HMac(new SHA1Digest()),
new KDF2BytesGenerator(DigestFactory.createSHA1()),
new HMac(DigestFactory.createSHA1()),
new PaddedBufferedBlockCipher(new CBCBlockCipher(new DESedeEngine()))), 8);
}
}
Expand All @@ -538,8 +538,8 @@ static public class IESwithAESCBC
public IESwithAESCBC()
{
super(new IESEngine(new DHBasicAgreement(),
new KDF2BytesGenerator(new SHA1Digest()),
new HMac(new SHA1Digest()),
new KDF2BytesGenerator(DigestFactory.createSHA1()),
new HMac(DigestFactory.createSHA1()),
new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()))), 16);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import org.bouncycastle.crypto.DerivationFunction;
import org.bouncycastle.crypto.agreement.kdf.DHKEKGenerator;
import org.bouncycastle.crypto.digests.SHA1Digest;
import org.bouncycastle.crypto.util.DigestFactory;
import org.bouncycastle.jcajce.provider.asymmetric.util.BaseAgreementSpi;
import org.bouncycastle.jcajce.spec.UserKeyingMaterialSpec;

Expand Down Expand Up @@ -237,7 +237,7 @@ public static class DHwithRFC2631KDF
{
public DHwithRFC2631KDF()
{
super("DHwithRFC2631KDF", new DHKEKGenerator(new SHA1Digest()));
super("DHwithRFC2631KDF", new DHKEKGenerator(DigestFactory.createSHA1()));
}
}
}
Loading

0 comments on commit cd8ae5a

Please sign in to comment.