org.spongycastle.crypto.macs.HMac Java Examples

The following examples show how to use org.spongycastle.crypto.macs.HMac. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ECIESCoder.java    From wkcwallet-java with Apache License 2.0 6 votes vote down vote up
public static byte[] decrypt(ECPoint ephem, BigInteger prv, byte[] IV, byte[] cipher, byte[] macData) throws InvalidCipherTextException {
    AESFastEngine aesFastEngine = new AESFastEngine();

    EthereumIESEngine iesEngine = new EthereumIESEngine(
            new ECDHBasicAgreement(),
            new ConcatKDFBytesGenerator(new SHA256Digest()),
            new HMac(new SHA256Digest()),
            new SHA256Digest(),
            new BufferedBlockCipher(new SICBlockCipher(aesFastEngine)));


    byte[]         d = new byte[] {};
    byte[]         e = new byte[] {};

    IESParameters p = new IESWithCipherParameters(d, e, KEY_SIZE, KEY_SIZE);
    ParametersWithIV parametersWithIV =
            new ParametersWithIV(p, IV);

    iesEngine.init(false, new ECPrivateKeyParameters(prv, CURVE), new ECPublicKeyParameters(ephem, CURVE), parametersWithIV);

    return iesEngine.processBlock(cipher, 0, cipher.length, macData);
}
 
Example #2
Source File: ECIESCoder.java    From wkcwallet-java with Apache License 2.0 6 votes vote down vote up
/**
 *  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>
 *
 *  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);
}
 
Example #3
Source File: ECIESCoder.java    From wkcwallet-java with Apache License 2.0 6 votes vote down vote up
private static EthereumIESEngine makeIESEngine(boolean isEncrypt, ECPoint pub, BigInteger prv, byte[] IV) {
    AESFastEngine aesFastEngine = new AESFastEngine();

    EthereumIESEngine iesEngine = new EthereumIESEngine(
            new ECDHBasicAgreement(),
            new ConcatKDFBytesGenerator(new SHA256Digest()),
            new HMac(new SHA256Digest()),
            new SHA256Digest(),
            new BufferedBlockCipher(new SICBlockCipher(aesFastEngine)));


    byte[]         d = new byte[] {};
    byte[]         e = new byte[] {};

    IESParameters p = new IESWithCipherParameters(d, e, KEY_SIZE, KEY_SIZE);
    ParametersWithIV parametersWithIV = new ParametersWithIV(p, IV);

    iesEngine.init(isEncrypt, new ECPrivateKeyParameters(prv, CURVE), new ECPublicKeyParameters(pub, CURVE), parametersWithIV);
    return iesEngine;
}
 
Example #4
Source File: ECIESCoder.java    From wkcwallet-java with Apache License 2.0 5 votes vote down vote up
/**
     *  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>
     *
     *  Used for Whisper V3
     */
    public static byte[] encryptSimple(ECPoint pub, byte[] plaintext) 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);

        ECKeyPairGenerator eGen = new ECKeyPairGenerator();
        SecureRandom random = new SecureRandom();
        KeyGenerationParameters gParam = new ECKeyGenerationParameters(CURVE, random);
        eGen.init(gParam);

//        AsymmetricCipherKeyPairGenerator testGen = new AsymmetricCipherKeyPairGenerator() {
//            ECKey priv = ECKey.fromPrivate(Hex.decode("d0b043b4c5d657670778242d82d68a29d25d7d711127d17b8e299f156dad361a"));
//
//            @Override
//            public void init(KeyGenerationParameters keyGenerationParameters) {
//            }
//
//            @Override
//            public AsymmetricCipherKeyPair generateKeyPair() {
//                return new AsymmetricCipherKeyPair(new ECPublicKeyParameters(priv.getPubKeyPoint(), CURVE),
//                        new ECPrivateKeyParameters(priv.getPrivKey(), CURVE));
//            }
//        };

        EphemeralKeyPairGenerator ephemeralKeyPairGenerator =
                new EphemeralKeyPairGenerator(/*testGen*/eGen, new ECIESPublicKeyEncoder());

        iesEngine.init(new ECPublicKeyParameters(pub, CURVE), parametersWithIV, ephemeralKeyPairGenerator);

        return iesEngine.processBlock(plaintext, 0, plaintext.length);
    }
 
