Example usage for org.bouncycastle.crypto Mac init

List of usage examples for org.bouncycastle.crypto Mac init

Introduction

In this page you can find the example usage for org.bouncycastle.crypto Mac init.

Prototype

public void init(CipherParameters params) throws IllegalArgumentException;

Source Link

Document

Initialise the MAC.

Usage

From source file:com.github.capone.protocol.crypto.SymmetricKey.java

License:Open Source License

public byte[] encrypt(Nonce nonce, byte[] message) throws EncryptionException {
    try {//from ww  w . j  a v a2  s  . co m
        byte[] subkey = new byte[SUBKEYBYTES];
        byte[] encrypted = new byte[message.length + MACBYTES];

        StreamCipher cipher = new XSalsa20Engine();
        cipher.init(true, new ParametersWithIV(key, nonce.nonce));
        cipher.processBytes(subkey, 0, subkey.length, subkey, 0);
        cipher.processBytes(message, 0, message.length, encrypted, MACBYTES);

        Mac auth = new Poly1305();
        auth.init(new KeyParameter(subkey));
        auth.update(encrypted, MACBYTES, message.length);
        auth.doFinal(encrypted, 0);

        return encrypted;
    } catch (Exception e) {
        throw new EncryptionException();
    }
}

From source file:com.github.capone.protocol.crypto.SymmetricKey.java

License:Open Source License

public byte[] decrypt(Nonce nonce, byte[] encrypted) throws DecryptionException {
    byte[] subkey = new byte[SUBKEYBYTES];
    byte[] mac = new byte[MACBYTES];
    byte[] plain = new byte[encrypted.length - MACBYTES];

    if (encrypted.length < MACBYTES) {
        throw new DecryptionException();
    }//from w ww  .  j av  a2 s  . c  om

    StreamCipher cipher = new XSalsa20Engine();
    cipher.init(true, new ParametersWithIV(key, nonce.nonce));
    cipher.processBytes(subkey, 0, subkey.length, subkey, 0);

    Mac auth = new Poly1305();
    auth.init(new KeyParameter(subkey));
    auth.update(encrypted, MACBYTES, encrypted.length - MACBYTES);
    auth.doFinal(mac, 0);

    boolean validMac = true;
    for (int i = 0; i < MACBYTES; i++) {
        validMac &= mac[i] == encrypted[i];
    }
    if (!validMac) {
        throw new DecryptionException();
    }

    cipher.processBytes(encrypted, MACBYTES, encrypted.length - MACBYTES, plain, 0);

    return plain;
}

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 a v a2s  .  com
    return output;
}

From source file:com.google.authenticator.blackberry.AuthenticatorScreen.java

License:Apache License

/**
 * Computes the one-time PIN given the secret key.
 * /*  w w w  . j  a v a 2  s .  com*/
 * @param secret
 *          the secret key
 * @return the PIN
 * @throws GeneralSecurityException
 * @throws DecodingException
 *           If the key string is improperly encoded.
 */
public static String computePin(String secret, Long counter) {
    try {
        final byte[] keyBytes = Base32String.decode(secret);
        Mac mac = new HMac(new SHA1Digest());
        mac.init(new KeyParameter(keyBytes));
        PasscodeGenerator pcg = new PasscodeGenerator(mac);
        if (counter == null) { // time-based totp
            return pcg.generateTimeoutCode();
        } else { // counter-based hotp
            return pcg.generateResponseCode(counter.longValue());
        }
    } catch (RuntimeException e) {
        return "General security exception";
    } catch (DecodingException e) {
        return "Decoding exception";
    }
}

From source file:com.google.authenticator.blackberry.CheckCodeScreen.java

License:Apache License

static String getCheckCode(String secret) throws Base32String.DecodingException {
    final byte[] keyBytes = Base32String.decode(secret);
    Mac mac = new HMac(new SHA1Digest());
    mac.init(new KeyParameter(keyBytes));
    PasscodeGenerator pcg = new PasscodeGenerator(mac);
    return pcg.generateResponseCode(0L);
}

From source file:com.skplanet.jose.jwa.crypto.CryptoUtils.java

License:Open Source License

public static byte[] hmac(Transformation transformation, byte[] raw, byte[] macKey)
        throws NoSuchAlgorithmException, InvalidKeyException {
    Mac hmac = Mac.getInstance(transformation.getValue());
    hmac.init(new SecretKeySpec(macKey, transformation.getAlgorithm()));
    return hmac.doFinal(raw);
}

From source file:de.tsenger.animamea.crypto.AmAESCrypto.java

License:Open Source License

@Override
public byte[] getMAC(byte[] data) {

    byte[] n = new byte[sscBytes.length + data.length];
    System.arraycopy(sscBytes, 0, n, 0, sscBytes.length);
    System.arraycopy(data, 0, n, sscBytes.length, data.length);
    n = addPadding(n);/*from   www.jav a2s . co m*/

    BlockCipher cipher = new AESFastEngine();
    Mac mac = new CMac(cipher, 64);

    mac.init(keyP);
    mac.update(n, 0, n.length);
    byte[] out = new byte[mac.getMacSize()];

    mac.doFinal(out, 0);

    return out;
}

From source file:de.tsenger.animamea.crypto.AmAESCrypto.java

License:Open Source License

@Override
public byte[] getMAC(byte[] key, byte[] data) {
    BlockCipher cipher = new AESFastEngine();
    Mac mac = new CMac(cipher, 64); // TODO Padding der Daten

    KeyParameter keyP = new KeyParameter(key);
    mac.init(keyP);

    mac.update(data, 0, data.length);//from   w  ww.  ja v a  2 s . c o  m

    byte[] out = new byte[8];

    mac.doFinal(out, 0);

    return out;
}

From source file:de.tsenger.animamea.crypto.AmDESCrypto.java

License:Open Source License

@Override
public byte[] getMAC(byte[] data) {

    byte[] n = new byte[8 + data.length];
    System.arraycopy(sscBytes, 0, n, 0, 8);
    System.arraycopy(data, 0, n, 8, data.length);

    BlockCipher cipher = new DESEngine();
    Mac mac = new ISO9797Alg3Mac(cipher, 64, new ISO7816d4Padding());

    ParametersWithIV parameterIV = new ParametersWithIV(keyP, IV);

    mac.init(parameterIV);
    mac.update(n, 0, n.length);/*  w w w.  java 2 s  .  co m*/

    byte[] out = new byte[8];

    mac.doFinal(out, 0);

    return out;
}

From source file:de.tsenger.animamea.crypto.AmDESCrypto.java

License:Open Source License

@Override
public byte[] getMAC(byte[] key, byte[] data) {
    BlockCipher cipher = new DESEngine();
    Mac mac = new ISO9797Alg3Mac(cipher, 64, new ISO7816d4Padding());

    KeyParameter keyP = new KeyParameter(key);
    mac.init(keyP);
    mac.update(data, 0, data.length);/* www  .  j  av  a  2s. c o  m*/

    byte[] out = new byte[8];

    mac.doFinal(out, 0);

    return out;
}