Skip to main content

porla_crypto

Wraps libsodium and provides various cryptographic operations.

Exports​

auth(input: string, key: string): string​

v0.44.0

Returns a 32-byte HMAC-SHA512-256.

Raises​

  • invalid key size if the key is not exactly 32 bytes.

auth_keygen(): string​

v0.44.0

Generates a key that is suitable for auth and auth_verify.

auth_verify(mac: string, input: string, key: string): boolean​

v0.44.0

Raises​

  • invalid key size
  • invalid mac size

hash(input: string): string​

v0.44.0

Hashes input and returns a 32-byte BLAKE2b hash.

hash(input: string, key: string): string​

v0.44.0

Hashes input and returns a 32-byte BLAKE2b hash, or nil if the key is longer than 64 bytes.

hash_init(key: string): SoHashState | nil​

v0.44.0

memcmp(a: string, b: string): boolean​

v0.44.0

Compares two string in constant time. Use this to avoid leaking information when comparing secrets such as hashes, etc.

Returns false when lengths differ.

pwhash(input: string, callback: function): void​

v0.44.0

Derives a password hash from input. The result is passed to the callback.

Example​

local crypto = require("porla_crypto")

crypto.pwhash("hunter2", function(err, hash)
if err then
-- failed to hash password
return
end
print("hash", hash)
end)

pwhash_verify(hash: string, input: string, callback: function): void​

v0.44.0

Verifies input against a hash produced by pwhash.

Example​

crypto.pwhash_verify("stored hash", "user input", function(err, ok)
if not ok then
return
return

-- password hash verified
end)

randombytes(n: number): string​

v0.44.0

Returns n number of random bytes in a binary string.

Accepts n 0-4096.

Raises​

  • size must be non-negative
  • size exceeds maximum of 4096 bytes

randombytes_uniform(n: number): number​

v0.44.0

Returns a uniformly distributed random integer in the range [0, n).

Raises​

  • upper_bound must be positive
  • upper_bound too large

secretbox(message: string, key: string): string​

v0.44.0

Encrypts and authenticates message with the given key and returns the result as a binary string.

Key must be exactly 32 bytes.

secretbox_keygen(): string​

v0.44.0

Generates a random secret key suitable for use with secretbox and secretbox_open.

secretbox_open(box: string, key: string): string | nil​

v0.44.0

Verifies and decrypts box with the given key. Returns the original message, or nil if the input is too short, has the wrong version or key, or has been tampered with.