Home >

ProtectToolkit-J Reference Guide > Supported MAC Algorithms > Sample MAC Code

Sample MAC Code

This sample code fragment will generate a MAC code (based on a randomly generated DES key) for the bytes in the string "hello world".

KeyGenerator keyGen = KeyGenerator.getInstance("DES", "SAFENET");
Key desKey = keyGen.generateKey();
Mac desMac = Mac.getInstance("DES", "SAFENET");
desMac.init(desKey);
byte[] mac = desMac.doFinal("hello world".getBytes());