Skip to main content

Security

linkiir.sec

Security primitives with modern, explicit parameters. Grouped into hash (digests/MAC/KDF), cipher (symmetric AES) and key (asymmetric RSA).


linkiir.sec.hash

function

linkiir.sec.hash{ algorithm='sha256', data=, hex=true }

Compute a digest string.

Compute a one-way digest (e.g. sha256, sha1, md5) of the given data.

Usage

linkiir.sec.hash{ algorithm='sha256', data=, hex=true }

Parameters

NameTypeRequiredDescription
algorithmstringNoHash algorithm, default 'sha256' (e.g. 'sha1', 'sha256', 'sha512', 'md5').
datastringYesData to hash.
hexbooleanNoReturn a hex-encoded string when true (default); raw bytes otherwise.

Returns

  • digest string — the computed value.

Errors

Raises a Lua error on failure.

Codes: INVALID_PARAMETER, UNSUPPORTED

Example

local Digest = linkiir.sec.hash{ algorithm = 'sha256', data = Payload, hex = true }
local Mac = linkiir.sec.hmac{ algorithm = 'sha256', key = Key, data = Payload }

linkiir.sec.hmac

function

linkiir.sec.hmac{ algorithm='sha256', key=, data=, hex=true }

Compute a MAC string.

Compute a keyed message authentication code (HMAC) over the given data.

Usage

linkiir.sec.hmac{ algorithm='sha256', key=, data=, hex=true }

Parameters

NameTypeRequiredDescription
algorithmstringNoHash algorithm, default 'sha256'.
keystringYesHMAC key.
datastringYesData to authenticate.
hexbooleanNoReturn a hex-encoded string when true (default); raw bytes otherwise.

Returns

  • MAC string — the computed value.

Errors

Raises a Lua error on failure.

Codes: INVALID_PARAMETER, UNSUPPORTED

Example

local Digest = linkiir.sec.hash{ algorithm = 'sha256', data = Payload, hex = true }
local Mac = linkiir.sec.hmac{ algorithm = 'sha256', key = Key, data = Payload }

linkiir.sec.pbkdf2

function

linkiir.sec.pbkdf2{ password=, salt=, iterations=, length=, algorithm='sha256' }

Compute a derived key.

Derive a key from a password using PBKDF2.

Usage

linkiir.sec.pbkdf2{ password=, salt=, iterations=, length=, algorithm='sha256' }

Parameters

NameTypeRequiredDescription
passwordstringYesPassword to derive from.
saltstringYesRandom salt bytes.
iterationsintegerYesIteration count.
lengthintegerYesDesired derived-key length in bytes.
algorithmstringNoHash algorithm, default 'sha256'.

Returns

  • derived key — the computed value.

Errors

Raises a Lua error on failure.

Codes: INVALID_PARAMETER, UNSUPPORTED

Example

local Key = linkiir.sec.pbkdf2{ password = Password, salt = Salt,
iterations = 100000, length = 32, algorithm = 'sha256' }

linkiir.sec.cipher.encrypt

function

linkiir.sec.cipher.encrypt{ key=, iv=, mode=, data= }

AES encryption.

Encrypt data using AES with the given key, IV, and mode.

Usage

local cipher, err = linkiir.sec.cipher.encrypt{ key=, iv=, mode='gcm', data=, aad= }

Parameters

NameTypeRequiredDescription
keystringYesKey bytes.
ivstringYesInitialization vector bytes.
datastringYesPlaintext (encrypt) or ciphertext (decrypt).
modestringNo'gcm' (default), 'cbc', 'ctr'.
aadstringNoAdditional authenticated data (AEAD modes).

Returns

  • cipher/plain string on success
  • nil, err on failure

Errors

Returns result, err.

Codes: INVALID_KEY, BAD_TAG, CIPHER_ERROR

Example

local Cipher, Err = linkiir.sec.cipher.encrypt{ key = Key, iv = Iv, mode = 'gcm', data = Plain }
local Plain, Err2 = linkiir.sec.cipher.decrypt{ key = Key, iv = Iv, mode = 'gcm', data = Cipher }

linkiir.sec.cipher.decrypt

function

linkiir.sec.cipher.decrypt{ key=, iv=, mode=, data= }

AES decryption.

Decrypt AES-encrypted data using the given key, IV, and mode.

Usage

local plain, err = linkiir.sec.cipher.decrypt{ key=, iv=, mode='gcm', data=, aad= }

