To compute a digest, proceed as follows:
|
// a function to compute the MD5 of a file function filemd5(name) d=digest.new("MD5"); f=io.open(name); buf=io.read(f, 256); while buf#null do digest.add(d, buf); buf=io.read(f, 256) end; io.close(f); return digest.get(d) end |
|
// A function to compute the SHA1 digest of a string // (lower 8 bits only). // The digest is returned as a hexadecimal string. function sha1(s) d=digest.new("SHA1"); digest.add(d, s); return digest.get(d) end print encoding.tohex(sha1("Lucky Luke")) → d977763133f671049ca991a32e6a107b33be97d0
|
Digest the low bytes of all characters in string data with digest, or digest the single byte with number data.
|
// Compute the MD5 of the bytes 0 to 255 d=digest.new("MD5") for b=0 to 255 do digest.add(d, b) end print encoding.tohex(digest.get(d)) → e2c865db4162bed963bfaa9ef6ac18f0
|
Digest the low and high bytes of all characters in string data with digest. This function can be used to properly digest multi-byte strings (in UTF-16 little endian), as digest.add discards the high bytes.
Clear all data digested so far with digest, and start a new digestion.
Finalize the current digestion with digest and get the result as a string of bytes.
Repeated calls to digest.get without digesting more data in between will return the same digest.
Note that calling digest.get may add padding bytes to be able to compute the digest. Thus, subsequent digests after adding more data will most likely be different from those obtained without intermediate calls to digest.get.
Create a new digest with algorithm (a string, not case sensitive), and return it. Currently supported algorithms are:
| Alg. | Description | RFC | Bits |
| MD2 | Older message digest algorithm for general digital signature applications, and optimized for 8-bit machines. | 1319 | 128 |
| MD5 | Widely used message digest algorithm for general digital signature applications. | 1321 | 128 |
| SHA1 | Secure Hash Algorithm (version 1) for use with the Digital Signature Standard. | 3174 | 180 |
Throws ErrArgument if algorithm is not the name of a supported algorithm.
|
// create an MD5 digest object d=digest.new("md5") // get the hash (hex) of the empty message print encoding.tohex(digest.get(d)) → d41d8cd98f00b204e9800998ecf8427e
|