Mac creation : Mac « Security « Java Tutorial






import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

public class MainClass {
  public static void main(String args[]) throws Exception {
    SecretKeySpec k = new SecretKeySpec("1234".getBytes(), "HMACSHA1");
    Mac m = Mac.getInstance("HmacMD5");
    m.init(k);
    m.update("test".getBytes("UTF8"));
    byte s[] = m.doFinal();
    for (int i = 0; i < s.length; i++) {
      System.out.print( Integer.toHexString((0x000000ff & s[i]) | 0xffffff00).substring(6));
    }
  }
}








36.25.Mac
36.25.1.Mac creation
36.25.2.Use MAC
36.25.3.Generating a Message Authentication Code (MAC) Key
36.25.4.Generating a MAC from a text input using a random key.