crypto_data_hash/3

Markus Triska
This draft is for consideration of inclusion into the Prolog prologue. References refer to 13211-1:1995. Parts needing improvements are underlined.

p.c.x crypto_data_hash/3

p.c.x.1 Description

crypto_data_hash(Cs, Hs, Options) is true iff Hs is a list of characters '0','1',...'9',a,b,c,d,e,f denoting the Base16-encoding of a cryptographic hash of the characters Cs, taking into account the list Options of crypto_data_hash-options. Direct unification of Hs with a computed result may leak information about the expected hash or HMAC.

Options:

algorithm(A)
Use hash algorithm A, either a variable or one of sha256, sha384, or sha512. If A is a variable, then an implementation-dependent algorithm I is used, and A is unified with I.
encoding(E)
Encoding E to use for interpreting each character of Cs as a sequence of bytes, either utf8 for UTF-8 encoding, or octet to use the character code of each character as byte value.
hmac(Bs)
Hs is a hash-based message authentication code (HMAC) of Cs, using the list of bytes Bs as key. The construction is specified in RFC 2104.

p.c.x.2 Template and modes

crypto_data_hash(+character_list, -character_list, +crypto_data_hash_options_list)

p.c.x.3 Errors

a) Cs is a partial list or a list with an element E which is a variable
instantiation_error.
b) Options is a partial list or has a list prefix with an element E which is a variable
instantiation_error.
c) Cs is neither a list nor a partial list.
type_error(list, Cs).
d) Cs is a ground list with an element E which is not a character
type_error(character, E).
e) Options is neither a partial list nor a list
type_error(list, Options).
f) Options is a list with an element E which is neither a variable nor a valid option
domain_error(crypto_data_hash_option, E).
g) Hs is neither a list nor a partial list.
type_error(list, Hs).
h) Hs has a list prefix with an element E which is not a character
type_error(character, E).

p.c.x.4 Examples

?- crypto_data_hash("test", Hs, [algorithm(sha256)]).
   Hs = "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08".

History

2017-01-04: first availability in SWI-Prolog, with hmac/1 option as later addition
2020-05-13: first availability in Scryer Prolog, with encoding/1 and hmac/1 options as later additions
2023-11-14: first traceable availability in Trealla Prolog