|
// retoor <retoor@molodetz.nl>
|
|
|
|
import "crypto" for Crypto, Hash
|
|
|
|
System.print("=== Crypto Module Demo ===\n")
|
|
|
|
System.print("--- Random Bytes ---")
|
|
var bytes = Crypto.randomBytes(16)
|
|
System.print("16 random bytes: %(bytes)")
|
|
System.print("As hex: %(Hash.toHex(bytes))")
|
|
|
|
System.print("\n--- Random Integers ---")
|
|
System.print("Random integers between 1 and 100:")
|
|
for (i in 0...5) {
|
|
var num = Crypto.randomInt(1, 101)
|
|
System.print(" %(num)")
|
|
}
|
|
|
|
System.print("\n--- Random Integers (different ranges) ---")
|
|
System.print("1-10: %(Crypto.randomInt(1, 11))")
|
|
System.print("0-255: %(Crypto.randomInt(0, 256))")
|
|
System.print("1000-9999: %(Crypto.randomInt(1000, 10000))")
|
|
|
|
System.print("\n--- MD5 Hashing ---")
|
|
var text = "Hello, World!"
|
|
var md5Bytes = Hash.md5(text)
|
|
var md5Hex = Hash.toHex(md5Bytes)
|
|
System.print("Text: %(text)")
|
|
System.print("MD5: %(md5Hex)")
|
|
|
|
System.print("\n--- SHA1 Hashing ---")
|
|
var sha1Bytes = Hash.sha1(text)
|
|
var sha1Hex = Hash.toHex(sha1Bytes)
|
|
System.print("SHA1: %(sha1Hex)")
|
|
|
|
System.print("\n--- SHA256 Hashing ---")
|
|
var sha256Bytes = Hash.sha256(text)
|
|
var sha256Hex = Hash.toHex(sha256Bytes)
|
|
System.print("SHA256: %(sha256Hex)")
|
|
|
|
System.print("\n--- Hashing Different Inputs ---")
|
|
var inputs = [
|
|
"",
|
|
"test",
|
|
"The quick brown fox jumps over the lazy dog",
|
|
"password123"
|
|
]
|
|
|
|
for (input in inputs) {
|
|
var hash = Hash.toHex(Hash.sha256(input))
|
|
var displayInput = input
|
|
if (input == "") displayInput = "(empty string)"
|
|
if (input.count > 30) displayInput = input[0...30] + "..."
|
|
System.print("%(displayInput)")
|
|
System.print(" -> %(hash)")
|
|
}
|
|
|
|
System.print("\n--- Password Hashing Example ---")
|
|
var password = "mysecretpassword"
|
|
var passwordHash = Hash.toHex(Hash.sha256(password))
|
|
System.print("Password: %(password)")
|
|
System.print("Hash: %(passwordHash)")
|
|
|
|
System.print("\n--- Generating Random Token ---")
|
|
var tokenBytes = Crypto.randomBytes(32)
|
|
var token = Hash.toHex(tokenBytes)
|
|
System.print("Random token (64 hex chars): %(token)")
|
|
|
|
System.print("\n--- Generating Random Session ID ---")
|
|
var sessionBytes = Crypto.randomBytes(16)
|
|
var sessionId = Hash.toHex(sessionBytes)
|
|
System.print("Session ID: %(sessionId)")
|
|
|
|
System.print("\n--- Hash Comparison (constant input) ---")
|
|
var data = "consistent input"
|
|
var hash1 = Hash.toHex(Hash.sha256(data))
|
|
var hash2 = Hash.toHex(Hash.sha256(data))
|
|
System.print("Same input produces same hash: %(hash1 == hash2)")
|
|
|
|
System.print("\n--- Practical Example: File Checksum ---")
|
|
var fileContent = "This is the content of a file.\nLine 2.\nLine 3."
|
|
var checksum = Hash.toHex(Hash.sha256(fileContent))
|
|
System.print("File content checksum (SHA256):")
|
|
System.print(" %(checksum)")
|
|
|
|
System.print("\n--- Practical Example: API Key Generation ---")
|
|
var apiKeyBytes = Crypto.randomBytes(24)
|
|
var apiKey = Hash.toHex(apiKeyBytes)
|
|
System.print("Generated API key: %(apiKey)")
|