The Hash class provides basic hashing functionality.
// 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); A string containing the md5 hash value of the parameter. If an error is encountered, null is returned
Calculates the crc32 value of the parameter passed to the function and returns it as a number
A string containing the md5 hash value of the parameter. If an error is encountered, null is returned
Calculates the md5 hash value of the parameter passed to the function and returns it as a string.
A string containing the md5sum of the file specified in the path parameter. If an error is encountered, null is returned
Calculates the md5sum of the file specified in the path parameter