List of usage examples for org.bouncycastle.crypto.digests SHA1Digest SHA1Digest
public SHA1Digest()
From source file:org.ethereum.crypto.ECIESCoder.java
License:Open Source License
/** * Encryption equivalent to the Crypto++ default ECIES<ECP> settings: * * DL_KeyAgreementAlgorithm: DL_KeyAgreementAlgorithm_DH<struct ECPPoint,struct EnumToType<enum CofactorMultiplicationOption,0> > * DL_KeyDerivationAlgorithm: DL_KeyDerivationAlgorithm_P1363<struct ECPPoint,0,class P1363_KDF2<class SHA1> > * DL_SymmetricEncryptionAlgorithm: DL_EncryptionAlgorithm_Xor<class HMAC<class SHA1>,0> * DL_PrivateKey: DL_Key<ECPPoint> * DL_PrivateKey_EC<class ECP>//from w w w . j a v a 2s . c om * * Used for Whisper V3 */ public static byte[] decryptSimple(BigInteger privKey, byte[] cipher) throws IOException, InvalidCipherTextException { EthereumIESEngine iesEngine = new EthereumIESEngine(new ECDHBasicAgreement(), new MGF1BytesGeneratorExt(new SHA1Digest(), 1), new HMac(new SHA1Digest()), new SHA1Digest(), null); IESParameters p = new IESParameters(null, null, KEY_SIZE); ParametersWithIV parametersWithIV = new ParametersWithIV(p, new byte[0]); iesEngine.setHashMacKey(false); iesEngine.init(new ECPrivateKeyParameters(privKey, CURVE), parametersWithIV, new ECIESPublicKeyParser(ECKey.CURVE)); return iesEngine.processBlock(cipher, 0, cipher.length); }
From source file:org.fnppl.opensdx.security.SecurityHelper.java
License:Open Source License
public static byte[] getSHA1LocalProof(Vector<Element> ve) throws Exception { byte[] ret = new byte[20];//160 bit = 20 byte SHA1Digest sha1 = new SHA1Digest(); //System.out.println("--- sha1localproof ---"); for (Element e : ve) { rekursiveUpdateSHA1(sha1, e);/* ww w . ja v a2s. co m*/ } sha1.doFinal(ret, 0); //System.out.println("--- RESULT ----"); //System.out.println(SecurityHelper.HexDecoder.encode(ret, ':',-1)); return ret; }
From source file:org.forgerock.openicf.framework.remote.security.ECIESEncryptor.java
License:Open Source License
protected IESEngine initialiseEngine(boolean encrypt, CipherParameters privateKey, CipherParameters publicKey, IESParameters parameters) {/*w w w. jav a 2s.com*/ IESEngine engine = new IESEngine(new ECDHBasicAgreement(), new KDF2BytesGenerator(new SHA1Digest()), new HMac(new SHA1Digest())); engine.init(encrypt, privateKey, publicKey, parameters); return engine; }
From source file:org.jboss.totp.Token.java
License:Apache License
public Token(String name, byte hmacAlgorithm, int timeStep, String key, byte passCodeLength, int timeDelta) { this.name = name; this.hmacAlgorithm = hmacAlgorithm; this.timeStep = timeStep; this.key = key; this.passCodeLength = passCodeLength; this.timeDelta = timeDelta; Digest digest = null;/* ww w .j a v a2s .com*/ switch (hmacAlgorithm) { case HMACAlgorithm.SHA_1: digest = new SHA1Digest(); break; case HMACAlgorithm.SHA_256: digest = new SHA256Digest(); break; case HMACAlgorithm.SHA_512: digest = new SHA512Digest(); break; } hmac = new HMac(digest); byte[] secret = Base32.decode(key); hmac.init(new KeyParameter(secret)); }
From source file:org.jboss.totp.TOTPMIDlet.java
License:Apache License
private boolean displayToken(Display display) { final String warning = validateInput(); if (warning.length() == 0) { siProfile.setText(tfProfile.getString()); final int algorithmIdx = chgHmacAlgorithm.getSelectedIndex(); final byte[] secretKey = base32Decode(tfSecret.getString()); HMac newHmac = null;/*from ww w . j ava 2 s.co m*/ if (secretKey != null) { Digest digest = null; if (SHA1.equals(HMAC_ALGORITHMS[algorithmIdx])) { digest = new SHA1Digest(); } else if (SHA256.equals(HMAC_ALGORITHMS[algorithmIdx])) { digest = new SHA256Digest(); } else if (SHA512.equals(HMAC_ALGORITHMS[algorithmIdx])) { digest = new SHA512Digest(); } newHmac = new HMac(digest); newHmac.init(new KeyParameter(secretKey)); } setHMac(newHmac); refreshTokenTask.run(); display.setCurrent(fMain); return true; } else { displayAlert("Invalid input:\n" + warning, fOptions); return false; } }
From source file:org.jboss.totp.TOTPMIDlet.java
License:Apache License
private boolean displayTokenFromURI(Display display) { OtpauthUri uri = null;// w w w . j a v a 2s. c o m try { uri = OtpauthUri.parse(tfOtpauthUri.getString()); } catch (InvalidUriException e) { displayAlert("Invalid URI:\n" + e.getMessage(), fOptions); return false; } String profileName = uri.getIssuer() != null ? uri.getIssuer() + ":" + uri.getLabel() : uri.getLabel(); tfProfile.setString(profileName); siProfile.setText(tfProfile.getString()); tfSecret.setString(uri.getSecret()); Digest digest = null; HMac newHmac = null; if (uri.getAlgorithm() != null) { if (uri.getAlgorithm().toLowerCase().equals("sha1")) { chgHmacAlgorithm.setSelectedIndex(0, true); digest = new SHA1Digest(); } else if (uri.getAlgorithm().toLowerCase().equals("sha256")) { chgHmacAlgorithm.setSelectedIndex(1, true); digest = new SHA256Digest(); } else if (uri.getAlgorithm().toLowerCase().equals("sha512")) { chgHmacAlgorithm.setSelectedIndex(2, true); digest = new SHA512Digest(); } } else { digest = new SHA1Digest(); } if (uri.getPeriod() != null) { tfTimeStep.setString(String.valueOf(uri.getPeriod())); } if (uri.getDigits() != null) { tfDigits.setString(String.valueOf(uri.getDigits())); } final String warning = validateInput(); if (warning.length() > 0) { displayAlert("Invalid input:\n" + warning, fOptions); return false; } newHmac = new HMac(digest); newHmac.init(new KeyParameter(base32Decode(uri.getSecret()))); setHMac(newHmac); refreshTokenTask.run(); display.setCurrent(fMain); return true; }
From source file:org.jclouds.encryption.bouncycastle.BouncyCastleEncryptionService.java
License:Apache License
public String hmacSha1Base64(String toEncode, byte[] key) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidKeyException { Digest digest = new SHA1Digest(); return hmacBase64(toEncode, key, digest); }
From source file:org.jclouds.http.HttpUtils.java
License:Apache License
public static String hmacSha1Base64(String toEncode, byte[] key) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidKeyException { Digest digest = new SHA1Digest(); return hmacBase64(toEncode, key, digest); }
From source file:org.jcryptool.visual.hashing.views.HashingView.java
License:Open Source License
private String computeHash(String hashName, String inputText, Text hashText) { hash = hash.getName(hashName);/* w w w.j a va2 s . com*/ byte[] digest = null; switch (hash) { case MD2: MD2Digest md2 = new MD2Digest(); md2.update(inputText.getBytes(), 0, inputText.getBytes().length); digest = new byte[md2.getDigestSize()]; md2.doFinal(digest, 0); break; case MD4: MD4Digest md4 = new MD4Digest(); md4.update(inputText.getBytes(), 0, inputText.getBytes().length); digest = new byte[md4.getDigestSize()]; md4.doFinal(digest, 0); break; case MD5: MD5Digest md5 = new MD5Digest(); md5.update(inputText.getBytes(), 0, inputText.getBytes().length); digest = new byte[md5.getDigestSize()]; md5.doFinal(digest, 0); break; case SHA1: SHA1Digest sha1 = new SHA1Digest(); sha1.update(inputText.getBytes(), 0, inputText.getBytes().length); digest = new byte[sha1.getDigestSize()]; sha1.doFinal(digest, 0); break; case SHA256: SHA256Digest sha256 = new SHA256Digest(); sha256.update(inputText.getBytes(), 0, inputText.getBytes().length); digest = new byte[sha256.getDigestSize()]; sha256.doFinal(digest, 0); break; case SHA512: SHA512Digest sha512 = new SHA512Digest(); sha512.update(inputText.getBytes(), 0, inputText.getBytes().length); digest = new byte[sha512.getDigestSize()]; sha512.doFinal(digest, 0); break; case SHA3_224: SHA3.Digest224 sha3_224 = new SHA3.Digest224(); sha3_224.update(inputText.getBytes(), 0, inputText.getBytes().length); digest = new byte[sha3_224.getDigestLength()]; digest = sha3_224.digest(); break; case SHA3_256: SHA3.Digest256 sha3_256 = new SHA3.Digest256(); sha3_256.update(inputText.getBytes(), 0, inputText.getBytes().length); digest = new byte[sha3_256.getDigestLength()]; digest = sha3_256.digest(); break; case SHA3_384: SHA3.Digest384 sha3_384 = new SHA3.Digest384(); sha3_384.update(inputText.getBytes(), 0, inputText.getBytes().length); digest = new byte[sha3_384.getDigestLength()]; digest = sha3_384.digest(); break; case SHA3_512: SHA3.Digest512 sha3_512 = new SHA3.Digest512(); sha3_512.update(inputText.getBytes(), 0, inputText.getBytes().length); digest = new byte[sha3_512.getDigestLength()]; digest = sha3_512.digest(); break; case SKEIN_256: Skein.Digest_256_256 skein_256 = new Skein.Digest_256_256(); skein_256.update(inputText.getBytes(), 0, inputText.getBytes().length); digest = new byte[skein_256.getDigestLength()]; digest = skein_256.digest(); break; case SKEIN_512: Skein.Digest_512_512 skein_512 = new Skein.Digest_512_512(); skein_512.update(inputText.getBytes(), 0, inputText.getBytes().length); digest = new byte[skein_512.getDigestLength()]; digest = skein_512.digest(); break; case SKEIN_1024: Skein.Digest_1024_1024 skein_1024 = new Skein.Digest_1024_1024(); skein_1024.update(inputText.getBytes(), 0, inputText.getBytes().length); digest = new byte[skein_1024.getDigestLength()]; digest = skein_1024.digest(); break; case RIPEMD160: RIPEMD160Digest ripemd160 = new RIPEMD160Digest(); ripemd160.update(inputText.getBytes(), 0, inputText.getBytes().length); digest = new byte[ripemd160.getDigestSize()]; ripemd160.doFinal(digest, 0); break; case SM3: SM3Digest sm3 = new SM3Digest(); sm3.update(inputText.getBytes(), 0, inputText.getBytes().length); digest = new byte[sm3.getDigestSize()]; sm3.doFinal(digest, 0); break; case TIGER: TigerDigest tiger = new TigerDigest(); tiger.update(inputText.getBytes(), 0, inputText.getBytes().length); digest = new byte[tiger.getDigestSize()]; tiger.doFinal(digest, 0); break; case GOST3411: GOST3411Digest gost3411 = new GOST3411Digest(); gost3411.update(inputText.getBytes(), 0, inputText.getBytes().length); digest = new byte[gost3411.getDigestSize()]; gost3411.doFinal(digest, 0); break; case WHIRLPOOL: WhirlpoolDigest whirlpool = new WhirlpoolDigest(); whirlpool.update(inputText.getBytes(), 0, inputText.getBytes().length); digest = new byte[whirlpool.getDigestSize()]; whirlpool.doFinal(digest, 0); break; default: break; } String hashHexValue = new String(Hex.encode(digest)); if (btnHexadezimal.getSelection()) { String hashValueOutput = hashHexValue.toUpperCase().replaceAll(".{2}", "$0 "); //$NON-NLS-1$ //$NON-NLS-2$ hashText.setText(hashValueOutput); } else if (btnDezimal.getSelection()) { String hashValue = hexToDecimal(hashHexValue); hashValue = hashValue.replaceAll(".{3}", "$0 "); //$NON-NLS-1$ //$NON-NLS-2$ hashText.setText(hashValue); } else if (btnBinary.getSelection()) { String hashValue = hexToBinary(hashHexValue); hashValue = hashValue.replaceAll(".{8}", "$0#"); //$NON-NLS-1$ //$NON-NLS-2$ hashText.setText(hashValue); } return hashHexValue; }
From source file:org.jitsi.impl.neomedia.transform.srtp.CryptoBenchmark.java
License:LGPL
public static void main(String[] args) throws Exception { boolean benchmarkJavaxCryptoCipher = false; boolean benchmarkNIOBlockCipher = false; for (String arg : args) { if ("-javax-crypto-cipher".equalsIgnoreCase(arg)) benchmarkJavaxCryptoCipher = true; else if ("-nio-block-cipher".equalsIgnoreCase(arg)) benchmarkNIOBlockCipher = true; }/* www . j av a2 s . c o m*/ Provider sunPKCS11 = new sun.security.pkcs11.SunPKCS11( "--name=CryptoBenchmark\\n" + "nssDbMode=noDb\\n" + "attributes=compatibility"); Provider sunJCE = Security.getProvider("SunJCE"); // for (Provider provider : new Provider[] { sunPKCS11, sunJCE }) // for (Provider.Service service : provider.getServices()) // if ("Cipher".equalsIgnoreCase(service.getType())) // System.err.println(service); // org.bouncycastle.crypto.Digest & java.security.MessageDigest Digest[] digests = { new SHA1Digest(), new OpenSSLDigest(OpenSSLDigest.SHA1) }; MessageDigest[] messageDigests = { MessageDigest.getInstance("SHA-1"), MessageDigest.getInstance("SHA-1", sunPKCS11) }; int maxDigestSize = 0; int maxByteLength = 0; for (Digest digest : digests) { int digestSize = digest.getDigestSize(); if (maxDigestSize < digestSize) maxDigestSize = digestSize; int byteLength = (digest instanceof ExtendedDigest) ? ((ExtendedDigest) digest).getByteLength() : 64; if (maxByteLength < byteLength) maxByteLength = byteLength; System.err.println(digest.getClass().getName() + ": digestSize " + digestSize + ", byteLength " + byteLength + "."); } for (MessageDigest messageDigest : messageDigests) { int digestLength = messageDigest.getDigestLength(); if (maxDigestSize < digestLength) maxDigestSize = digestLength; System.err.println( messageDigest.getProvider().getClass().getName() + ": digestLength " + digestLength + "."); } // org.bouncycastle.crypto.BlockCipher BlockCipher[] ciphers = { new AESFastEngine(), new BlockCipherAdapter(Cipher.getInstance("AES_128/ECB/NoPadding", sunPKCS11)), new BlockCipherAdapter(Cipher.getInstance("AES_128/ECB/NoPadding", sunJCE)), new OpenSSLBlockCipher(OpenSSLBlockCipher.AES_128_ECB) }; for (BlockCipher cipher : ciphers) { Class<?> clazz; if (cipher instanceof BlockCipherAdapter) { clazz = ((BlockCipherAdapter) cipher).getCipher().getProvider().getClass(); } else { clazz = cipher.getClass(); } System.err.println(clazz.getName() + ": blockSize " + cipher.getBlockSize()); } // org.bouncycastle.crypto.Mac Mac[] macs = { new HMac(new SHA1Digest()), new HMac(new OpenSSLDigest(OpenSSLDigest.SHA1)), new OpenSSLHMAC(OpenSSLDigest.SHA1) }; Random random = new Random(System.currentTimeMillis()); byte[] in = new byte[1024 * maxByteLength]; ByteBuffer inNIO = ByteBuffer.allocateDirect(in.length); byte[] out = new byte[maxDigestSize]; ByteBuffer outNIO = ByteBuffer.allocateDirect(out.length); long time0 = 0; int dMax = Math.max(digests.length, messageDigests.length); final int iEnd = 1000, jEnd = 1000; // Base64.Encoder byteEncoder = Base64.getEncoder().withoutPadding(); inNIO.order(ByteOrder.nativeOrder()); outNIO.order(ByteOrder.nativeOrder()); for (int i = 0; i < iEnd; ++i) { System.err.println("========================================"); random.nextBytes(in); inNIO.clear(); inNIO.put(in); // org.bouncycastle.crypto.BlockCipher time0 = 0; for (BlockCipher blockCipher : ciphers) { NIOBlockCipher nioBlockCipher = (blockCipher instanceof NIOBlockCipher) ? (NIOBlockCipher) blockCipher : null; Cipher cipher; Class<?> clazz; if (blockCipher instanceof BlockCipherAdapter) { cipher = ((BlockCipherAdapter) blockCipher).getCipher(); clazz = cipher.getProvider().getClass(); } else { cipher = null; clazz = blockCipher.getClass(); } int blockSize = blockCipher.getBlockSize(); blockCipher.init(true, new KeyParameter(in, 0, blockSize)); long startTime, endTime; int offEnd = in.length - blockSize; if (nioBlockCipher != null && benchmarkNIOBlockCipher) { inNIO.clear(); outNIO.clear(); startTime = System.nanoTime(); for (int j = 0; j < jEnd; ++j) { for (int off = 0; off < offEnd;) { nioBlockCipher.processBlock(inNIO, off, outNIO, 0); off += blockSize; } // nioBlockCipher.reset(); } endTime = System.nanoTime(); outNIO.get(out); } else if (cipher != null && benchmarkJavaxCryptoCipher) { startTime = System.nanoTime(); for (int j = 0; j < jEnd; ++j) { for (int off = 0; off < offEnd;) { int nextOff = off + blockSize; inNIO.limit(nextOff); inNIO.position(off); outNIO.clear(); cipher.update(inNIO, outNIO); off = nextOff; } // cipher.doFinal(); } endTime = System.nanoTime(); outNIO.clear(); outNIO.get(out); } else { startTime = System.nanoTime(); for (int j = 0; j < jEnd; ++j) { for (int off = 0; off < offEnd;) { blockCipher.processBlock(in, off, out, 0); off += blockSize; } // blockCipher.reset(); } endTime = System.nanoTime(); } long time = endTime - startTime; if (time0 == 0) time0 = time; Arrays.fill(out, blockSize, out.length, (byte) 0); System.err.println(clazz.getName() + ": ratio " + String.format("%.2f", time / (double) time0) + ", time " + time + ", out " /*+ byteEncoder.encodeToString(out)*/ + "."); } // org.bouncycastle.crypto.Digest & java.security.MessageDigest System.err.println("----------------------------------------"); time0 = 0; for (int d = 0; d < dMax; ++d) { Arrays.fill(out, (byte) 0); // org.bouncycastle.crypto.Digest Digest digest = (d < digests.length) ? digests[d] : null; int byteLength = (digest instanceof ExtendedDigest) ? ((ExtendedDigest) digest).getByteLength() : 64; long startTime, endTime; int offEnd = in.length - byteLength; if (digest != null) { startTime = System.nanoTime(); for (int j = 0; j < jEnd; ++j) { for (int off = 0; off < offEnd;) { digest.update(in, off, byteLength); off += byteLength; } digest.doFinal(out, 0); } endTime = System.nanoTime(); long time = endTime - startTime; if (time0 == 0) time0 = time; System.err.println(digest.getClass().getName() + ": ratio " + String.format("%.2f", time / (double) time0) + ", time " + time + ", digest " /*+ byteEncoder.encodeToString(out)*/ + "."); } // java.security.MessageDigest MessageDigest messageDigest = (d < messageDigests.length) ? messageDigests[d] : null; if (messageDigest != null) { @SuppressWarnings("unused") byte[] t = null; startTime = System.nanoTime(); for (int j = 0; j < jEnd; ++j) { for (int off = 0; off < offEnd;) { messageDigest.update(in, off, byteLength); off += byteLength; } t = messageDigest.digest(); } endTime = System.nanoTime(); long time = endTime - startTime; if (time0 == 0) time0 = time; System.err.println(messageDigest.getProvider().getClass().getName() + ": ratio " + String.format("%.2f", time / (double) time0) + ", time " + (endTime - startTime) + ", digest " /*+ byteEncoder.encodeToString(t)*/ + "."); } } // org.bouncycastle.crypto.Mac System.err.println("----------------------------------------"); time0 = 0; for (Mac mac : macs) { mac.init(new KeyParameter(in, 0, maxByteLength)); long startTime, endTime; int offEnd = in.length - maxByteLength; startTime = System.nanoTime(); for (int j = 0; j < jEnd; ++j) { for (int off = 0; off < offEnd; off = off + maxByteLength) mac.update(in, off, maxByteLength); mac.doFinal(out, 0); } endTime = System.nanoTime(); int macSize = mac.getMacSize(); long time = endTime - startTime; if (time0 == 0) time0 = time; Arrays.fill(out, macSize, out.length, (byte) 0); System.err.println(mac.getClass().getName() + ": ratio " + String.format("%.2f", time / (double) time0) + ", time " + time + ", out " /*+ byteEncoder.encodeToString(out)*/ + "."); } } }