Class Checksum

All Implemented Interfaces:
Proxy

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

GLib provides a generic API for computing checksums (or ‘digests’) for a sequence of arbitrary bytes, using various hashing algorithms like MD5, SHA-1 and SHA-256. Checksums are commonly used in various environments and specifications.

To create a new GChecksum, use Checksum(ChecksumType). To free a GChecksum, use free().

GLib supports incremental checksums using the GChecksum data structure, by calling update(byte[]) as long as there’s data available and then using getString() or getDigest(byte[]) to compute the checksum and return it either as a string in hexadecimal form, or as a raw sequence of bytes. To compute the checksum for binary blobs and nul-terminated strings in one go, use the convenience functions GLib.computeChecksumForData(ChecksumType, byte[]) and GLib.computeChecksumForString(ChecksumType, String, long), respectively.

Since:
2.16
  • Constructor Summary

    Constructors
    Constructor
    Description
    Create a Checksum proxy instance for the provided memory address.
    Checksum(ChecksumType checksumType)
    Creates a new GChecksum, using the checksum algorithm checksumType. If the checksumType is not known, null is returned.
  • Method Summary

    Modifier and Type
    Method
    Description
    Copies a GChecksum.
    void
    Frees the memory allocated for checksum.
    void
    getDigest(@Nullable byte @Nullable [] buffer)
    Gets the digest from this Checksum as a raw binary vector 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 digest as a hexadecimal string.
    static @Nullable Type
    Get the GType of the Checksum class.
    void
    Resets the state of the this Checksum back to its initial state.
    static long
    Gets the length in bytes of digests of type checksumType
    void
    update(@Nullable byte @Nullable [] data)
    Feeds data into an existing GChecksum.

    Methods inherited from class ProxyInstance

    equals, handle, hashCode

    Methods inherited from class Object

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

    • Checksum

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

      public Checksum(ChecksumType checksumType)

      Creates a new GChecksum, using the checksum algorithm checksumType. If the checksumType is not known, null is returned. A GChecksum can be used to compute the checksum, or digest, of an arbitrary binary blob, using different hashing algorithms.

      A GChecksum works by feeding a binary blob through g_checksum_update() until there is data to be checked; the digest can then be extracted using g_checksum_get_string(), which will return the checksum as a hexadecimal string; or g_checksum_get_digest(), which will return a vector of raw bytes. Once either g_checksum_get_string() or g_checksum_get_digest() have been called on a GChecksum, the checksum will be closed and it won't be possible to call g_checksum_update() on it anymore.

      Parameters:
      checksumType - the desired type of checksum
      Since:
      2.16
  • Method Details

    • getType

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

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

      public static long typeGetLength(ChecksumType checksumType)
      Gets the length in bytes of digests of type checksumType
      Parameters:
      checksumType - a GChecksumType
      Returns:
      the checksum length, or -1 if checksumType is not supported.
      Since:
      2.16
    • copy

      public Checksum copy()
      Copies a GChecksum. If this Checksum has been closed, by calling g_checksum_get_string() or g_checksum_get_digest(), the copied checksum will be closed as well.
      Returns:
      the copy of the passed GChecksum. Use g_checksum_free() when finished using it.
      Since:
      2.16
    • free

      public void free()
      Frees the memory allocated for checksum.
      Since:
      2.16
    • getDigest

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

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

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

      Parameters:
      buffer - output buffer
      Since:
      2.16
    • getString

      public String getString()

      Gets the digest as a hexadecimal string.

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

      The hexadecimal characters will be lower case.

      Returns:
      the hexadecimal representation of the checksum. The returned string is owned by the checksum and should not be modified or freed.
      Since:
      2.16
    • reset

      public void reset()
      Resets the state of the this Checksum back to its initial state.
      Since:
      2.18
    • update

      public void update(@Nullable byte @Nullable [] data)
      Feeds data into an existing GChecksum. The checksum must still be open, that is g_checksum_get_string() or g_checksum_get_digest() must not have been called on checksum.
      Parameters:
      data - buffer used to compute the checksum
      Since:
      2.16