crypto.q

Cryptographic digests (SHA-2, SHA-3, BLAKE2, MD5, SHA-1, MD4), HMAC and random bytes from the platform's OpenSSL. hash and hmac read the bytes of a string, a byte vector or a symbol (its text); a char atom is one byte. A symbol vector, or a list of those subjects, answers one digest per element in a single call (a list of one-char strings is spelt (enlist "a";enlist "b") - ("a";"b") is the string "ab"); anything else is 'type. A digest, a MAC and a random draw are byte vectors, as md5 answers, a list of them over a collection; raze string gives the hex. A box without OpenSSL answers 'nyi, as a TLS connection does there.

Examples

raze string .crypto.sha256 "hello world"
.crypto.sha256 `AAPL`MSFT`IBM
.crypto.hmac[`$"sha2-256";"secret";"hello world"]

Entity Summary

EntitiesShort Description
.crypto.algos[]The digest names .crypto.hash accepts in this process: the seventeen it knows, less any this OpenSSL cannot compute.
.crypto.hash[algo;data]The digest named by algo: md4 md5 sha1 sha2-224 sha2-256 sha2-384 sha2-512 sha3-224 sha3-256 sha3-384 sha3-512 keccak224 keccak256 keccak384 keccak512 blake2b-512 blake2s-256; the keccak names answer SHA-3.
.crypto.hmac[algo;secret;data]The HMAC (RFC 2104) of data under secret, with the digest named by algo as .crypto.hash names it.
.crypto.randbytes[n]n cryptographically secure random bytes from OpenSSL's generator, which seeds itself from the OS; \S does not reach it.
.crypto.sha256SHA-256 of data: .crypto.hash[`$"sha2-256";].

Entity Details

.crypto.algos[]

The digest names .crypto.hash accepts in this process: the seventeen it knows, less any this OpenSSL cannot compute.

Return: (symbol list) the names, always holding `$"sha2-256"

Examples

.crypto.algos[]

.crypto.hash[algo;data]

The digest named by algo: md4 md5 sha1 sha2-224 sha2-256 sha2-384 sha2-512 sha3-224 sha3-256 sha3-384 sha3-512 keccak224 keccak256 keccak384 keccak512 blake2b-512 blake2s-256; the keccak names answer SHA-3. .crypto.algos[] lists the names this OpenSSL computes; any other name is 'domain (OpenSSL 3 keeps md4 in its legacy provider, so it is usually absent).

Return: (byte list) the digest; a list of them over a collection
Parameters:
  • data - (any) the string, byte vector, symbol or char to digest, or a symbol vector / list of those for one digest each
  • algo - (symbol) the digest name; the same name as a string also reads
throws:
  • domain - a name this process has no digest for
  • type - data (or an element of it) is not text, bytes or a symbol

Examples

.crypto.hash[`$"sha2-256";"abc"]
raze string .crypto.hash[`md5;"abc"]
.crypto.hash[`$"sha2-256";("abc";`abc;0x616263)]

.crypto.hmac[algo;secret;data]

The HMAC (RFC 2104) of data under secret, with the digest named by algo as .crypto.hash names it.

Return: (byte list) the MAC, as long as the digest; a list of them over a collection
Parameters:
  • data - (any) the message, in the same forms, or a symbol vector / list of those for one MAC each under the same key
  • secret - (any) the key, ONE string, byte vector, symbol or char; "" is a key
  • algo - (symbol) the digest name, as .crypto.hash takes it
throws:
  • domain - a name this process has no digest for
  • type - secret is not one text, bytes or symbol, or data (or an element of it) is not

Examples

raze string .crypto.hmac[`$"sha2-256";"key";"abc"]
.crypto.hmac[`$"sha2-256";"key";`abc`hello]

.crypto.randbytes[n]

n cryptographically secure random bytes from OpenSSL's generator, which seeds itself from the OS; \S does not reach it.

Return: (byte list) n bytes
Parameters:
  • n - (long) an integer atom, 1 to 4294967295
throws:
  • domain - n outside 1 to 4294967295
  • type - n is not an integer

Examples

.crypto.randbytes 16

.crypto.sha256

SHA-256 of data: .crypto.hash[`$"sha2-256";].

Examples

raze string .crypto.sha256 "abc"