Wednesday, May 28, 2025

How to Securing Passwords Against Quantum Computers

Why special care for passwords?

Passwords are stored as hashes, not encrypted, to prevent attackers from recovering the original password if the database leaks. However, with quantum computers, classical password hashing algorithms could become easier to brute force.

Quantum Threats to Password Hashing

  • Grover’s algorithm speeds up brute-force attacks quadratically on symmetric cryptography and hash functions.
  • This means attackers could try roughly the square root of password guesses in the same time compared to classical brute force.
  • Password hashing needs to be computationally expensive and memory-hard, making each guess costly on quantum hardware.

Best Practices for Quantum-Resistant Password Hashing

  1. Use slow, memory-hard hashing algorithms designed for password storage like Argon2, scrypt, or bcrypt.
  2. Always use a unique random salt per password.
  3. Use sufficiently large parameters (iterations, memory, CPU cost) to slow brute forcing.
  4. Use constant-time verification to avoid timing attacks.

C# Examples for Quantum-Resistant Password Hashing:


1. Argon2 — Recommended for quantum resistance

// Install NuGet Package: Install-Package Isopoh.Cryptography.Argon2
using Isopoh.Cryptography.Argon2;
using System;

class Program
{
    static void Main()
    {
        string password = "MyQuantumSafePassword123!";

        // Hash the password with Argon2id
        string hash = Argon2.Hash(password);

        Console.WriteLine($"Argon2 Hash: {hash}");

        // Verify the password
        bool valid = Argon2.Verify(hash, password);
        Console.WriteLine($"Password valid? {valid}");
    }
}

2. bcrypt — Widely used, moderately quantum-resistant

// Install NuGet Package: Install-Package BCrypt.Net-Next
using BCrypt.Net;

class Program
{
    static void Main()
    {
        string password = "MyQuantumSafePassword123!";

        // Generate bcrypt hash
        string hash = BCrypt.Net.BCrypt.HashPassword(password);

        Console.WriteLine($"bcrypt Hash: {hash}");

        // Verify the password
        bool valid = BCrypt.Net.BCrypt.Verify(password, hash);
        Console.WriteLine($"Password valid? {valid}");
    }
}

3. scrypt — Memory-hard, good for resisting quantum attacks

// Install NuGet Package: Install-Package CryptSharpOfficial
using CryptSharp;

class Program
{
    static void Main()
    {
        string password = "MyQuantumSafePassword123!";

        // Generate scrypt hash
        string hash = Crypter.Scrypt.Crypt(password);

        Console.WriteLine($"scrypt Hash: {hash}");

        // Verify the password
        bool valid = Crypter.CheckPassword(password, hash);
        Console.WriteLine($"Password valid? {valid}");
    }
}

4. PBKDF2 — Built-in, less memory-hard, but still usable with high iteration count

using System;
using System.Security.Cryptography;

class Program
{
    static void Main()
    {
        string password = "MyQuantumSafePassword123!";

        // Generate a 16-byte salt
        byte[] salt = new byte[16];
        using (var rng = RandomNumberGenerator.Create())
        {
            rng.GetBytes(salt);
        }

        // Derive a 256-bit key using PBKDF2 with 100,000 iterations and SHA256
        var pbkdf2 = new Rfc2898DeriveBytes(password, salt, 100_000, HashAlgorithmName.SHA256);
        byte[] hash = pbkdf2.GetBytes(32);

        // Convert to base64 for storage
        string saltBase64 = Convert.ToBase64String(salt);
        string hashBase64 = Convert.ToBase64String(hash);

        Console.WriteLine($"Salt: {saltBase64}");
        Console.WriteLine($"Hash: {hashBase64}");

        // Verification
        var pbkdf2Verify = new Rfc2898DeriveBytes(password, salt, 100_000, HashAlgorithmName.SHA256);
        byte[] hashToVerify = pbkdf2Verify.GetBytes(32);

        bool valid = CryptographicOperations.FixedTimeEquals(hash, hashToVerify);
        Console.WriteLine($"Password valid? {valid}");
    }
}

No comments:

Post a Comment