Exemplo de geração de MAC.
package doxy.examples;
import com.dinamonetworks.Dinamo;
import br.com.trueaccess.TacException;
import br.com.trueaccess.TacNDJavaLib;
public class GenerateMAC {
private static String strAddr = "127.0.0.1";
private static String strUsrId = "master";
private static String strPwd = "12345678";
private static String hmacKeyName = "hmac_key";
public static void main(String[] args) throws TacException {
Dinamo api = new Dinamo();
System.out.println("--> Login HSM");
api.openSession(strAddr, strUsrId, strPwd);
System.out.println("--> Create a HMAC key");
api.createKey(hmacKeyName, TacNDJavaLib.ALG_HMAC_SHA2_256, true);
byte message1[] =
{
(byte)0x31, (byte)0x32, (byte)0x33, (byte)0x34, (byte)0x35, (byte)0x36, (byte)0x37, (byte)0x38,
(byte)0x31, (byte)0x32, (byte)0x33, (byte)0x34, (byte)0x35, (byte)0x36, (byte)0x37, (byte)0x38
} ;
byte message2[] =
{
(byte)0x31, (byte)0x31, (byte)0x31, (byte)0x31, (byte)0x31, (byte)0x31, (byte)0x31, (byte)0x31,
(byte)0x32, (byte)0x32, (byte)0x32, (byte)0x32, (byte)0x32, (byte)0x32, (byte)0x32, (byte)0x32,
} ;
System.out.println("--> Generate HMAC in one operation");
byte[] mac = api.generateMAC(TacNDJavaLib.ALG_HMAC_SHA2_256,
hmacKeyName,
message1);
System.out.println("--> Initialize a HMAC multiple parts operation");
api.initMAC(TacNDJavaLib.ALG_HMAC_SHA2_256,
hmacKeyName);
System.out.println("--> Update part 1");
api.updateMAC(message1);
System.out.println("--> Update part 2");
api.updateMAC(message2);
System.out.println("--> End multiple parts MAC operation");
mac = api.endMAC();
System.out.println("--> Delete HMAC key");
api.deleteKey(hmacKeyName);
api.closeSession();
System.out.println("The process ended sucessfully");
}
}