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

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

Introduction

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

Prototype

public CMac(BlockCipher cipher, int macSizeInBits) 

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.pearson.pdn.demos.chainoflearning.EventingUtility.java

License:Apache License

/**
 * Creates an auth token signed with principal's credentials
 * /*from   ww  w.ja v a2s  . c o  m*/
 * @param dateTime   utc time in yyyy-MM-dd'T'HH:mm:ssZ format 
 * @param delimiter   Delimiter to between token parts
 * @param principalId   Id portion of credentials
 * @param key   Secret portion of credentials
 * @param payload   Data being signed
 * @return   Token value
 * @throws UnsupportedEncodingException
 */
private static String generateAuthToken(String dateTime, String delimiter, String principalId, String key,
        String payload) throws UnsupportedEncodingException {
    // prepare the data
    byte[] macInput = (dateTime + payload).getBytes("UTF-8");
    byte[] macOutput = new byte[16];
    // sign the data
    CMac macProvider = new CMac(new AESFastEngine(), 128);
    macProvider.init(new KeyParameter(key.getBytes()));
    macProvider.update(macInput, 0, macInput.length);
    macProvider.doFinal(macOutput, 0);
    // hex the signed data
    String macOutputHex = new String(Hex.encode(macOutput));
    // concatenate the token parts
    return principalId + delimiter + dateTime + delimiter + macOutputHex;
}

From source file:com.pearson.pdn.learningstudio.eventing.EventingClient.java

License:Apache License

/**
 * Creates an auth token signed with principal's credentials
 * /*from   w  w  w  . j  a v  a  2  s . c o  m*/
 * @param dateTime   utc time in yyyy-MM-dd'T'HH:mm:ssZ format 
 * @param delimiter   Delimiter to between token parts
 * @param principalId   Id portion of credentials
 * @param key   Secret portion of credentials
 * @param payload   Data being signed
 * @return   Token value
 * @throws UnsupportedEncodingException
 */
String generateAuthToken(String dateTime, String payload) throws UnsupportedEncodingException {
    // prepare the data
    byte[] macInput = (dateTime + payload).getBytes("UTF-8");
    byte[] macOutput = new byte[16];
    // sign the data
    CMac macProvider = new CMac(new AESFastEngine(), 128);
    macProvider.init(new KeyParameter(config.getPrincipalSecret().getBytes()));
    macProvider.update(macInput, 0, macInput.length);
    macProvider.doFinal(macOutput, 0);
    // hex the signed data
    String macOutputHex = new String(Hex.encode(macOutput));
    // concatenate the token parts
    return config.getPrincipalId() + DEFAULT_DELIMITER + dateTime + DEFAULT_DELIMITER + macOutputHex;
}

From source file:common.crypto.bouncycastle.CCryptoCMacBC.java

License:Open Source License

@Override
public void initialize(CryptoTypes.EKeyLength eLen) {
    m_nKeyLen = CryptoTypes.getKeyLen(eLen);
    BlockCipher bc = new AESLightEngine();
    m_cmac = new CMac(bc, m_nKeyLen);

}

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 ww w  .j av a  2 s .  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);/*from  w  w  w  .  j a  v  a  2  s . co m*/

    mac.update(data, 0, data.length);

    byte[] out = new byte[8];

    mac.doFinal(out, 0);

    return out;
}