hash
calculates a digest hash
(any subject, string algorithm, string resultFormat) -> any
Calculates a digest hash of given subject using a specified hashing algorithm.
Processing
The digest of subject
is calculated as follows:
-
If
subject
isnil
,nil
is returned. -
If
subject
is of typebinary
the digest of its bytes is returned. -
If
subject
is of typestring
the digest of the UTF-8 bytes of the string is returned. -
If
subject
is of a type that can be cast to string - such as numeric, boolean or datetime types - it is cast to string and the digest of the UTF-8 bytes of the string is returned. -
If
subject
is a list, the bytes to hash are constructed as follows:
Each element contributes a byte sequence consisting of- bytes of the element index as UTF-8 string
- bytes of the element value - a
nil
value contributes no bytes
The byte sequences of all elements are concatenated and hashed.
For example:
The empty list has the same hash value as the empty string.
The value["zero", nil, "two"]
has the same hash value as the string'0zero12two'
-
If
subject
is a dict, the bytes to hash are constructed as follows:
Each element contributes a byte sequence consisting of- bytes of element key as UTF-8 string
- bytes of the element value - a
nil
value contributes no bytes
The byte sequences of all elements are concatenated in lexographical order of keys and hashed.
For example:
The empty dict has the same hash value as the empty string.
The value{:a 'one', :b nil, :c 'three'}
has the same hash value as the string'aonebcthree'
. -
If
subject
is or contains a data type that is not convertible to bytes via the above rules, an error is thrown.
Parameters
Name | Type | Description |
---|---|---|
subject |
any | the value to hash |
algorithm |
string |
The following algorithms are supported:
|
resultFormat |
string |
The hashing function can return the hash digest in the following forms:
|