Example usage for org.bouncycastle.crypto.macs CBCBlockCipherMac CBCBlockCipherMac

List of usage examples for org.bouncycastle.crypto.macs CBCBlockCipherMac CBCBlockCipherMac

Introduction

In this page you can find the example usage for org.bouncycastle.crypto.macs CBCBlockCipherMac CBCBlockCipherMac.

Prototype

public CBCBlockCipherMac(BlockCipher cipher, int macSizeInBits, BlockCipherPadding padding) 

Source Link

Document

create a standard MAC based on a block cipher with the size of the MAC been given in bits.

Usage

From source file:com.licel.jcardsim.crypto.SymmetricSignatureImpl.java

License:Apache License

public void init(Key theKey, byte theMode, byte[] bArray, short bOff, short bLen) throws CryptoException {
    if (theKey == null) {
        CryptoException.throwIt(CryptoException.UNINITIALIZED_KEY);
    }/* w  w w  .  j av a2  s .co  m*/
    if (!theKey.isInitialized()) {
        CryptoException.throwIt(CryptoException.UNINITIALIZED_KEY);
    }
    if (!(theKey instanceof SymmetricKeyImpl)) {
        CryptoException.throwIt(CryptoException.ILLEGAL_VALUE);
    }
    CipherParameters cipherParams = null;
    BlockCipher cipher = ((SymmetricKeyImpl) theKey).getCipher();
    if (bArray == null) {
        cipherParams = ((SymmetricKeyImpl) theKey).getParameters();
    } else {
        if (bLen != cipher.getBlockSize()) {
            CryptoException.throwIt(CryptoException.ILLEGAL_VALUE);
        }
        cipherParams = new ParametersWithIV(((SymmetricKeyImpl) theKey).getParameters(), bArray, bOff, bLen);
    }
    switch (algorithm) {
    case ALG_DES_MAC4_NOPAD:
        engine = new CBCBlockCipherMac(cipher, 32, null);
        break;
    case ALG_DES_MAC8_NOPAD:
        engine = new CBCBlockCipherMac(cipher, 64, null);
        break;
    case ALG_DES_MAC4_ISO9797_M1:
        engine = new CBCBlockCipherMac(cipher, 32, new ZeroBytePadding());
        break;
    case ALG_DES_MAC8_ISO9797_M1:
        engine = new CBCBlockCipherMac(cipher, 64, new ZeroBytePadding());
        break;
    case ALG_DES_MAC4_ISO9797_M2:
        engine = new CBCBlockCipherMac(cipher, 32, new ISO7816d4Padding());
        break;
    case ALG_DES_MAC8_ISO9797_M2:
        engine = new CBCBlockCipherMac(cipher, 64, new ISO7816d4Padding());
        break;
    case ALG_DES_MAC8_ISO9797_1_M2_ALG3:
        engine = new ISO9797Alg3Mac(new DESEngine(), 64, new ISO7816d4Padding());
        break;
    case ALG_DES_MAC4_PKCS5:
        engine = new CBCBlockCipherMac(cipher, 32, new PKCS7Padding());
        break;
    case ALG_DES_MAC8_PKCS5:
        engine = new CBCBlockCipherMac(cipher, 64, new PKCS7Padding());
        break;
    case ALG_AES_MAC_128_NOPAD:
        engine = new CBCBlockCipherMac(cipher, 128, null);
        break;
    case ALG_HMAC_SHA1:
        engine = new HMac(new SHA1Digest());
        break;
    case ALG_HMAC_SHA_256:
        engine = new HMac(new SHA256Digest());
        break;
    case ALG_HMAC_SHA_384:
        engine = new HMac(new SHA384Digest());
        break;
    case ALG_HMAC_SHA_512:
        engine = new HMac(new SHA512Digest());
        break;
    case ALG_HMAC_MD5:
        engine = new HMac(new MD5Digest());
        break;
    case ALG_HMAC_RIPEMD160:
        engine = new HMac(new RIPEMD160Digest());
        break;
    default:
        CryptoException.throwIt(CryptoException.NO_SUCH_ALGORITHM);
        break;
    }
    engine.init(cipherParams);
    isInitialized = true;
}