MAC, PRNG & KDF

1. HMAC — từng bước tính toán

Nhập key và message để xem HMAC-SHA256 tính như thế nào. Sử dụng Web Crypto API — giá trị thật.

Key (hex, padded to 64 bytes)
K_inner = Key ⊕ ipad (0x36...)
K_outer = Key ⊕ opad (0x5c...)
Inner hash = SHA256(K_inner ‖ message)
HMAC-SHA256 = SHA256(K_outer ‖ inner_hash)
HMAC fix length-extension: inner hash hoàn thành trước → outer hash nhận input đầy đủ, không thể extend. Dùng cho API authentication (AWS Sig v4, JWT HS256).

2. LCG — đoán output tiếp theo

LCG: Xn+1 = (a·Xn + c) mod m. Sau khi thấy 3 output, ta đoán được X4 và mọi output sau.

⚠ LCG là tuyến tính: biết 3 output → giải a, c → predict vô hạn. KHÔNG dùng cho crypto. Dùng crypto.getRandomValues() (browser) hay crypto/rand (Go).

3. CSPRNG vs LCG — entropy visualization

Mỗi chấm = 1 byte random. LCG có chu kỳ ngắn → pattern lặp. CSPRNG trông hoàn toàn ngẫu nhiên.

LCG (insecure)

CSPRNG (Web Crypto)

4. KDF — cost parameters & brute-force time

Điều chỉnh parameters và xem thời gian brute-force 8-char password.

100 ms
1
a-z (26 chars)
8 ký tự

So sánh KDF algorithms

PBKDF2-SHA256

~80ms
600,000 iter (OWASP 2023). Không memory-hard → GPU-friendly.
FIPS / Legacy

bcrypt (cost=12)

~100ms
Battle-tested 25 năm. Giới hạn 72 byte input! Không memory-hard.
⚠ 72-byte limit

scrypt (N=2¹⁶)

~100ms, 64MB
Memory-hard — ASIC-resistant hơn bcrypt. Khó tune parameters.
Memory-hard

Argon2id

~100ms, 64MB
PHC winner 2015. Memory + CPU hard. Side-channel resistant. Recommended.
✓ Default 2024