Get a Pentest and security assessment of your IT network.

Cyber Security

SHA256 Recovery from Username Hashes

TL;DR

No, a secret S cannot be efficiently recovered if you only have a list of usernames and the SHA256 hashes of S + username. This is because SHA256 is a one-way function – it’s easy to calculate the hash from the input, but extremely difficult (practically impossible) to reverse engineer the input from the hash alone.

Why It Doesn’t Work

SHA256 creates a fixed-size ‘fingerprint’ of data. Different inputs will produce different hashes, and even a tiny change in the input drastically alters the hash. This makes it very hard to guess the original secret.

The Problem Explained

You have:

  • A secret S (what you’re trying to find).
  • A list of usernames: username1, username2, username3...
  • For each username, the hash: SHA256(S + username1), SHA256(S + username2), SHA256(S + username3)...

The goal is to find S. Because you don’t know S, you can’t verify if a guessed value of S is correct without knowing the original hashes.

What You *Can’t* Do

  1. Brute-Force: Trying every possible secret S. This becomes impossible very quickly as the length of S increases. Even with a fast computer, it would take an unfeasibly long time to test all combinations.
  2. Reverse SHA256: There is no known efficient algorithm to reverse SHA256. Tools claiming to do this are usually using pre-computed tables (rainbow tables) or trying common passwords/phrases – they won’t work for arbitrary secrets.

What You *Could* Do (If Possible, but unlikely)

These scenarios require additional information beyond what you’ve described.

  1. Known Username and Hash: If you know one username username1 and its corresponding hash SHA256(S + username1), you can try to find S by testing possible values. However, this is still computationally expensive if S is long.
    # Example in Python (very slow for anything but short S)
    import hashlib
    
    def find_s(username, hash_value):
      for i in range(1000): # Try secrets from 0 to 999
        secret = str(i)
        calculated_hash = hashlib.sha256((secret + username).encode()).hexdigest()
        if calculated_hash == hash_value:
          return secret
      return None
    
    username = "testuser"
    hash_value = "your_known_hash_here"
    s = find_s(username, hash_value)
    if s:
      print("Secret found: ", s)
    else:
      print("Secret not found within the tested range.")
    
  2. Salted Hashes with Weak Salts: If the usernames are used as salts, and there’s a limited number of possible usernames (e.g., short, common names), you could attempt to pre-compute hashes for all possible S + username combinations. This is still resource intensive but more feasible than brute-forcing S directly.
  3. Vulnerability in the System: If there’s a flaw in how the system generates or stores the usernames and hashes, it might be possible to exploit that vulnerability to recover S. This is highly specific to the implementation of the system.

Conclusion

Without additional information, recovering the secret S from a list of usernames and their SHA256(S + username) hashes is practically impossible due to the nature of cryptographic hash functions. Focus on securing the storage of S itself rather than relying on this method for security.

Related posts
Cyber Security

Zip Codes & PII: Are They Personal Data?

Cyber Security

Zero-Day Vulnerabilities: User Defence Guide

Cyber Security

Zero Knowledge Voting with Trusted Server

Cyber Security

ZeroNet: 51% Attack Risks & Mitigation