module crypto.tf
The tweakstreet/crypto
module contains functions implementing digest hashing.
library digest
The digest library provides functions related to digest hashing.
algorithms
() -> list
Returns a list of supported digest algorithms such as MD5, SHA, etc.
The returned list is populated by Security.getAlgorithms(“MessageDigest”)
> digest.algorithms()
[
"MD2",
"MD5",
"SHA",
"SHA-224",
"SHA-256",
"SHA-384",
"SHA-512",
"SHA-512/224",
"SHA-512/256",
"SHA3-224",
"SHA3-256",
"SHA3-384",
"SHA3-512"
]
digest
(string algorithm, boolean return_binary=false) -> function
Returns a function f
of the form (x) -> any
.
When called, f
returns the message digest of x
using the given algorithm
.
See digest.algorithms for a list of available algorithms.
If return_binary
is true
, f
returns a binary value containing the hash value,
otherwise a hex string - all lowercase letters - is returned.
Function f
calculates the digest value as follows:
- If
x
isnil
,nil
is returned. - If
x
is of typebinary
the digest of its bytes is returned. - If
x
is of typestring
the digest of the UTF-8 bytes of the string is returned. - If
x
is of a type that can be cast to string - such as numeric types - it is cast to string and the digest of the UTF-8 bytes of the string is returned. - If
x
cannot be cast to string, an error is thrown.
> f: digest.digest("MD5")
function
> f("Hello World!")
"ed076287532e86365e841e92bfc50d8c"
> f: digest.digest("SHA")
function
> f("Hello World!")
"2ef7bde608ce5404e97d5f042f95f89f1c232871"
md5
(x) -> string
Returns the MD5 digest value of x
. This function is equivalent to the function produced by digest("MD5")
.
See digest for details on how the hash is calculated
for different types of x
.
> digest.md5("Hello World!")
"ed076287532e86365e841e92bfc50d8c"
sha1
(x) -> string
Returns the SHA-1 digest value of x
. This function is equivalent to the function produced by digest("SHA")
.
See digest for details on how the hash is calculated for different types of x
.
> digest.sha1("Hello World!")
"2ef7bde608ce5404e97d5f042f95f89f1c232871"
sha256
(x) -> string
Returns the SHA-256 digest value of x
. This function is equivalent to the function produced by digest("SHA-256")
.
See digest for details on how the hash is calculated for different types of x
.
> digest.sha256("Hello World!")
"7f83b1657ff1fc53b92dc18148a1d65dfc2d4b1fa3d677284addd200126d9069"
sha512
(x) -> string
Returns the SHA-512 digest value of x
. This function is equivalent to the function produced by digest("SHA-512")
.
See digest for details on how the hash is calculated for different types of x
.
> digest.sha512("Hello World!")
"861844d6704e8573fec34d967e20bcfef3d424cf48be04e6dc08f2bd58c729743371015ead891cc3cf1c9d34b49264b510751b1ff9e537937bc46b5d6ff4ecc8"