Hash

Edit on GitHub
#include <Hash.h>
SReturnNameParameters
Hash (void)
Hash (const char* file)
Hash (const uint8* data, uintptr dataLength)
bool Append (const char* file)
void Append (const uint8* data, uintptr dataLength)
bool operator == (uint32 hash) const
bool operator != (uint32 hash) const
bool operator == (const Hash& hash) const
bool operator != (const Hash& hash) const
uint32 Result

Description

Represents a 32-bit hash value resulting from a hashing operation. Hashes are computed using the CRC-32 algorithm, making it unsuitable for cryptographic purposes, but it works well for quickly comparing data and files. Hashes can be computed through the Constructor or Append functions, and retrieved at any time with Result.

Constructors

    
Hash (void)

Constructs a hash with Result set to zero.

    
Hash (const char* file)
Hash (const uint8* data, uintptr dataLength)

Constructs a hash with a call to Append.

Functions

    
bool Append (const char* file)

Opens file and appends its data to this hash without resetting it, allowing continuous hashing at any time. The data is read in 4KB chunks before being passed over to the Append function. If file could not be read, this function returns false.

    
void Append (const uint8* data, uintptr dataLength)

Appends dataLength bytes from data to this hash without resetting it, allowing continuous hashing at any time. Ensure data contains at least dataLength bytes to avoid a buffer overflow.

Operators

    
bool operator == (uint32 hash) const
bool operator != (uint32 hash) const
bool operator == (const Hash& hash) const
bool operator != (const Hash& hash) const

Performs equality comparison on the Result.

Properties

    
uint32 Result

Provides direct access to the result of this hash.

Examples

// C++
#include <Robot.h>
ROBOT_NS_USE_ALL;

int main (void)
{
    Hash h1 ((uint8*) "Hello ", 6);
    Hash h2 ((uint8*) "World!", 6);

    h1.Result; // 0xEA2DFCC0
    h2.Result; // 0x76289DDE
    h2.Result = 0;

    h1.Append ((uint8*)       "World!", 6 );
    h2.Append ((uint8*) "Hello World!", 12);

    h1.Result; // 0x1C291CA3
    h2.Result; // 0x1C291CA3

    h1 == h2; // True
    h1 != h2; // False

    // CRC32 Hash of MyFile
    Hash ("MyFile").Result;
    return 0;
}
// Node
var robot = require ("robot-js");

var h1 = robot.Hash ("Hello ");
var h2 = robot.Hash ("World!");

h1.result; // 0xEA2DFCC0
h2.result; // 0x76289DDE
h2.result = 0;

h1.append (      "World!");
h2.append ("Hello World!");

h1.result; // 0x1C291CA3
h2.result; // 0x1C291CA3

h1.eq (h2); // True
h1.ne (h2); // False