Class HMACDigest

  • All Implemented Interfaces:
    java.lang.Cloneable

    public class HMACDigest
    extends java.lang.Object
    implements java.lang.Cloneable
    This class implements the HMAC algorithm specified in RFC 2104 using any MessageDigest.
    Version:
    $Revision$, $Date$
    Author:
    mikep
    See Also:
    MessageDigest
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int IPAD  
      static int OPAD  
      static int PAD_BYTES  
    • Constructor Summary

      Constructors 
      Constructor Description
      HMACDigest​(java.security.MessageDigest md)
      Creates an HMACDigest
      HMACDigest​(java.security.MessageDigest md, byte[] key)
      Creates an HMACDigest and initializes the HMAC function with the given key.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.lang.Object clone()
      Clone the HMACDigest
      byte[] digest()
      Completes the HMAC computation with the outer pad The digest is reset after this call is made.
      java.security.MessageDigest getMessageDigest()
      Return the MessageDigest used for this HMAC
      void init​(byte[] key)
      Initialize the HMAC function The HMAC transform looks like: hash(key XOR opad, hash(key XOR ipad, text)) where key is an n byte key ipad is the byte 0x36 repeated 64 times opad is the byte 0x5c repeated 64 times and text is the data being protected This routine must be called after every reset.
      void reset()
      Resets the digest for further use.
      void update​(byte[] input)
      Updates the digest using the specified array of bytes.
      • Methods inherited from class java.lang.Object

        equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • HMACDigest

        public HMACDigest​(java.security.MessageDigest md)
        Creates an HMACDigest
        Parameters:
        md - The MessageDigest to be used for the HMAC calculation. It must be clonable.
      • HMACDigest

        public HMACDigest​(java.security.MessageDigest md,
                          byte[] key)
        Creates an HMACDigest and initializes the HMAC function with the given key.
        Parameters:
        md - The MessageDigest to be used for the HMAC calculation. It must be clonable.
        key - The key value to be used in the HMAC calculation
    • Method Detail

      • getMessageDigest

        public java.security.MessageDigest getMessageDigest()
        Return the MessageDigest used for this HMAC
      • init

        public void init​(byte[] key)
        Initialize the HMAC function The HMAC transform looks like: hash(key XOR opad, hash(key XOR ipad, text)) where key is an n byte key ipad is the byte 0x36 repeated 64 times opad is the byte 0x5c repeated 64 times and text is the data being protected This routine must be called after every reset.
        Parameters:
        key - The password used to protect the hash value
      • update

        public void update​(byte[] input)
        Updates the digest using the specified array of bytes.
        Parameters:
        input - the array of bytes.
      • digest

        public byte[] digest()
        Completes the HMAC computation with the outer pad The digest is reset after this call is made.
        Returns:
        the array of bytes for the resulting hash value.
      • reset

        public void reset()
        Resets the digest for further use.
      • clone

        public java.lang.Object clone()
                               throws java.lang.CloneNotSupportedException
        Clone the HMACDigest
        Overrides:
        clone in class java.lang.Object
        Returns:
        a clone if the implementation is cloneable.
        Throws:
        java.lang.CloneNotSupportedException - if this is called on a MessageDigest implementation that does not support Cloneable.