Get a Pentest and security assessment of your IT network.

Cyber Security

BCrypt Digest Limit & Alternatives

TL;DR

BCrypt is designed for password hashing, not general-purpose digests. Its 72-character limit can cause issues if you try to store longer data. Use a dedicated hash function like SHA-256 or BLAKE3 instead.

Understanding the Problem

BCrypt (and its variants) are excellent for storing passwords securely because they include a salt and use adaptive hashing, making brute-force attacks harder. However, BCrypt has limitations when used outside of password storage:

  • 72-character limit: The output of BCrypt is typically limited to 72 characters. This isn’t enough for many general digest applications where longer hashes are preferred.
  • Performance: BCrypt is intentionally slow, which is good for passwords but inefficient for frequent hashing tasks.
  • Purpose-built: It’s designed specifically to resist password cracking attacks and doesn’t offer the flexibility of general hash functions.

Solution: Use a Dedicated Hash Function

For anything other than password storage, use a dedicated cryptographic hash function like SHA-256 or BLAKE3.

1. Choosing a Hash Function

  • SHA-256: A widely used and well-respected hash function. It produces a 256-bit (32-byte) hash, usually represented as a 64-character hexadecimal string.
  • BLAKE3: A modern hash function that is faster than SHA-256 while still providing excellent security. It’s also more flexible in terms of output length.

2. Implementing SHA-256 (Python Example)

Here’s how to use SHA-256 in Python:

import hashlib

data = "Your data to hash"
encoded_data = data.encode('utf-8') # Encode the string into bytes
hash_object = hashlib.sha256(encoded_data)
hex_dig = hash_object.hexdigest()
print(hex_dig)

This code snippet will output a 64-character hexadecimal representation of the SHA-256 hash.

3. Implementing BLAKE3 (Python Example)

Here’s how to use BLAKE3 in Python:

import blake3

data = "Your data to hash"
encoded_data = data.encode('utf-8') # Encode the string into bytes
hasher = blake3.blake3()
hasher.update(encoded_data)
digest = hasher.digest().hex()
print(digest)

This code snippet will output a hexadecimal representation of the BLAKE3 hash.

4. Considerations

  • Salt: Even with dedicated hash functions, always use a unique salt for each piece of data you’re hashing. This prevents rainbow table attacks.
  • Library Availability: Ensure the chosen hash function has reliable and well-maintained libraries available in your programming language.
  • Output Length: Choose an appropriate output length based on your security requirements. SHA-256 provides a good balance for most applications, but BLAKE3 allows you to customize this.
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