Parameters

NameTypeRequiredDescription
keystringYesKey bytes.
ivstringYesInitialization vector bytes.
datastringYesPlaintext (encrypt) or ciphertext (decrypt).
modestringNo'gcm' (default), 'cbc', 'ctr'.
aadstringNoAdditional authenticated data (AEAD modes).

Returns

  • cipher/plain string on success
  • nil, err on failure

Errors

Returns result, err.

Codes: INVALID_KEY, BAD_TAG, CIPHER_ERROR

Example

local Cipher, Err = linkiir.sec.cipher.encrypt{ key = Key, iv = Iv, mode = 'gcm', data = Plain }
local Plain, Err2 = linkiir.sec.cipher.decrypt{ key = Key, iv = Iv, mode = 'gcm', data = Cipher }

linkiir.sec.key.encrypt

function

linkiir.sec.key.encrypt{ key=<pem>, data=, padding='oaep' }

RSA encrypt.

RSA-encrypt data with the given key.

Usage

linkiir.sec.key.encrypt{ key=<pem>, data=, padding='oaep' }

Parameters

NameTypeRequiredDescription
keystringYesPEM-encoded public key.
datastringYesPlaintext to encrypt.
paddingstringNoPadding scheme, default 'oaep'.

Returns

  • Result (see usage).

Errors

Returns result, err.

Codes: INVALID_KEY, VERIFY_FAILED, CIPHER_ERROR

Example

local Cipher, Err = linkiir.sec.key.encrypt{ key = PubKey, data = Payload }
if not Cipher then error(Err.message) end

linkiir.sec.key.decrypt

function

linkiir.sec.key.decrypt{ key=<pem>, data=, padding='oaep' }

RSA decrypt.

RSA-decrypt data with the given key.

Usage

linkiir.sec.key.decrypt{ key=<pem>, data=, padding='oaep' }

Parameters

NameTypeRequiredDescription
keystringYesPEM-encoded private key.
datastringYesCiphertext to decrypt.
paddingstringNoPadding scheme, default 'oaep'.

Returns

  • Result (see usage).

Errors

Returns result, err.

Codes: INVALID_KEY, VERIFY_FAILED, CIPHER_ERROR

Example

local Plain, Err = linkiir.sec.key.decrypt{ key = PrivKey, data = Cipher }
if not Plain then error(Err.message) end

linkiir.sec.key.sign

function

linkiir.sec.key.sign{ key=<privPem>, data=, algorithm='sha256' }

RSA sign.

Sign data with an RSA private key.

Usage

linkiir.sec.key.sign{ key=<privPem>, data=, algorithm='sha256' } → signature

Parameters

NameTypeRequiredDescription
keystringYesPEM-encoded private key.
datastringYesData to sign.
algorithmstringNoHash algorithm, default 'sha256'.

Returns

  • signature

Errors

Returns result, err.

Codes: INVALID_KEY, VERIFY_FAILED, CIPHER_ERROR

Example

local Sig = linkiir.sec.key.sign{ key = PrivKey, data = Payload, algorithm = 'sha256' }
local Ok = linkiir.sec.key.verify{ key = PubKey, data = Payload, signature = Sig }

linkiir.sec.key.verify

function

linkiir.sec.key.verify{ key=<pubPem>, data=, signature= }

RSA verify.

Verify an RSA signature against data using the public key.

Usage

linkiir.sec.key.verify{ key=<pubPem>, data=, signature= } → boolean

Parameters

NameTypeRequiredDescription
keystringYesPEM-encoded public key.
datastringYesOriginal data.
signaturestringYesSignature to verify.

Returns

  • boolean

Errors

Returns result, err.

Codes: INVALID_KEY, VERIFY_FAILED, CIPHER_ERROR

Example

local Sig = linkiir.sec.key.sign{ key = PrivKey, data = Payload, algorithm = 'sha256' }
local Ok = linkiir.sec.key.verify{ key = PubKey, data = Payload, signature = Sig }

linkiir.sec.info

function

linkiir.sec.info()

Available algorithms & library metadata.

Return crypto library metadata.

Usage

local info = linkiir.sec.info()

Returns

  • { version=, ciphers={...}, digests={...} }

Errors

Raises a Lua error on failure.

Codes: RUNTIME_ERROR

Example

local Info = linkiir.sec.info()
print(Info.version)