Tabular IQ

Overview

The Hash class provides basic hashing functionality.

Methods

Hash.crc32
Calculates a crc32 value
Hash.md5
Calculates an md5 hash value
Hash.md5sum
Calculates an md5sum of a file

Example

// Hash computes MD5 digests and CRC32 checksums.  Every method is
// static; there is nothing to construct.  md5() and crc32() each take a
// String or a MemoryBuffer -- a String is hashed as its UTF-8 bytes.

const text = "The quick brown fox jumps over the lazy dog";

let output = `md5:    ${Hash.md5(text)}\n`;
output += `crc32:  ${Hash.crc32(text)}\n\n`;

// -- a file --

// (Environment.getTempPath() has no trailing separator)
const path = `${Environment.getTempPath()}/hash_example.txt`;

// write() rather than writeLine(), so the file holds exactly these bytes
const writer = File.createText(path);
writer.write(text);
writer.close();

// md5sum() hashes the bytes on disk, so for plain ASCII text it agrees
// with md5() of the string that was written
output += `md5sum:        ${Hash.md5sum(path)}\n`;
output += `matches md5(): ${Hash.md5sum(path) === Hash.md5(text)}\n`;

// a missing file gives null -- there is no exception to catch
const missing = Hash.md5sum(`${path}.nope`);
output += `missing file:  ${missing}\n\n`;

// -- binary data --

// a MemoryBuffer is hashed as raw bytes, with no text conversion; this
// is the form for images, downloads and anything else that is not text
const buf = new MemoryBuffer(256);
for (let i = 0; i < 256; ++i) {
    buf[i] = i;
}

output += `buffer md5:    ${Hash.md5(buf)}\n`;
output += `buffer crc32:  ${Hash.crc32(buf)}`;

alert(output);

Hash.crc32

static function Hash.crc32(value : String) : Number
static function Hash.crc32(value : MemoryBuffer) : Number

Arguments

value
A string or memory buffer to hash

Returns

A string containing the md5 hash value of the parameter. If an error is encountered, null is returned

Description

Calculates the crc32 value of the parameter passed to the function and returns it as a number

Hash.md5

static function Hash.md5(value : String) : String
static function Hash.md5(value : MemoryBuffer) : String

Arguments

value
A string or memory buffer to hash

Returns

A string containing the md5 hash value of the parameter. If an error is encountered, null is returned

Description

Calculates the md5 hash value of the parameter passed to the function and returns it as a string.

Hash.md5sum

static function Hash.md5sum(path : String) : String

Arguments

value
A string or memory buffer to hash

Returns

A string containing the md5sum of the file specified in the path parameter. If an error is encountered, null is returned

Description

Calculates the md5sum of the file specified in the path parameter