Introduction to Cryptographic Functions
Cryptographic functions are essential tools in modern computing, providing security for data transmission and storage. This guide explores various encryption, decryption, and hashing methods available in cryptographic libraries.
Hash Functions
MD5 Hashing
Calculate the MD5 hash value of a string.
Syntax:
crypto.md5(str)Parameters:
str: Input string to hash
Returns:
- Hex string representing the MD5 hash
Example:
local md5_hash = crypto.md5("example_string")HMAC-MD5
Calculate HMAC using MD5 as the underlying hash function.
Syntax:
crypto.hmac_md5(str, key)Parameters:
str: Input stringkey: Secret key
Returns:
- Hex string of HMAC-MD5 value
SHA Family of Hash Functions
SHA-1 Hashing
Calculate SHA-1 hash value.
Syntax:
crypto.sha1(str)SHA-256 Hashing
Calculate SHA-256 hash value.
Syntax:
crypto.sha256(str)SHA-512 Hashing
Calculate SHA-512 hash value.
Syntax:
crypto.sha512(str)Symmetric Encryption
Encryption
Perform symmetric encryption using specified algorithm.
Syntax:
crypto.cipher_encrypt(algorithm, padding, data, key, iv)Parameters:
algorithm: Encryption algorithm (e.g., "AES-128-CBC")padding: Padding schemedata: Data to encryptkey: Encryption keyiv: Initialization vector
Decryption
Perform symmetric decryption.
Syntax:
crypto.cipher_decrypt(algorithm, padding, data, key, iv)Checksum Calculations
CRC Algorithms
Various CRC algorithms available:
- CRC16
- CRC32
- CRC8
- CRC7
Example:
local crc_value = crypto.crc32("data_to_check")Random Number Generation
True Random Numbers
Generate cryptographically secure random numbers.
Syntax:
crypto.trng(length)TOTP Generation
Generate Time-based One-Time Password.
Syntax:
crypto.totp(secret, [timestamp])Base64 Encoding/Decoding
Encoding
Syntax:
crypto.base64_encode(data)Decoding
Syntax:
crypto.base64_decode(data)Advanced Features
Streaming Hash Calculation
For large data sets, use streaming interface:
- Initialize hash stream
- Update with data chunks
- Finalize to get hash
Example:
local stream = crypto.hash_init("SHA256")
crypto.hash_update(stream, "data_part1")
crypto.hash_update(stream, "data_part2")
local final_hash = crypto.hash_finish(stream)๐ Explore more cryptographic tools
FAQ Section
Q: What's the difference between encryption and hashing?
A: Encryption is reversible (can be decrypted), while hashing is one-way and used primarily for verification.
Q: When should I use HMAC instead of regular hash?
A: Use HMAC when you need to verify both data integrity and authenticity using a secret key.
Q: Is MD5 still secure?
A: MD5 is considered cryptographically broken for security purposes due to vulnerability to collision attacks.
Q: What's the advantage of streaming hash functions?
A: Streaming allows hashing of large files without loading entire contents into memory.
Q: How do I choose between AES-CBC and AES-ECB?
A: ECB is simpler but less secure for patterns in data; CBC is more secure but requires an IV.