Search in sources :

Example 1 with NAEMac

use of com.ingrian.security.nae.NAEMac in project CipherTrust_Application_Protection by thalescpl-io.

the class MultiThreadMacSample method run.

public void run() {
    try {
        System.out.println("[" + Thread.currentThread().getName() + "] starting sample.");
        // create and initialize mac object
        NAEMac mac = NAEMac.getNAEMacInstance("HmacSHA512", "IngrianProvider");
        mac.init(_key);
        // Generate random data to mac
        SecureRandom rng = SecureRandom.getInstance("IngrianRNG", "IngrianProvider");
        byte[] randomBytes = new byte[16];
        rng.nextBytes(randomBytes);
        String dataToMac = new String(randomBytes);
        // perform the mac operation and send message string to Key Manager
        mac.setMessage("client is creating mac: " + Thread.currentThread().getName());
        byte[] macValue = mac.doFinal(dataToMac.getBytes());
        // create and initialize mac object for verification
        NAEMac macV = NAEMac.getNAEMacInstance("HmacSHA512Verify", "IngrianProvider");
        macV.init(_key, new MACValue(macValue));
        // perform the macV operation and send message string to Key Manager
        macV.setMessage("client is verifying the mac: " + Thread.currentThread().getName());
        byte[] result = macV.doFinal(dataToMac.getBytes());
        // check verification result
        if (result.length != 1 || result[0] != 1) {
            System.out.println(Thread.currentThread().getName() + " Invalid MAC.");
        } else {
            System.out.println(Thread.currentThread().getName() + " MAC Verified OK.");
        }
    } catch (Exception e) {
        System.out.println("Got exception: " + e);
        e.printStackTrace();
    }
}
Also used : NAEMac(com.ingrian.security.nae.NAEMac) SecureRandom(java.security.SecureRandom) MACValue(com.ingrian.security.nae.MACValue)

Aggregations

MACValue (com.ingrian.security.nae.MACValue)1 NAEMac (com.ingrian.security.nae.NAEMac)1 SecureRandom (java.security.SecureRandom)1