public static final Digest createHash(int hashAlgorithm) { switch (hashAlgorithm) { case HashAlgorithm.md5: return new MD5Digest(); case HashAlgorithm.sha1: return new SHA1Digest(); case HashAlgorithm.sha224: return new SHA224Digest(); case HashAlgorithm.sha256: return new SHA256Digest(); case HashAlgorithm.sha384: return new SHA384Digest(); case HashAlgorithm.sha512: return new SHA512Digest(); default: throw new IllegalArgumentException("unknown HashAlgorithm"); } }
static byte[] calculateKeyId(SubjectPublicKeyInfo info) { Digest dig = new SHA1Digest(); // TODO: include definition of SHA-1 here byte[] hash = new byte[dig.getDigestSize()]; byte[] spkiEnc = new byte[0]; try { spkiEnc = info.getEncoded(ASN1Encoding.DER); } catch (IOException e) { return new byte[0]; } // try the outlook 2010 calculation dig.update(spkiEnc, 0, spkiEnc.length); dig.doFinal(hash, 0); return hash; }
/** * Construct a PKCS 12 Parameters generator. This constructor will * accept MD5, SHA1, and RIPEMD160. * * @param digest the digest to be used as the source of derived keys. * @exception IllegalArgumentException if an unknown digest is passed in. */ public OldPKCS12ParametersGenerator( Digest digest) { this.digest = digest; if (digest instanceof MD5Digest) { u = 128 / 8; v = 512 / 8; } else if (digest instanceof SHA1Digest) { u = 160 / 8; v = 512 / 8; } else if (digest instanceof RIPEMD160Digest) { u = 160 / 8; v = 512 / 8; } else { throw new IllegalArgumentException("Digest " + digest.getAlgorithmName() + " unsupported"); } }
static byte[] PRF_legacy(byte[] secret, byte[] label, byte[] labelSeed, int size) { int s_half = (secret.length + 1) / 2; byte[] s1 = new byte[s_half]; byte[] s2 = new byte[s_half]; System.arraycopy(secret, 0, s1, 0, s_half); System.arraycopy(secret, secret.length - s_half, s2, 0, s_half); byte[] b1 = new byte[size]; byte[] b2 = new byte[size]; hmac_hash(new MD5Digest(), s1, labelSeed, b1); hmac_hash(new SHA1Digest(), s2, labelSeed, b2); for (int i = 0; i < size; i++) { b1[i] ^= b2[i]; } return b1; }
public static final Digest cloneHash(int hashAlgorithm, Digest hash) { switch (hashAlgorithm) { case HashAlgorithm.md5: return new MD5Digest((MD5Digest)hash); case HashAlgorithm.sha1: return new SHA1Digest((SHA1Digest)hash); case HashAlgorithm.sha224: return new SHA224Digest((SHA224Digest)hash); case HashAlgorithm.sha256: return new SHA256Digest((SHA256Digest)hash); case HashAlgorithm.sha384: return new SHA384Digest((SHA384Digest)hash); case HashAlgorithm.sha512: return new SHA512Digest((SHA512Digest)hash); default: throw new IllegalArgumentException("unknown HashAlgorithm"); } }
protected Digest createHMACDigest(int macAlgorithm) throws IOException { switch (macAlgorithm) { case MACAlgorithm._null: return null; case MACAlgorithm.hmac_md5: return new MD5Digest(); case MACAlgorithm.hmac_sha1: return new SHA1Digest(); case MACAlgorithm.hmac_sha256: return new SHA256Digest(); case MACAlgorithm.hmac_sha384: return new SHA384Digest(); case MACAlgorithm.hmac_sha512: return new SHA512Digest(); default: throw new TlsFatalAlert(AlertDescription.internal_error); } }
/** * create an AuthorityKeyIdentifier with the GeneralNames tag and * the serial number provided as well. */ public AuthorityKeyIdentifier( SubjectPublicKeyInfo spki, GeneralNames name, BigInteger serialNumber) { Digest digest = new SHA1Digest(); byte[] resBuf = new byte[digest.getDigestSize()]; byte[] bytes = spki.getPublicKeyData().getBytes(); digest.update(bytes, 0, bytes.length); digest.doFinal(resBuf, 0); this.keyidentifier = new DEROctetString(resBuf); this.certissuer = GeneralNames.getInstance(name.toASN1Primitive()); this.certserno = new ASN1Integer(serialNumber); }
private static byte[] sign() throws Exception { RSAEngine rsa = new RSAEngine(); Digest dig = new SHA1Digest(); RSAPrivateKey privateKey = (RSAPrivateKey) getPrivate(privateKeyFilename); BigInteger big = ((RSAKey) privateKey).getModulus(); ISO9796d2Signer eng = new ISO9796d2Signer(rsa, dig, true); RSAKeyParameters rsaPriv = new RSAKeyParameters(true, big, privateKey.getPrivateExponent()); eng.init(true, rsaPriv); eng.update(message[0]); eng.update(message, 1, message.length - 1); byte[] signature = eng.generateSignature(); return signature; }
/** * Construct a PKCS 12 Parameters generator. This constructor will * accept MD5, SHA1, and RIPEMD160. * * @param digest the digest to be used as the source of derived keys. * @exception IllegalArgumentException if an unknown digest is passed in. */ public PKCS12ParametersGenerator( Digest digest) { this.digest = digest; if (digest instanceof MD5Digest) { u = 128 / 8; v = 512 / 8; } else if (digest instanceof SHA1Digest) { u = 160 / 8; v = 512 / 8; } else if (digest instanceof RIPEMD160Digest) { u = 160 / 8; v = 512 / 8; } else { throw new IllegalArgumentException("Digest " + digest.getAlgorithmName() + " unsupported"); } }
/** * create an AuthorityKeyIdentifier with the GeneralNames tag and * the serial number provided as well. */ public AuthorityKeyIdentifier( SubjectPublicKeyInfo spki, GeneralNames name, BigInteger serialNumber) { Digest digest = new SHA1Digest(); byte[] resBuf = new byte[digest.getDigestSize()]; byte[] bytes = spki.getPublicKeyData().getBytes(); digest.update(bytes, 0, bytes.length); digest.doFinal(resBuf, 0); this.keyidentifier = new DEROctetString(resBuf); this.certissuer = GeneralNames.getInstance(name.toASN1Object()); this.certserno = new DERInteger(serialNumber); }
public static Digest createHash(short hashAlgorithm) { switch (hashAlgorithm) { case HashAlgorithm.md5: return new MD5Digest(); case HashAlgorithm.sha1: return new SHA1Digest(); case HashAlgorithm.sha224: return new SHA224Digest(); case HashAlgorithm.sha256: return new SHA256Digest(); case HashAlgorithm.sha384: return new SHA384Digest(); case HashAlgorithm.sha512: return new SHA512Digest(); default: throw new IllegalArgumentException("unknown HashAlgorithm"); } }
public static Digest cloneHash(short hashAlgorithm, Digest hash) { switch (hashAlgorithm) { case HashAlgorithm.md5: return new MD5Digest((MD5Digest)hash); case HashAlgorithm.sha1: return new SHA1Digest((SHA1Digest)hash); case HashAlgorithm.sha224: return new SHA224Digest((SHA224Digest)hash); case HashAlgorithm.sha256: return new SHA256Digest((SHA256Digest)hash); case HashAlgorithm.sha384: return new SHA384Digest((SHA384Digest)hash); case HashAlgorithm.sha512: return new SHA512Digest((SHA512Digest)hash); default: throw new IllegalArgumentException("unknown HashAlgorithm"); } }
/** * create an AuthorityKeyIdentifier with the GeneralNames tag and * the serial number provided as well. * @deprecated create the extension using org.bouncycastle.cert.X509ExtensionUtils */ public AuthorityKeyIdentifier( SubjectPublicKeyInfo spki, GeneralNames name, BigInteger serialNumber) { Digest digest = new SHA1Digest(); byte[] resBuf = new byte[digest.getDigestSize()]; byte[] bytes = spki.getPublicKeyData().getBytes(); digest.update(bytes, 0, bytes.length); digest.doFinal(resBuf, 0); this.keyidentifier = new DEROctetString(resBuf); this.certissuer = GeneralNames.getInstance(name.toASN1Primitive()); this.certserno = new ASN1Integer(serialNumber); }
public void performTest() { checkMask(1, new MGF1BytesGenerator(new ShortenedDigest(new SHA256Digest(), 20)), seed1, mask1); checkMask(2, new MGF1BytesGenerator(new SHA1Digest()), seed2, mask2); checkMask(3, new MGF1BytesGenerator(new ShortenedDigest(new SHA256Digest(), 20)), seed3, mask3); try { new MGF1BytesGenerator(new SHA1Digest()).generateBytes(new byte[10], 0, 20); fail("short input array not caught"); } catch (DataLengthException e) { // expected } }
public void performTest() { checkMask(1, new KDF1BytesGenerator(new ShortenedDigest(new SHA256Digest(), 20)), seed1, mask1); checkMask(2, new KDF1BytesGenerator(new SHA1Digest()), seed2, mask2); checkMask(3, new KDF1BytesGenerator(new ShortenedDigest(new SHA256Digest(), 20)), seed3, mask3); try { new KDF1BytesGenerator(new SHA1Digest()).generateBytes(new byte[10], 0, 20); fail("short input array not caught"); } catch (DataLengthException e) { // expected } }
private void shouldPassSignatureTest1() throws Exception { BigInteger n = new BigInteger("c9be1b28f8caccca65d86cc3c9bbcc13eccc059df3b80bd2292b811eff3aa0dd75e1e85c333b8e3fa9bed53bb20f5359ff4e6900c5e9a388e3a4772a583a79e2299c76582c2b27694b65e9ba22e66bfb817f8b70b22206d7d8ae488c86dbb7137c26d5eff9b33c90e6cee640630313b7a715802e15142fef498c404a8de19674974785f0f852e2d470fe85a2e54ffca9f5851f672b71df691785a5cdabe8f14aa628942147de7593b2cf962414a5b59c632c4e14f1768c0ab2e9250824beea60a3529f11bf5e070ce90a47686eb0be1086fb21f0827f55295b4a48307db0b048c05a4aec3f488c576ca6f1879d354224c7e84cbcd8e76dd217a3de54dba73c35", 16); BigInteger e = new BigInteger("e75b1b", 16); byte[] msg = Hex.decode("5bb0d1c0ef9b5c7af2477fe08d45523d3842a4b2db943f7033126c2a7829bacb3d2cfc6497ec91688189e81b7f8742488224ba320ce983ce9480722f2cc5bc42611f00bb6311884f660ccc244788378673532edb05284fd92e83f6f6dab406209032e6af9a33c998677933e32d6fb95fd27408940d7728f9c9c40267ca1d20ce"); byte[] sig = Hex.decode("0fe8bb8e3109a1eb7489ef35bf4c1a0780071da789c8bd226a4170538eafefdd30b732d628f0e87a0b9450051feae9754d4fb61f57862d10f0bacc4f660d13281d0cd1141c006ade5186ff7d961a4c6cd0a4b352fc1295c5afd088f80ac1f8e192ef116a010a442655fe8ff5eeacea15807906fb0f0dfa86e680d4c005872357f7ece9aa4e20b15d5f709b30f08648ecaa34f2fbf54eb6b414fa2ff6f87561f70163235e69ccb4ac82a2e46d3be214cc2ef5263b569b2d8fd839b21a9e102665105ea762bda25bb446cfd831487a6b846100dee113ae95ae64f4af22c428c87bab809541c962bb3a56d4c86588e0af4ebc7fcc66dadced311051356d3ea745f7"); RSAKeyParameters rsaPublic = new RSAKeyParameters(false, n, e); X931Signer signer = new X931Signer(new RSAEngine(), new SHA1Digest()); signer.init(false, rsaPublic); signer.update(msg, 0, msg.length); if (!signer.verifySignature(sig)) { fail("RSA X931 verify test 1 failed."); } }
public void performTest() { checkMask(1, new KDF2BytesGenerator(new ShortenedDigest(new SHA256Digest(), 20)), seed1, mask1); checkMask(2, new KDF2BytesGenerator(new ShortenedDigest(new SHA256Digest(), 20)), seed2, mask2); checkMask(3, new KDF2BytesGenerator(new SHA256Digest()), seed2, adjustedMask2); checkMask(4, new KDF2BytesGenerator(new SHA1Digest()), seed2, sha1Mask); checkMask(5, new KDF2BytesGenerator(new SHA1Digest()), seed3, mask3); checkMask(6, new KDF2BytesGenerator(new SHA1Digest()), seed4, mask4); try { new KDF2BytesGenerator(new SHA1Digest()).generateBytes(new byte[10], 0, 20); fail("short input array not caught"); } catch (DataLengthException e) { // expected } }
static byte[] calculateKeyBlock_SSL(byte[] master_secret, byte[] random, int size) { Digest md5 = new MD5Digest(); Digest sha1 = new SHA1Digest(); int md5Size = md5.getDigestSize(); byte[] shatmp = new byte[sha1.getDigestSize()]; byte[] tmp = new byte[size + md5Size]; int i = 0, pos = 0; while (pos < size) { byte[] ssl3Const = SSL3_CONST[i]; sha1.update(ssl3Const, 0, ssl3Const.length); sha1.update(master_secret, 0, master_secret.length); sha1.update(random, 0, random.length); sha1.doFinal(shatmp, 0); md5.update(master_secret, 0, master_secret.length); md5.update(shatmp, 0, shatmp.length); md5.doFinal(tmp, pos); pos += md5Size; ++i; } byte rval[] = new byte[size]; System.arraycopy(tmp, 0, rval, 0, size); return rval; }
static byte[] calculateMasterSecret_SSL(byte[] pre_master_secret, byte[] random) { Digest md5 = new MD5Digest(); Digest sha1 = new SHA1Digest(); int md5Size = md5.getDigestSize(); byte[] shatmp = new byte[sha1.getDigestSize()]; byte[] rval = new byte[md5Size * 3]; int pos = 0; for (int i = 0; i < 3; ++i) { byte[] ssl3Const = SSL3_CONST[i]; sha1.update(ssl3Const, 0, ssl3Const.length); sha1.update(pre_master_secret, 0, pre_master_secret.length); sha1.update(random, 0, random.length); sha1.doFinal(shatmp, 0); md5.update(pre_master_secret, 0, pre_master_secret.length); md5.update(shatmp, 0, shatmp.length); md5.doFinal(rval, pos); pos += md5Size; } return rval; }
private static byte[] getDigest(SubjectPublicKeyInfo spki) { Digest digest = new SHA1Digest(); byte[] resBuf = new byte[digest.getDigestSize()]; byte[] bytes = spki.getPublicKeyData().getBytes(); digest.update(bytes, 0, bytes.length); digest.doFinal(resBuf, 0); return resBuf; }
public static Digest getDigest( String digestName) { digestName = Strings.toUpperCase(digestName); if (sha1.contains(digestName)) { return new SHA1Digest(); } if (md5.contains(digestName)) { return new MD5Digest(); } if (sha224.contains(digestName)) { return new SHA224Digest(); } if (sha256.contains(digestName)) { return new SHA256Digest(); } if (sha384.contains(digestName)) { return new SHA384Digest(); } if (sha512.contains(digestName)) { return new SHA512Digest(); } return null; }
public Object clone() throws CloneNotSupportedException { Digest d = (Digest)super.clone(); d.digest = new SHA1Digest((SHA1Digest)digest); return d; }
public IESwithDESede() { super(new IESEngine(new DHBasicAgreement(), new KDF2BytesGenerator(new SHA1Digest()), new HMac(new SHA1Digest()), new PaddedBufferedBlockCipher(new DESedeEngine()))); }
public IESwithAES() { super(new IESEngine(new DHBasicAgreement(), new KDF2BytesGenerator(new SHA1Digest()), new HMac(new SHA1Digest()), new PaddedBufferedBlockCipher(new AESEngine()))); }
public ECIESwithDESede() { super(new IESEngine(new ECDHBasicAgreement(), new KDF2BytesGenerator(new SHA1Digest()), new HMac(new SHA1Digest()), new PaddedBufferedBlockCipher(new DESedeEngine()))); }
public ECIESwithAES() { super(new IESEngine(new ECDHBasicAgreement(), new KDF2BytesGenerator(new SHA1Digest()), new HMac(new SHA1Digest()), new PaddedBufferedBlockCipher(new AESEngine()))); }
public IES() { super(new IESEngine( new DHBasicAgreement(), new KDF2BytesGenerator(new SHA1Digest()), new HMac(new SHA1Digest()))); }
public void engineStore(OutputStream stream, char[] password) throws IOException { Cipher cipher; DataOutputStream dOut = new DataOutputStream(stream); byte[] salt = new byte[STORE_SALT_SIZE]; int iterationCount = MIN_ITERATIONS + (random.nextInt() & 0x3ff); random.nextBytes(salt); dOut.writeInt(version); dOut.writeInt(salt.length); dOut.write(salt); dOut.writeInt(iterationCount); cipher = this.makePBECipher(STORE_CIPHER, Cipher.ENCRYPT_MODE, password, salt, iterationCount); CipherOutputStream cOut = new CipherOutputStream(dOut, cipher); DigestOutputStream dgOut = new DigestOutputStream(new SHA1Digest()); this.saveStore(new TeeOutputStream(cOut, dgOut)); byte[] dig = dgOut.getDigest(); cOut.write(dig); cOut.close(); }
private static ConcurrentBag<ConcurrentBagEntry<Digest>> getMD5MessageDigests() { ConcurrentBag<ConcurrentBagEntry<Digest>> mds = new ConcurrentBag<>(); for (int i = 0; i < PARALLELISM; i++) { Digest md = new SHA1Digest(); mds.add(new ConcurrentBagEntry<>(md)); } return mds; }
private static String verify() throws Exception { RSAEngine engine = new RSAEngine(); Digest digest = new SHA1Digest(); RSAPublicKey publicKey = (RSAPublicKey) getPublic(publicKeyFilename); BigInteger big = ((RSAKey) publicKey).getModulus(); RSAKeyParameters rsaPublic = new RSAKeyParameters(false, big, publicKey.getPublicExponent()); ISO9796d2Signer verifier = new ISO9796d2Signer(engine, digest, true); verifier.init(false, rsaPublic); // false for verify if (!verifier.verifySignature(signature)) { System.err.println("Signature was modified, could not verify correctness!"); return ""; } String recoveredMessage = ""; try { if (verifier.hasFullMessage()) { verifier.updateWithRecoveredMessage(signature); } byte[] message = verifier.getRecoveredMessage(); recoveredMessage = new String(message, "UTF-8"); } catch (Exception exception) { System.err.println("Recover failed!"); } return recoveredMessage; }
public ECIES() { super(new IESEngine( new ECDHBasicAgreement(), new KDF2BytesGenerator(new SHA1Digest()), new HMac(new SHA1Digest()))); }
/** * * Calulates the keyidentifier using a SHA1 hash over the BIT STRING * from SubjectPublicKeyInfo as defined in RFC2459. * **/ public SubjectKeyIdentifier( SubjectPublicKeyInfo spki) { Digest digest = new SHA1Digest(); byte[] resBuf = new byte[digest.getDigestSize()]; byte[] bytes = spki.getPublicKeyData().getBytes(); digest.update(bytes, 0, bytes.length); digest.doFinal(resBuf, 0); this.keyidentifier=resBuf; }
public OldECIESwithCipher(BlockCipher baseCipher, int ivLength) { super(new OldIESEngine(new ECDHBasicAgreement(), new KDF2BytesGenerator(new SHA1Digest()), new HMac(new SHA1Digest()), new PaddedBufferedBlockCipher(baseCipher)), ivLength); }
static Digest createDigest(int algorithm) throws PGPException { switch (algorithm) { case HashAlgorithmTags.SHA1: return new SHA1Digest(); case HashAlgorithmTags.SHA224: return new SHA224Digest(); case HashAlgorithmTags.SHA256: return new SHA256Digest(); case HashAlgorithmTags.SHA384: return new SHA384Digest(); case HashAlgorithmTags.SHA512: return new SHA512Digest(); case HashAlgorithmTags.MD2: return new MD2Digest(); case HashAlgorithmTags.MD5: return new MD5Digest(); case HashAlgorithmTags.RIPEMD160: return new RIPEMD160Digest(); case HashAlgorithmTags.TIGER_192: return new TigerDigest(); default: throw new PGPException("cannot recognise digest"); } }
private void testHashRandom() { DRBGTestVector tv = new DRBGTestVector( new SHA1Digest(), new SHA1EntropyProvider().get(440), true, "2021222324", 80, new String[] { "532CA1165DCFF21C55592687639884AF4BC4B057DF8F41DE653AB44E2ADEC7C9303E75ABE277EDBF", "73C2C67C696D686D0C4DBCEB5C2AF7DDF6F020B6874FAE4390F102117ECAAFF54418529A367005A0" }) .setPersonalizationString("404142434445464748494A4B4C4D4E4F505152535455565758595A5B5C5D5E5F606162636465666768696A6B6C6D6E6F70717273747576"); doHashTest(0, tv); tv = new DRBGTestVector( new SHA1Digest(), new SHA1EntropyProvider().get(440), false, "2021222324", 80, new String[] { "AB438BD3B01A0AF85CFEE29F7D7B71621C4908B909124D430E7B406FB1086EA994C582E0D656D989", "29D9098F987E7005314A0F51B3DD2B8122F4AED706735DE6AD5DDBF223177C1E5F3AEBC52FAB90B9" }) .setPersonalizationString("404142434445464748494A4B4C4D4E4F505152535455565758595A5B5C5D5E5F606162636465666768696A6B6C6D6E6F70717273747576"); doHashTest(1, tv); }
private void testHMACRandom() { DRBGTestVector tv = new DRBGTestVector( new SHA1Digest(), new SHA1EntropyProvider().get(440), true, "2021222324", 80, new String[] { "6C37FDD729AA40F80BC6AB08CA7CC649794F6998B57081E4220F22C5C283E2C91B8E305AB869C625", "CAF57DCFEA393B9236BF691FA456FEA7FDF1DF8361482CA54D5FA723F4C88B4FA504BF03277FA783" }) .setPersonalizationString("404142434445464748494A4B4C4D4E4F505152535455565758595A5B5C5D5E5F606162636465666768696A6B6C6D6E6F70717273747576"); doHMACTest(tv); tv = new DRBGTestVector( new SHA1Digest(), new SHA1EntropyProvider().get(440), false, "2021222324", 80, new String[] { "5A7D3B449F481CB38DF79AD2B1FCC01E57F8135E8C0B22CD0630BFB0127FB5408C8EFC17A929896E", "82cf772ec3e84b00fc74f5df104efbfb2428554e9ce367d03aeade37827fa8e9cb6a08196115d948" }); doHMACTest(tv); }