metis.crypto.sha1
Calculate the SHA1 hash for some text.
SHA1 has been broken for several years now, and so should not be treated as secure. However, it is often a useful (and efficient) way to compute a checksum of a value.
Usage
Hash the string "Hello, world!"
local sha1 = require "metis.crypto.sha1" print(sha1("Hello, world!")) -- 943a702d06f34599aee1f8da8ef9f7296031d699
Hash a file without reading it all into memory.
local sha1 = require "metis.crypto.sha1" local h = fs.open("rom/startup.lua", "r") local sha = sha1.create() while true do local block = h.read(2048) if not block then return end sha:append(block) end print(tostring(sha))
create() | Construct a new hash builder, to generate a SHA1 hash incrementally. |
---|
- create()Source
Construct a new hash builder, to generate a SHA1 hash incrementally.
The
sha1
module may be called as a function directly (see the module description) to compute the hash. However, in some circumstances it may be better to build a hash incrementally.For instance, instead of reading a file in one go, one may read it in chunks and hash each chunk separately.
Returns
Hash
The hash builder.
Types
Hash
A "work in progress" hash, created with create
.
The hash input may be extended with Hash:append
and the final string computed
with tostring
.