Encryption, Decryption, and Hash Functions in Cryptography

ยท

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:

Returns:

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:

Returns:

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:

Decryption

Perform symmetric decryption.

Syntax:

crypto.cipher_decrypt(algorithm, padding, data, key, iv)

Checksum Calculations

CRC Algorithms

Various CRC algorithms available:

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:

  1. Initialize hash stream
  2. Update with data chunks
  3. 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.

๐Ÿ‘‰ Learn about advanced cryptographic techniques