glibmm  2.62.0
About: glibmm is a set of C++ bindings for glib, including cross-platform APIs such as a std::string-like UTF8 string class, string utility methods, such as a text encoding converter API, file access, and threads.
  Fossies Dox: glibmm-2.62.0.tar.xz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

Glib::Checksum Class Reference

Computes the checksum for data. More...

#include <checksum.h>

Public Types

enum  ChecksumType {
  CHECKSUM_MD5, CHECKSUM_SHA1, CHECKSUM_SHA256, CHECKSUM_SHA512,
  CHECKSUM_SHA384
}
 

Public Member Functions

 Checksum ()
 Constructs an invalid object. More...
 
 Checksum (GChecksum *castitem, bool make_a_copy=false)
 
 Checksum (const Checksum &src)
 
Checksumoperator= (const Checksum &src)
 
 Checksum (Checksum &&other) noexcept
 
Checksumoperator= (Checksum &&other) noexcept
 
 ~Checksum () noexcept
 
void swap (Checksum &other) noexcept
 
GChecksum * gobj ()
 
const GChecksum * gobj () const
 
GChecksum * gobj_copy () const
 Provides access to the underlying C instance. The caller is responsible for freeing it. Use when directly setting fields in structs. More...
 
 Checksum (ChecksumType checksum_type)
 Creates a new Checksum, using the checksum algorithm checksum_type. More...
 
 operator bool () const
 Returns true if the Checksum object is valid. More...
 
void reset ()
 Resets the state of the checksum back to its initial state. More...
 
void update (const guchar *data, gsize length)
 Feeds data into an existing Checksum. More...
 
void update (const std::string &data)
 Feeds data into an existing Checksum. More...
 
void get_digest (guint8 *buffer, gsize *digest_len) const
 Gets the digest from checksum as a raw binary vector and places it into buffer. More...
 
std::string get_string () const
 Gets the digest as a hexadecimal string. More...
 

Static Public Member Functions

static std::string compute_checksum (ChecksumType checksum_type, const guchar *data, gsize length)
 Computes the checksum for a binary data of length. More...
 
static std::string compute_checksum (ChecksumType checksum_type, const std::string &str)
 Computes the checksum of a string. More...
 
static gssize get_length (ChecksumType checksum_type)
 Gets the length in bytes of digests of type checksum_type. More...
 

Protected Attributes

GChecksum * gobject_
 

Related Functions

(Note that these are not member functions.)

void swap (Checksum &lhs, Checksum &rhs) noexcept
 
Glib::Checksum wrap (GChecksum *object, bool take_copy=false)
 A Glib::wrap() method for this object. More...
 

Detailed Description

Computes the checksum for data.

This is 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.

glibmm supports incremental checksums by calling update() as long as there's data available and then using get_string() or get_digest() 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 NULL-terminated strings in one go, use the static compute_checksum() convenience functions().

Since @PACKAGE_NAME@ 2.16:

Definition at line 46 of file checksum.h.

Constructor & Destructor Documentation

◆ Checksum() [1/5]

Glib::Checksum::Checksum ( )

Constructs an invalid object.

E.g. for output arguments to methods. There is not much you can do with the object before it has been assigned a valid value.

Definition at line 98 of file checksum.cc.

◆ Checksum() [2/5]

Glib::Checksum::Checksum ( GChecksum *  castitem,
bool  make_a_copy = false 
)
explicit

Definition at line 108 of file checksum.cc.

References gobject_.

◆ Checksum() [3/5]

Glib::Checksum::Checksum ( const Checksum src)

Definition at line 103 of file checksum.cc.

◆ Checksum() [4/5]

Glib::Checksum::Checksum ( Checksum &&  other)
noexcept

Definition at line 139 of file checksum.cc.

◆ ~Checksum()

Glib::Checksum::~Checksum ( )
noexcept

Definition at line 153 of file checksum.cc.

References gobject_.

◆ Checksum() [5/5]

Glib::Checksum::Checksum ( ChecksumType  checksum_type)
explicit

Creates a new Checksum, using the checksum algorithm checksum_type.

If the checksum_type is not known, then operator bool() will return false.

Parameters
checksum_typeChecksum type, one of defined above.

Definition at line 32 of file checksum.cc.

Member Function Documentation

◆ compute_checksum() [1/2]

std::string Glib::Checksum::compute_checksum ( ChecksumType  checksum_type,
const guchar *  data,
gsize  length 
)
static

Computes the checksum for a binary data of length.

This is a convenience wrapper for g_checksum_new(), g_checksum_get_string() and g_checksum_free().

The hexadecimal string returned will be in lower case.

Since @PACKAGE_NAME@ 2.16:
Parameters
checksum_typeA ChecksumType.
dataBinary blob to compute the digest of.
lengthLength of data.
Returns
The digest of the binary data as a string in hexadecimal.

