Copyright 2017 Moddable Tech, Inc.
Revised: November 7, 2017
Warning: These notes are preliminary. Omissions and errors are likely. If you encounter problems, please ask for assistance.
The Digest class creates cryptographic hashes using a variety of algorithms.
import {Digest} from "crypt";
let digest = new Digest("MD5");
digest.write("hello, world);
trace(`MD5 Hash: ${digest.close()}\n`);
let digest = new Digest("SHA1");
digest.write("hello,");
digest.write(" world");
trace(`SHA1 Hash: ${digest.close()}\n`);
The Digest
constructor takes the type of the hash to calculate as its sole argument.
let digest = new Digest("SHA1");
The following hash functions are supported:
- MD5
- SHA1
- SHA224
- SHA256
- SHA384
- SHA512
The write
function adds a message to the hash being calculated. There is no restriction on the length of the message. The message argument to write may be a String
or ArrayBuffer
. The write
function may be called more than once for a given digest calculation.
digest.write("123");
digest.write("456");
The close
function returns the the calculated hash. The hash is returned as an ArrayBuffer
. The close
function may only be called once, as it frees all resources associated with the digest calculation.