List of usage examples for org.bouncycastle.crypto.macs HMac HMac
public HMac(Digest digest)
From source file:ECIESTest.java
public TestResult perform() { SecureRandom random = new SecureRandom(); ECCurve.Fp curve = new ECCurve.Fp( new BigInteger("883423532389192164791648750360308885314476597252960362792450860609699839"), // q new BigInteger("7fffffffffffffffffffffff7fffffffffff8000000000007ffffffffffc", 16), // a new BigInteger("6b016c3bdcf18941d0d654921475ca71a9db2fb27d1d37796185c2942c0a", 16)); // b ECDomainParameters params = new ECDomainParameters(curve, curve.decodePoint(Hex.decode("020ffa963cdca8816ccc33b8642bedf905c3d358573d3f27fbbd3b3cb9aaaf")), // G new BigInteger("883423532389192164791648750360308884807550341691627752275345424702807307")); // n ECKeyPairGenerator pGen = new ECKeyPairGenerator(); ECKeyGenerationParameters genParam = new ECKeyGenerationParameters(params, random); pGen.init(genParam);// w ww .ja va 2 s . c om AsymmetricCipherKeyPair p1 = pGen.generateKeyPair(); AsymmetricCipherKeyPair p2 = pGen.generateKeyPair(); // // stream test // IESEngine i1 = new IESEngine(new ECDHBasicAgreement(), new KDF2BytesGenerator(new SHA1Digest()), new HMac(new SHA1Digest())); IESEngine i2 = new IESEngine(new ECDHBasicAgreement(), new KDF2BytesGenerator(new SHA1Digest()), new HMac(new SHA1Digest())); byte[] d = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }; byte[] e = new byte[] { 8, 7, 6, 5, 4, 3, 2, 1 }; IESParameters p = new IESParameters(d, e, 64); i1.init(true, p1.getPrivate(), p2.getPublic(), p); i2.init(false, p2.getPrivate(), p1.getPublic(), p); byte[] message = Hex.decode("1234567890abcdef"); try { byte[] out1 = i1.processBlock(message, 0, message.length); byte[] out2 = i2.processBlock(out1, 0, out1.length); if (!sameAs(out2, message)) { return new SimpleTestResult(false, this.getName() + ": stream cipher test failed"); } } catch (Exception ex) { return new SimpleTestResult(false, this.getName() + ": stream cipher test exception " + ex.toString()); } // // twofish with IV0 test // BufferedBlockCipher c1 = new PaddedBufferedBlockCipher(new CBCBlockCipher(new TwofishEngine())); BufferedBlockCipher c2 = new PaddedBufferedBlockCipher(new CBCBlockCipher(new TwofishEngine())); i1 = new IESEngine(new ECDHBasicAgreement(), new KDF2BytesGenerator(new SHA1Digest()), new HMac(new SHA1Digest()), c1); i2 = new IESEngine(new ECDHBasicAgreement(), new KDF2BytesGenerator(new SHA1Digest()), new HMac(new SHA1Digest()), c2); d = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 }; e = new byte[] { 8, 7, 6, 5, 4, 3, 2, 1 }; p = new IESWithCipherParameters(d, e, 64, 128); i1.init(true, p1.getPrivate(), p2.getPublic(), p); i2.init(false, p2.getPrivate(), p1.getPublic(), p); message = Hex.decode("1234567890abcdef"); try { byte[] out1 = i1.processBlock(message, 0, message.length); byte[] out2 = i2.processBlock(out1, 0, out1.length); if (!sameAs(out2, message)) { return new SimpleTestResult(false, this.getName() + ": twofish cipher test failed"); } } catch (Exception ex) { return new SimpleTestResult(false, this.getName() + ": twofish cipher test exception " + ex.toString()); } return new SimpleTestResult(true, this.getName() + ": Okay"); }
From source file:com.completetrsst.crypto.Crypto.java
License:Apache License
private static byte[] _cryptIES(byte[] input, Key recipient, boolean forEncryption) throws InvalidKeyException, IllegalBlockSizeException, BadPaddingException { IESCipher cipher = new IESCipher(new IESEngine(new ECDHBasicAgreement(), new KDF2BytesGenerator(new SHA1Digest()), new HMac(new SHA256Digest()), new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine())))); cipher.engineInit(forEncryption ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE, recipient, new SecureRandom()); return cipher.engineDoFinal(input, 0, input.length); }
From source file:com.distrimind.util.crypto.BCMac.java
License:Open Source License
public void init(org.bouncycastle.crypto.SymmetricSecretKey _key) throws NoSuchAlgorithmException { Digest d;//from w w w.j av a 2s. c o m if (type.getCodeProviderForSignature() == CodeProvider.BC) { switch (type.getMessageDigestType()) { case BC_FIPS_SHA3_256: d = new SHA3Digest(256); break; case BC_FIPS_SHA3_384: d = new SHA3Digest(384); break; case BC_FIPS_SHA3_512: d = new SHA3Digest(512); break; case BC_BLAKE2B_160: case BC_BLAKE2B_256: case BC_BLAKE2B_384: case BC_BLAKE2B_512: d = new Blake2bDigest(type.getMessageDigestType().getDigestLengthInBits()); break; default: throw new NoSuchAlgorithmException(type.toString()); } } else { throw new NoSuchAlgorithmException(type.toString()); } mac = new HMac(d); mac.init(new KeyParameter((secretKey = _key).getKeyBytes())); reset(); }
From source file:com.DSC.crypto.Cipher.java
License:Open Source License
/** * //w w w.j a v a 2 s . co m * @param priKey * @param pubKey * @param passphrase * @param data * @return * @throws InvalidCipherTextException */ public static byte[] encryptKey(CipherParameters priKey, CipherParameters pubKey, String passphrase, byte[] data) throws InvalidCipherTextException { /* Initialize IESEngine in stream mode */ IESEngine engine = new IESEngine(new ECDHCBasicAgreement(), new KDF2BytesGenerator(new SHA256Digest()), new HMac(new SHA256Digest())); /* Set the IESEngine cipher parameters as the passphrase and passphrase reversed */ IESParameters param = new IESParameters(passphrase.getBytes(), new StringBuilder(passphrase).reverse().toString().getBytes(), engine.getMac().getMacSize() * 8); /* Initialize the engine and encrypt the key */ engine.init(true, priKey, pubKey, param); return engine.processBlock(data, 0, data.length); }
From source file:com.DSC.crypto.Cipher.java
License:Open Source License
/** * //from w w w. j av a2 s .c o m * @param priKey * @param pubKey * @param passphrase * @param data * @return * @throws InvalidCipherTextException */ public static byte[] decryptKey(CipherParameters priKey, CipherParameters pubKey, String passphrase, byte[] data) throws InvalidCipherTextException { /* IESEngine in stream mode */ IESEngine engine = new IESEngine(new ECDHCBasicAgreement(), new KDF2BytesGenerator(new SHA256Digest()), new HMac(new SHA256Digest())); /* Set the IESEngine cipher parameters as the passphrase and passphrase reversed */ IESParameters param = new IESParameters(passphrase.getBytes(), new StringBuilder(passphrase).reverse().toString().getBytes(), engine.getMac().getMacSize() * 8); /* Initialize the engine and decrypt the key */ engine.init(false, priKey, pubKey, param); return engine.processBlock(data, 0, data.length); }
From source file:com.DSC.crypto.Cipher.java
License:Open Source License
/** * /*from w w w . java 2s. c o m*/ * @param passphrase * @param data * @return */ public static BigInteger[] generateHMAC(String passphrase, byte[] data) { HMac hmac = new HMac(new MD5Digest()); byte[] buf = new byte[hmac.getMacSize()]; BigInteger[] hmacBigInt = new BigInteger[1]; /* Initializes and generate HMAC for message */ hmac.init(new KeyParameter(passphrase.getBytes())); hmac.update(data, 0, data.length); hmac.doFinal(buf, 0); /* Convert the HMAC to a big integer representation */ hmacBigInt[0] = new BigInteger(buf); return hmacBigInt; }
From source file:com.DSC.crypto.Cipher.java
License:Open Source License
/** * //from w w w . j a v a2 s .c o m * @param passphrase * @param HMAC * @param data * @return * @throws InvalidCipherTextException */ public static boolean verifyHMAC(String passphrase, BigInteger[] HMAC, byte[] data) throws InvalidCipherTextException { HMac hmac = new HMac(new MD5Digest()); byte[] expHMAC = new byte[hmac.getMacSize()]; byte[] recHMAC = new byte[hmac.getMacSize()]; /* Initializes and generate the expected HMAC for message */ hmac.init(new KeyParameter(passphrase.getBytes())); hmac.update(data, 0, data.length); hmac.doFinal(expHMAC, 0); /* Convert the received HMAC to a byte representation */ recHMAC = HMAC[0].toByteArray(); /* Compare the HMAC received to the expected HMAC */ if (Arrays.equals(expHMAC, recHMAC)) { return true; } else { throw new InvalidCipherTextException("Message HMAC failed!"); } }
From source file:com.entertailion.android.shapeways.api.Request.java
License:Apache License
/** * Generate HMAC-SHA1 for message// ww w . j a v a 2 s.c o m * * @param key * @param message * @return * @throws Exception */ private static String generateHmac(String key, String message) throws Exception { Log.d(LOG_TAG, "generateHmac: " + key + "=" + message); byte[] keyBytes = key.getBytes(ShapewaysClient.ENCODING); byte[] data = message.getBytes(ShapewaysClient.ENCODING); HMac macProvider = new HMac(new SHA1Digest()); macProvider.init(new KeyParameter(keyBytes)); macProvider.reset(); macProvider.update(data, 0, data.length); byte[] output = new byte[macProvider.getMacSize()]; macProvider.doFinal(output, 0); byte[] hmac = Base64.encode(output); return new String(hmac).replaceAll("\r\n", ""); }
From source file:com.github.horrorho.inflatabledonkey.crypto.NISTKDF.java
License:Open Source License
public static byte[] ctrHMac(byte[] keyDerivationKey, byte[] label, Supplier<Digest> digestSupplier, int keyLengthBytes) { logger.trace("<< ctrHMac() - keyDerivationKey: 0x{} label: {} digestSupplier: {} length: {}", Hex.toHexString(keyDerivationKey), Hex.toHexString(label), digestSupplier, keyLengthBytes); byte[] derivedKey = new byte[keyLengthBytes]; // fixedInputData = label || 0x00 || dkLen in bits as 4 bytes big endian ByteBuffer buffer = ByteBuffer.allocate(label.length + 5); buffer.put(label);//from ww w. j av a 2 s. com buffer.put((byte) 0); buffer.putInt(keyLengthBytes * 8); byte[] fixedInputData = buffer.array(); logger.debug("-- ctrHMac() - fixed input data: 0x{}", Hex.toHexString(fixedInputData)); HMac hMac = new HMac(digestSupplier.get()); KDFCounterBytesGenerator generator = new KDFCounterBytesGenerator(hMac); generator.init(new KDFCounterParameters(keyDerivationKey, fixedInputData, R)); generator.generateBytes(derivedKey, 0, derivedKey.length); logger.trace(">> ctrHMac() - derivedKey: 0x{}", Hex.toHexString(derivedKey)); return derivedKey; }
From source file:com.github.jinahya.rfc5849.OAuthSignatureHmacSha1Bc.java
License:Apache License
@Override byte[] get(final byte[] keyBytes, final byte[] baseBytes) throws Exception { final Mac mac = new HMac(new SHA1Digest()); mac.init(new KeyParameter(keyBytes)); mac.update(baseBytes, 0, baseBytes.length); final byte[] output = new byte[mac.getMacSize()]; mac.doFinal(output, 0);// w w w . j av a 2 s .co m return output; }