Class Hmac

All Implemented Interfaces:
Proxy

@Generated("org.javagi.JavaGI") public class Hmac extends ProxyInstance

HMACs should be used when producing a cookie or hash based on data and a key. Simple mechanisms for using SHA1 and other algorithms to digest a key and data together are vulnerable to various security issues. HMAC uses algorithms like SHA1 in a secure way to produce a digest of a key and data.

Both the key and data are arbitrary byte arrays of bytes or characters.

Support for HMAC Digests has been added in GLib 2.30, and support for SHA-512 in GLib 2.42. Support for SHA-384 was added in GLib 2.52.

To create a new GHmac, use Hmac(ChecksumType, byte[]). To free a GHmac, use unref().

Since:
2.30
  • Constructor Summary

    Constructors
    Constructor
    Description
    Create a Hmac proxy instance for the provided memory address.
    Hmac(ChecksumType digestType, @Nullable byte @Nullable [] key)
    Creates a new GHmac, using the digest algorithm digestType. If the digestType is not known, null is returned.
  • Method Summary

    Modifier and Type
    Method
    Description
    Copies a GHmac.
    void
    getDigest(@Nullable byte @Nullable [] buffer)
    Gets the digest from checksum as a raw binary array and places it into buffer. The size of the digest depends on the type of checksum.
    The memory layout of the native struct.
    Gets the HMAC as a hexadecimal string.
    static @Nullable Type
    Get the GType of the Hmac class.
    ref()
    Atomically increments the reference count of this Hmac by one.
    void
    Atomically decrements the reference count of this Hmac by one.
    void
    update(@Nullable byte @Nullable [] data)
    Feeds data into an existing GHmac.

    Methods inherited from class ProxyInstance

    equals, handle, hashCode

    Methods inherited from class Object

    clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Hmac

      public Hmac(MemorySegment address)
      Create a Hmac proxy instance for the provided memory address.
      Parameters:
      address - the memory address of the native object
    • Hmac

      public Hmac(ChecksumType digestType, @Nullable byte @Nullable [] key)

      Creates a new GHmac, using the digest algorithm digestType. If the digestType is not known, null is returned. A GHmac can be used to compute the HMAC of a key and an arbitrary binary blob, using different hashing algorithms.

      A GHmac works by feeding a binary blob through g_hmac_update() until the data is complete; the digest can then be extracted using g_hmac_get_string(), which will return the checksum as a hexadecimal string; or g_hmac_get_digest(), which will return a array of raw bytes. Once either g_hmac_get_string() or g_hmac_get_digest() have been called on a GHmac, the HMAC will be closed and it won't be possible to call g_hmac_update() on it anymore.

      Support for digests of type ChecksumType.SHA512 has been added in GLib 2.42. Support for ChecksumType.SHA384 was added in GLib 2.52.

      Parameters:
      digestType - the desired type of digest
      key - the key for the HMAC
      Since:
      2.30
  • Method Details

    • getType

      public static @Nullable Type getType()
      Get the GType of the Hmac class.
      Returns:
      the GType
    • getMemoryLayout

      public static MemoryLayout getMemoryLayout()
      The memory layout of the native struct.
      Returns:
      the memory layout
    • copy

      public Hmac copy()
      Copies a GHmac. If this Hmac has been closed, by calling g_hmac_get_string() or g_hmac_get_digest(), the copied HMAC will be closed as well.
      Returns:
      the copy of the passed GHmac. Use g_hmac_unref() when finished using it.
      Since:
      2.30
    • getDigest

      public void getDigest(@Nullable byte @Nullable [] buffer)

      Gets the digest from checksum as a raw binary array and places it into buffer. The size of the digest depends on the type of checksum.

      Once this function has been called, the GHmac is closed and can no longer be updated with g_checksum_update().

      Parameters:
      buffer - output buffer
      Since:
      2.30
    • getString

      public String getString()

      Gets the HMAC as a hexadecimal string.

      Once this function has been called the GHmac can no longer be updated with g_hmac_update().

      The hexadecimal characters will be lower case.

      Returns:
      the hexadecimal representation of the HMAC. The returned string is owned by the HMAC and should not be modified or freed.
      Since:
      2.30
    • ref

      public Hmac ref()

      Atomically increments the reference count of this Hmac by one.

      This function is MT-safe and may be called from any thread.

      Returns:
      the passed in GHmac.
      Since:
      2.30
    • unref

      public void unref()

      Atomically decrements the reference count of this Hmac by one.

      If the reference count drops to 0, all keys and values will be destroyed, and all memory allocated by the hash table is released. This function is MT-safe and may be called from any thread. Frees the memory allocated for hmac.

      Since:
      2.30
    • update

      public void update(@Nullable byte @Nullable [] data)

      Feeds data into an existing GHmac.

      The HMAC must still be open, that is g_hmac_get_string() or g_hmac_get_digest() must not have been called on hmac.

      Parameters:
      data - buffer used to compute the checksum
      Since:
      2.30