This page is a placeholder. All examples on this page are currently AI-generated and are not correct. This documentation will be completed in the future with accurate, tested examples.
Security Level: 128 bitsSHA-256 provides strong collision resistance, making it computationally infeasible to find two different inputs that produce the same hash output.Attack Complexity:
Generic birthday attack: ~2^128 operations
Best known attack: No practical collision attack exists
Practical Security:
Copy
Ask AI
// Finding a collision requires approximately 2^128 hash computations// At 1 trillion hashes/second: ~10^19 years// Current age of universe: ~1.4 × 10^10 years// Collision attack is not practically feasible
The birthday paradox reduces collision attack complexity from 2^256 to 2^128. This is why SHA-256’s collision resistance is 128 bits despite 256-bit output.
Security Level: 256 bitsGiven an input m1 and its hash h = SHA256(m1), it is computationally infeasible to find a different input m2 such that SHA256(m2) = h.Attack Complexity:
Brute force: ~2^256 operations
Best known attack: No practical second preimage attack
Importance:
Prevents attackers from substituting malicious data with the same hash
Vulnerability: SHA-256 is vulnerable to length extension attacks.What It Means:
Given H(message) and len(message), an attacker can compute H(message || padding || extension) without knowing the original message.Example Vulnerable Code:
Copy
Ask AI
// INSECURE: Don't use hash alone for authenticationfunction insecureAuth(message: Uint8Array, secret: Uint8Array): Uint8Array { const combined = new Uint8Array([...secret, ...message]); return SHA256.hash(combined); // Vulnerable to length extension!}
Mitigation - Use HMAC:
Copy
Ask AI
// SECURE: Use HMAC-SHA256 insteadfunction hmacSha256(key: Uint8Array, message: Uint8Array): Uint8Array { const blockSize = 64; // Key derivation let derivedKey = key.length > blockSize ? SHA256.hash(key) : key; const paddedKey = new Uint8Array(blockSize); paddedKey.set(derivedKey); // HMAC computation const opad = new Uint8Array(blockSize).fill(0x5c); const ipad = new Uint8Array(blockSize).fill(0x36); for (let i = 0; i < blockSize; i++) { opad[i] ^= paddedKey[i]; ipad[i] ^= paddedKey[i]; } const innerHash = SHA256.hash(new Uint8Array([...ipad, ...message])); return SHA256.hash(new Uint8Array([...opad, ...innerHash]));}// Now secure against length extensionconst mac = hmacSha256(secret, message);
// INSECURE: SHA-256 is too fast for passwordsconst passwordHash = SHA256.hash(new TextEncoder().encode(password));// Vulnerable to brute force (billions of hashes/second)// SECURE: Use proper password hashimport { scrypt } from 'crypto';scrypt(password, salt, 32, { N: 2**16, r: 8, p: 1 }, callback);
Message Authentication (without HMAC):
Copy
Ask AI
// INSECURE: Vulnerable to length extensionconst mac = SHA256.hash(new Uint8Array([...secret, ...message]));// SECURE: Use HMAC-SHA256const mac = hmacSha256(secret, message);
Generating Random Keys:
Copy
Ask AI
// INSECURE: Hashing predictable inputconst badKey = SHA256.hash(new TextEncoder().encode(Date.now().toString()));// SECURE: Use cryptographically secure random generatorconst goodKey = crypto.getRandomValues(Bytes32());
SHA-256 implementations should use constant-time operations to resist timing attacks.Vulnerable Code:
Copy
Ask AI
// INSECURE: Early return leaks timing informationfunction insecureCompare(hash1: Uint8Array, hash2: Uint8Array): boolean { for (let i = 0; i < hash1.length; i++) { if (hash1[i] !== hash2[i]) return false; // Timing leak! } return true;}
Secure Code:
Copy
Ask AI
// SECURE: Constant-time comparisonfunction secureCompare(hash1: Uint8Array, hash2: Uint8Array): boolean { if (hash1.length !== hash2.length) return false; let result = 0; for (let i = 0; i < hash1.length; i++) { result |= hash1[i] ^ hash2[i]; } return result === 0; // No early return}