Definition at line 190 of file checksum.cc.

◆ compute_checksum() [2/2]

std::string Glib::Checksum::compute_checksum ( ChecksumType  checksum_type,
const std::string &  str 
)
static

Computes the checksum of a string.

Parameters
checksum_typeA ChecksumType
strThe string to compute the checksum of.
Returns
The checksum as a hexadecimal string.

Definition at line 48 of file checksum.cc.

◆ get_digest()

void Glib::Checksum::get_digest ( guint8 *  buffer,
gsize *  digest_len 
) const

Gets the digest from 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 Checksum is closed and can no longer be updated with g_checksum_update().

Since @PACKAGE_NAME@ 2.16:
Parameters
bufferOutput buffer.
digest_lenAn inout parameter. The caller initializes it to the size of buffer. After the call it contains the length of the digest.

Definition at line 180 of file checksum.cc.

References gobj().

◆ get_length()

gssize Glib::Checksum::get_length ( ChecksumType  checksum_type)
static

Gets the length in bytes of digests of type checksum_type.

Parameters
checksum_typeA ChecksumType.
Returns
The checksum length, or -1 if checksum_type is not supported.

Definition at line 42 of file checksum.cc.

◆ get_string()

std::string Glib::Checksum::get_string ( ) const

Gets the digest as a hexadecimal string.

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

The hexadecimal characters will be lower case.

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

Definition at line 185 of file checksum.cc.

References gobj().

◆ gobj() [1/2]

GChecksum* Glib::Checksum::gobj ( )
inline

Definition at line 73 of file checksum.h.

References gobject_.

Referenced by get_digest(), get_string(), reset(), Glib::Value< T >::set(), and update().

◆ gobj() [2/2]

const GChecksum* Glib::Checksum::gobj ( ) const
inline

Definition at line 74 of file checksum.h.

References gobject_.

◆ gobj_copy()

GChecksum * Glib::Checksum::gobj_copy ( ) const

Provides access to the underlying C instance. The caller is responsible for freeing it. Use when directly setting fields in structs.

Definition at line 164 of file checksum.cc.

References gobject_.

◆ operator bool()

Glib::Checksum::operator bool ( ) const
explicit

Returns true if the Checksum object is valid.

This will return false, for instance, if an unsupported checksum type was provided to the constructor.

Definition at line 36 of file checksum.cc.

◆ operator=() [1/2]

Checksum & Glib::Checksum::operator= ( Checksum &&  other)
noexcept

Definition at line 146 of file checksum.cc.

◆ operator=() [2/2]

Checksum & Glib::Checksum::operator= ( const Checksum src)

Definition at line 127 of file checksum.cc.

References gobject_.

◆ reset()

void Glib::Checksum::reset ( )

Resets the state of the checksum back to its initial state.

Since @PACKAGE_NAME@ 2.18:

Definition at line 170 of file checksum.cc.

References gobj().

◆ swap()

void Glib::Checksum::swap ( Checksum other)
noexcept

Definition at line 159 of file checksum.cc.

◆ update() [1/2]

void Glib::Checksum::update ( const guchar *  data,
gsize  length 
)

Feeds data into an existing Checksum.

The checksum must still be open, that is g_checksum_get_string() or g_checksum_get_digest() must not have been called on checksum.

Since @PACKAGE_NAME@ 2.16:
Parameters
dataBuffer used to compute the checksum.
lengthSize of the buffer, or -1 if it is a null-terminated string.

Definition at line 175 of file checksum.cc.

References gobj().

◆ update() [2/2]

void Glib::Checksum::update ( const std::string &  data)

Feeds data into an existing Checksum.

The checksum must still be open, that is get_string() or get_digest() must not have been called on the checksum.

Parameters
dataBuffer used to compute the checksum

Definition at line 55 of file checksum.cc.

References gobj().

Friends And Related Function Documentation

◆ swap()

void swap ( Checksum lhs,
Checksum rhs 
)
related
Parameters
lhsThe left-hand side
rhsThe right-hand side

Definition at line 259 of file checksum.h.

◆ wrap()

Glib::Checksum wrap ( GChecksum *  object,
bool  take_copy = false 
)
related

A Glib::wrap() method for this object.

Parameters
objectThe C instance.
take_copyFalse if the result should take ownership of the C instance. True if it should take a new copy or ref.
Returns
A C++ instance that wraps this C instance.

Definition at line 86 of file checksum.cc.

Member Data Documentation

◆ gobject_

GChecksum* Glib::Checksum::gobject_
protected

Definition at line 80 of file checksum.h.

Referenced by Checksum(), gobj(), gobj_copy(), operator=(), and ~Checksum().


The documentation for this class was generated from the following files: