Skip to content

Clausal Prolog — Crypto Modules (hash, hmac, pbkdf2)

Clausal Prolog and the seam

Examples on this page are Clausal Prolog (.clausal, ISO Prolog syntax). It imports the Python modules py.hash, py.hmac and py.pbkdf2 through their facades library(hash), library(hmac) and library(pbkdf2) (see Importing Prolog); the seam imports them directly, as -import_from(py.hash, [...]).

Overview

Three small modules wrapping Python's hashlib and hmac stdlib for cryptographic hashing, message authentication, and key derivation. All modules use only the standard library — no third-party dependencies.

:- use_module(library(hash), [hash/3]).
:- use_module(library(hmac), [sign/3, verify/3]).
:- use_module(library(pbkdf2), [derive/4]).

check_integrity(Data, Expected) :-
    hash("sha256", Data, Computed),
    Computed == Expected.

sign_message(Key, Msg, Sig) :-
    sign(Key, Msg, Sig).

verify_message(Key, Msg, Sig) :-
    verify(Key, Msg, Sig).

test("integrity") :-
    check_integrity("abc", "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad").

test("sign and verify") :-
    sign_message("k", "hello", Sig),
    verify_message("k", "hello", Sig).

test("a signature does not verify another message", fail) :-
    sign_message("k", "hello", Sig),
    verify_message("k", "goodbye", Sig).

py.hash — Cryptographic Hashing

:- use_module(library(hash), [hash/3, hash_bytes/3]).
Predicate Mode Description
hash(Algorithm, Data, Hex) +Algo, +Data, -Hex Hex digest via hashlib, as a string
hash_bytes(Algorithm, Data, Bytes) +Algo, +Data, -Bytes Raw digest bytes, as a list of byte values

Supported algorithms: "sha256", "sha512", "md5", "sha1", "sha384", "sha3_256", "sha3_512", "blake2b", "blake2s" (anything hashlib.new() accepts).

Data can be a string (UTF-8 encoded) or bytes.

?- hash("sha256", "abc", Hex).
   Hex = "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad".

?- hash("md5", "", Hex).
   Hex = "d41d8cd98f00b204e9800998ecf8427e".

?- hash_bytes("sha256", "abc", Raw).
   Raw = [186, 120, 22, 191, 143, 1, 207, 234|...].   % the 32 digest bytes

An unknown algorithm raises domain_error(hash_algorithm, Algorithm) (hash("nope", "abc", H) raises error(domain_error(hash_algorithm, "nope"), hash/3)), and an unbound Algorithm or Data raises instantiation_error.


py.hmac — Message Authentication

:- use_module(library(hmac), [sign/3, sign/4, verify/3, verify/4]).
Predicate Mode Description
sign(Key, Data, Hex) +Key, +Data, -Hex HMAC-SHA256 signature
sign(Algorithm, Key, Data, Hex) +Algo, +Key, +Data, -Hex HMAC with specified algorithm
verify(Key, Data, Hex) +Key, +Data, +Hex verify HMAC-SHA256 (constant-time)
verify(Algorithm, Key, Data, Hex) +Algo, +Key, +Data, +Hex verify with specified algorithm

Key and Data can be strings (UTF-8 encoded) or bytes. verify uses hmac.compare_digest for constant-time comparison.

?- sign("secret-key", "message", Sig), verify("secret-key", "message", Sig).
   Sig = "287a3bd8a4fc7731a94c722079055323644d8798bd291bf9878abc9b8fd4b1d0".

?- sign("sha512", "key", "data", Sig).   % SHA-512 HMAC: 128 hex digits

py.pbkdf2 — Key Derivation

:- use_module(library(pbkdf2), [derive/4, derive/5]).
Predicate Mode Description
derive(Password, Salt, Iterations, DerivedKey) +Pw, +Salt, +Iter, -DK PBKDF2-HMAC-SHA256, 32-byte key
derive(Password, Salt, Iterations, KeyLength, DerivedKey) +Pw, +Salt, +Iter, +KL, -DK Custom key length

DerivedKey is a hex string. Password and Salt can be strings, atoms or bytes. Iterations must be a positive integer.

?- derive("password", "salt", 100000, Hash).
   % Hash is a 64-character hex string (32 bytes)

?- derive("password", "salt", 100000, 64, Hash).
   % Hash is a 128-character hex string (64 bytes)

Examples

File integrity check (using the Files module)

:- use_module(library(hash), [hash/3]).
:- use_module(library(py_files), [read_file_to_string/2]).

file_hash(Path, Hash) :-
    read_file_to_string(Path, Content),
    hash("sha256", Content, Hash).

API request signing

:- use_module(library(hmac), [sign/4]).

signed_request(Key, Body, Signature) :-
    sign("sha256", Key, Body, Signature).

Password hashing

:- use_module(library(pbkdf2), [derive/4]).

hash_password(Password, Salt, Hash) :-
    derive(Password, Salt, 100000, Hash).

See also: Python Interop — the seam's ++() escape for additional cryptography operations · UUID — UUID generation with hash-based variants (v3, v5).