crypto - Cryptography

Requires: OpenSSL (auto-installed via minks install kse_crypto)

#include "kse_crypto"

AES-256-CBC encryption, SHA-512 hashing, HMAC-SHA256, PBKDF2 key derivation, and cryptographically secure random bytes.


Method Args Out Description
Crypto:AESEncrypt keyHex, ivHex, plaintext String AES-256-CBC encrypt → base64 ciphertext. keyHex = 64 hex chars (32 bytes), ivHex = 32 hex chars (16 bytes)
Crypto:AESDecrypt keyHex, ivHex, cipherBase64 String AES-256-CBC decrypt → plaintext
Crypto:SHA512 input String SHA-512 hash → 128-char hex string
Crypto:HMAC256 keyHex, message String HMAC-SHA256 → 64-char hex string
Crypto:PBKDF2 password, saltHex, iterations, keyLen String PBKDF2-SHA256 key derivation → hex string of keyLen bytes
Crypto:Random byteCount String Cryptographically secure random bytes → hex string

Throws CryptoException on failure. See Object - Exception.

Var:String key;
Var:String iv;
Var:String cipher;
Var:String plain;
Var:String hash;

// Generate a random 32-byte key and 16-byte IV
Crypto:Random(32, key);
Crypto:Random(16, iv);

Crypto:AESEncrypt(key, iv, "Hello, World!", cipher);
Crypto:AESDecrypt(key, iv, cipher, plain);
Konsol:Print(plain);   // Hello, World!

Crypto:SHA512("my password", hash);
Konsol:Print(hash);    // 128-char hex

Examples

// test_crypto.ks - Crypto plugin test
//
// Build and install:
//   cd crypto_plugin && make
//   minks install kse_crypto
//
// Or load explicitly:
//   minks --plugin crypto_plugin\kse_crypto.dll crypto_plugin\test_crypto.ks   (Windows)
//   minks --plugin ./crypto_plugin/kse_crypto.so crypto_plugin/test_crypto.ks  (Linux)
//   minks --plugin ./crypto_plugin/kse_crypto.dylib crypto_plugin/test_crypto.ks  (Mac)

#include "kse_crypto"

Konsol:Print("=== Crypto Plugin Test ===");
Konsol:Print("");

// -- Random bytes -------------------------------------------------------------

Var:String key;
Var:String iv;

Crypto:Random(32, key);   // 32 bytes → 64-char hex (AES-256 key)
Crypto:Random(16, iv);    //  16 bytes → 32-char hex (AES-CBC IV)

Konsol:Print("Random key (32 bytes): ${key}");
Konsol:Print("Random IV  (16 bytes): ${iv}");
Konsol:Print("");

// -- AES-256-CBC encrypt / decrypt --------------------------------------------

Var:String plaintext  = "Hello from KonsolScript!";
Var:String ciphertext;
Var:String decrypted;

try {
    Crypto:AESEncrypt(key, iv, plaintext, ciphertext);
    Crypto:AESDecrypt(key, iv, ciphertext, decrypted);
} catch (CryptoException e) {
    Konsol:Print("AES error: ${e.message}");
    Konsol:Exit(1);
}

Konsol:Print("Plaintext:  ${plaintext}");
Konsol:Print("Encrypted:  ${ciphertext}");
Konsol:Print("Decrypted:  ${decrypted}");
Konsol:Print("");

// -- SHA-512 hash -------------------------------------------------------------

Var:String hash;
Crypto:SHA512("my secret password", hash);
Konsol:Print("SHA-512:");
Konsol:Print(hash);
Konsol:Print("");

// -- HMAC-SHA256 --------------------------------------------------------------

Var:String hmac;
Crypto:HMAC256(key, "message to authenticate", hmac);
Konsol:Print("HMAC-SHA256: ${hmac}");
Konsol:Print("");

// -- PBKDF2 key derivation ----------------------------------------------------

Var:String salt;
Var:String derived;

Crypto:Random(16, salt);
Crypto:PBKDF2("user password", salt, 100000, 32, derived);
Konsol:Print("PBKDF2 derived key (100k iterations, 32 bytes):");
Konsol:Print(derived);
Konsol:Print("");

Konsol:Print("All crypto tests passed.");


Building from source

On MSYS2, OpenSSL is typically already present as a dependency of curl - check with pkg-config --exists openssl && echo found before installing.

Platform Command
Debian/Ubuntu sudo apt install libssl-dev
Fedora/RHEL sudo dnf install openssl-devel
macOS brew install openssl
MSYS2 MINGW64 pacman -S mingw-w64-x86_64-openssl
MSYS2 UCRT64 pacman -S mingw-w64-ucrt-x86_64-openssl
MSYS2 CLANG64 pacman -S mingw-w64-clang-x86_64-openssl
MSYS2 CLANGARM64 pacman -S mingw-w64-clang-aarch64-openssl

The Makefile auto-detects via pkg-config. For native Windows, download Win64 OpenSSL and override OPENSSL_DIR if not using the default (C:/OpenSSL-Win64):

make -C crypto_plugin OPENSSL_DIR="C:/your/openssl"