Example #5
Source File: Cryptograph.java    From SightRemote with GNU General Public License v3.0 5 votes vote down vote up
private static byte[] getHmac(byte[] secret, byte[] data, Digest algorithm) {
    HMac hmac = new HMac(algorithm);
    hmac.init(new KeyParameter(secret));
    byte[] result = new byte[hmac.getMacSize()];
    hmac.update(data, 0, data.length);
    hmac.doFinal(result, 0);
    return result;
}
 
Example #6
Source File: CryptoPrimitivesAndroid.java    From Clusion with GNU General Public License v3.0 5 votes vote down vote up
public static byte[] generateHmac(byte[] key, String msg) throws UnsupportedEncodingException {

		HMac hmac = new HMac(new SHA256Digest());
		byte[] result = new byte[hmac.getMacSize()];
		byte[] msgAry = msg.getBytes("UTF-8");
		hmac.init(new KeyParameter(key));
		hmac.reset();
		hmac.update(msgAry, 0, msgAry.length);
		hmac.doFinal(result, 0);
		return result;
	}
 
Example #7
Source File: CryptoPrimitivesAndroid.java    From Clusion with GNU General Public License v3.0 5 votes vote down vote up
public static byte[] generateHmac(byte[] key, byte[] msg) throws UnsupportedEncodingException {

		HMac hmac = new HMac(new SHA256Digest());
		byte[] result = new byte[hmac.getMacSize()];
		hmac.init(new KeyParameter(key));
		hmac.reset();
		hmac.update(msg, 0, msg.length);
		hmac.doFinal(result, 0);
		return result;
	}
 
Example #8
Source File: CryptoPrimitivesAndroid.java    From Clusion with GNU General Public License v3.0 5 votes vote down vote up
public static byte[] generateHmac512(byte[] key, String msg) throws UnsupportedEncodingException {

		HMac hmac = new HMac(new SHA512Digest());
		byte[] result = new byte[hmac.getMacSize()];
		byte[] msgAry = msg.getBytes("UTF-8");
		hmac.init(new KeyParameter(key));
		hmac.reset();
		hmac.update(msgAry, 0, msgAry.length);
		hmac.doFinal(result, 0);
		return result;
	}
 
Example #9
Source File: Cryptograph.java    From AndroidAPS with GNU Affero General Public License v3.0 5 votes vote down vote up
private static byte[] getHmac(byte[] secret, byte[] data, Digest algorithm) {
    HMac hmac = new HMac(algorithm);
    hmac.init(new KeyParameter(secret));
    byte[] result = new byte[hmac.getMacSize()];
    hmac.update(data, 0, data.length);
    hmac.doFinal(result, 0);
    return result;
}
 
Example #10
Source File: HDUtils.java    From bcm-android with GNU General Public License v3.0 4 votes vote down vote up
static HMac createHmacSha512Digest(byte[] key) {
    SHA512Digest digest = new SHA512Digest();
    HMac hMac = new HMac(digest);
    hMac.init(new KeyParameter(key));
    return hMac;
}
 
Example #11
Source File: MyHMacDSAKCalculator.java    From token-core-android with Apache License 2.0 4 votes vote down vote up
/**
 * Base constructor.
 *
 * @param digest digest to build the HMAC on.
 */
public MyHMacDSAKCalculator(Digest digest) {
  this.hMac = new HMac(digest);
  this.V = new byte[hMac.getMacSize()];
  this.K = new byte[hMac.getMacSize()];
}
 
Example #12
Source File: HDUtils.java    From green_android with GNU General Public License v3.0 4 votes vote down vote up
static HMac createHmacSha512Digest(byte[] key) {
    SHA512Digest digest = new SHA512Digest();
    HMac hMac = new HMac(digest);
    hMac.init(new KeyParameter(key));
    return hMac;
}
 
Example #13
Source File: HDUtils.java    From GreenBits with GNU General Public License v3.0 4 votes vote down vote up
static HMac createHmacSha512Digest(byte[] key) {
    SHA512Digest digest = new SHA512Digest();
    HMac hMac = new HMac(digest);
    hMac.init(new KeyParameter(key));
    return hMac;
}
 
Example #14
Source File: HDUtils.java    From bitherj with Apache License 2.0 4 votes vote down vote up
static HMac createHmacSha512Digest(byte[] key) {
    SHA512Digest digest = new SHA512Digest();
    HMac hMac = new HMac(digest);
    hMac.init(new KeyParameter(key));
    return hMac;
}