Blog | G5 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:

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

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

Exit mobile version