The crypto module provides comprehensive cryptographic functionalities including hashing, encryption/decryption, digital signatures, key generation, and secure random number generation.
Generates an MD5 hash for the given input.
- Parameters:
input- The data to hash (string or byte array) - Returns: MD5 hash as a hexadecimal string
- Example:
md5_hash := crypto.md5("hello")
Generates a SHA-1 hash for the given input.
- Parameters:
input- The data to hash (string or byte array) - Returns: SHA-1 hash as a hexadecimal string
Generates a SHA-224 hash for the given input.
- Parameters:
input- The data to hash (string or byte array) - Returns: SHA-224 hash as a hexadecimal string
Generates a SHA-256 hash for the given input.
- Parameters:
input- The data to hash (string or byte array) - Returns: SHA-256 hash as a hexadecimal string
Generates a SHA-384 hash for the given input.
- Parameters:
input- The data to hash (string or byte array) - Returns: SHA-384 hash as a hexadecimal string
Generates a SHA-512 hash for the given input.
- Parameters:
input- The data to hash (string or byte array) - Returns: SHA-512 hash as a hexadecimal string
Generates a SHA3-224 hash for the given input.
- Parameters:
input- The data to hash (string or byte array) - Returns: SHA3-224 hash as a hexadecimal string
Generates a SHA3-256 hash for the given input.
- Parameters:
input- The data to hash (string or byte array) - Returns: SHA3-256 hash as a hexadecimal string
Generates a SHA3-384 hash for the given input.
- Parameters:
input- The data to hash (string or byte array) - Returns: SHA3-384 hash as a hexadecimal string
Generates a SHA3-512 hash for the given input.
- Parameters:
input- The data to hash (string or byte array) - Returns: SHA3-512 hash as a hexadecimal string
Generates a BLAKE2b-256 hash for the given input.
- Parameters:
input- The data to hash (string or byte array) - Returns: BLAKE2b-256 hash as a hexadecimal string
Generates a BLAKE2b-512 hash for the given input.
- Parameters:
input- The data to hash (string or byte array) - Returns: BLAKE2b-512 hash as a hexadecimal string
Generates HMAC using MD5 hash function.
- Parameters:
key- HMAC key (string or byte array),data- Data to authenticate (string or byte array) - Returns: HMAC as hexadecimal string
Generates HMAC using SHA-1 hash function.
Generates HMAC using SHA-256 hash function.
Generates HMAC using SHA-384 hash function.
Generates HMAC using SHA-512 hash function.
Generates HMAC using SHA3-256 hash function.
Generates HMAC using SHA3-512 hash function.
Encrypts data using AES encryption.
- Parameters:
plaintext- Data to encrypt (string or byte array),key- Encryption key (string or byte array, 16/24/32 bytes for AES-128/192/256) - Returns: Encrypted data as byte array
Decrypts AES-encrypted data.
- Parameters:
ciphertext- Encrypted data (byte array),key- Decryption key (string or byte array) - Returns: Decrypted data as byte array
AES block size constant (16 bytes).
Generates RSA key pair.
- Parameters:
bits- Key size in bits (default: 2048) - Returns: Map with
privateandpublickeys in DER format
Encrypts data using RSA public key.
- Parameters:
data- Data to encrypt (string or byte array),public_key- Public key (DER bytes or PEM string) - Returns: Encrypted data as byte array
Decrypts RSA-encrypted data.
- Parameters:
ciphertext- Encrypted data (byte array),private_key- Private key (DER bytes or PEM string) - Returns: Decrypted data as byte array
Signs data using RSA private key.
- Parameters:
data- Data to sign (string or byte array),private_key- Private key (DER bytes or PEM string) - Returns: Signature as byte array
Verifies RSA signature.
- Parameters:
data- Original data (string or byte array),signature- Signature to verify (byte array),public_key- Public key (DER bytes or PEM string) - Returns: Boolean indicating if signature is valid
Exports key to PEM format.
- Parameters:
key- Key in DER format (byte array),key_type- "private" or "public" - Returns: PEM-encoded key as string
Imports key from PEM format.
- Parameters:
pem_data- PEM-encoded key (string or byte array),key_type- "private" or "public" - Returns: Key in DER format as byte array
Generates ECDSA key pair.
- Parameters:
curve- Elliptic curve name: "p224", "p256", "p384", or "p521" (default: "p256") - Returns: Map with
privateandpublickeys in DER format
Signs data using ECDSA private key.
- Parameters:
data- Data to sign (string or byte array),private_key- Private key (DER bytes or PEM string) - Returns: 64-byte signature (concatenated r and s values)
Verifies ECDSA signature.
- Parameters:
data- Original data (string or byte array),signature- 64-byte signature (byte array),public_key- Public key (DER bytes or PEM string) - Returns: Boolean indicating if signature is valid
Exports ECDSA key to PEM format.
Imports ECDSA key from PEM format.
Generates Ed25519 key pair.
- Returns: Map with
privateandpublickeys as byte arrays
Signs data using Ed25519 private key.
- Parameters:
data- Data to sign (string or byte array),private_key- Private key (byte array) - Returns: Signature as byte array
Verifies Ed25519 signature.
- Parameters:
data- Original data (string or byte array),signature- Signature (byte array),public_key- Public key (byte array) - Returns: Boolean indicating if signature is valid
Derives key using PBKDF2.
- Parameters:
password- Password (string or byte array),salt- Salt (string or byte array),iterations- Number of iterations,key_len- Desired key length,hash_func- Hash function name ("sha1", "sha256", "sha512", "sha3_256", "sha3_512") - Returns: Derived key as byte array
Hashes password using bcrypt.
- Parameters:
password- Password (string or byte array),cost- Computational cost (default: 10) - Returns: bcrypt hash as byte array
Derives key using scrypt.
- Parameters:
password- Password (string or byte array),salt- Salt (string or byte array),key_len- Desired key length,N- CPU/memory cost,r- Block size,p- Parallelization - Returns: Derived key as byte array
Derives key using Argon2id.
- Parameters:
password- Password (string or byte array),salt- Salt (string or byte array),time_cost- Time cost (iterations),memory_cost- Memory cost in KiB,threads- Parallelism,key_len- Desired key length - Returns: Derived key as byte array
Derives key using Argon2i.
Generates cryptographically secure random bytes.
- Parameters:
size- Number of bytes to generate - Returns: Random bytes as byte array
Generates cryptographically secure random integer.
- Parameters:
min- Minimum value,max- Maximum value - Returns: Random integer
Generates cryptographically secure random float.
- Returns: Random float between 0.0 and 1.0
Generates random UUID v4.
- Returns: UUID as string
Compares two values in constant time to prevent timing attacks.
- Parameters:
a,b- Values to compare (strings or byte arrays) - Returns: Boolean indicating if values are equal
import "crypto"
// Hashing examples
data := "Hello, World!"
println("MD5:", crypto.md5(data))
println("SHA256:", crypto.sha256(data))
// HMAC examples
key := "secret"
println("HMAC-SHA256:", crypto.hmac.sha256(key, data))
// AES encryption
aes_key := "0123456789abcdef0123456789abcdef" // 32 bytes
encrypted := crypto.aes.encrypt("secret message", aes_key)
decrypted := crypto.aes.decrypt(encrypted, aes_key)
println("Decrypted:", string(decrypted))
// RSA encryption
rsa_keys := crypto.rsa.generate_key(2048)
encrypted_rsa := crypto.rsa.encrypt("secret", rsa_keys.public)
decrypted_rsa := crypto.rsa.decrypt(encrypted_rsa, rsa_keys.private)
println("RSA Decrypted:", string(decrypted_rsa))
// Digital signatures
ec_keys := crypto.ecdsa.generate_key("p256")
signature := crypto.ecdsa.sign(data, ec_keys.private)
is_valid := crypto.ecdsa.verify(data, signature, ec_keys.public)
println("ECDSA Signature valid:", is_valid)
// Password hashing
password := "my_password"
salt := crypto.random.bytes(16)
argon_hash := crypto.argon2.id(password, salt, 3, 64*1024, 4, 32)
println("Argon2 hash:", argon_hash)
// Random generation
println("Random bytes:", crypto.random.bytes(32))
println("Random int:", crypto.random.int(1, 100))
println("UUID:", crypto.random.uuid())