Copied from wiki.php.net, last synchronized Nov 2, 2026 15:46 UTC
Bcrypt's 72-byte limit on passwords and issue with null bytes are inviting many userspace workarounds, poor tradeoffs, security vulnerabilities, UX issues, and considerable toil for developers.
Multiple attempts to solve these problems have so far failed:
PASSWORD_DEFAULT: php-internals threadThere is (not PHP-specific) consensus that a wrapping, prehashed-bcrypt construct is the solution (see FAQ), but no particular choice for the prehash primitive has emerged as the de facto standard. Some are suboptimal in strength, others are handicapped by implementers' choices. Each has its critics, or at least enough imperfection to get the “another hash is better” reaction.
This RFC also proposes a prehashed-bcrypt construct, named “bcrypt-wrap”. This new algorithm not only entirely eliminates Bcrypt's input length and null byte limitations, but also:
The overall pattern is the same, the details are what make the difference.
Add a new algorithm “bcrypt-wrap” (value of sole new constant PASSWORD_BCRYPT_WRAP), which has the following characteristics:
PASSWORD_BCRYPT_DEFAULT_COST.prehash option: cshake256 (default) and sha512 (using HMAC construct).<?php
$password = "A really, really long passw\x0rd, that's over 72 bytes and also includes a null byte";
$hash = password_hash($password, PASSWORD_BCRYPT_WRAP);
echo $hash, "\n";
// $bcrypt-wrap$v=0$cost=12,prehash=cshake256$+Thtm389lrboo7t9btjPVw$t76sKAokMxK5jjH918oNiRRXu0WWxd0
var_dump(password_verify($password, $hash)); // bool(true)
var_dump(password_verify($password." + bcrypt would ignore this", $hash)); // bool(false)
var_dump(password_get_info($hash));
/*
array(3) {
["algo"]=>
string(11) "bcrypt-wrap"
["algoName"]=>
string(11) "bcrypt-wrap"
["options"]=>
array(2) {
["cost"]=>
int(12)
["prehash"]=>
string(9) "cshake256"
}
}
*/
$hash = password_hash($password, PASSWORD_BCRYPT_WRAP, ["cost" => 4, "prehash" => "sha512"]);
echo $hash, "\n";
// $bcrypt-wrap$v=0$cost=4,prehash=sha512$jjAqrmWi61kah3sXIk4uLQ$Lru7YmJuOYolnxjzxJzvew2h5S7QBSQ
The output hash looks like so:
// Total output length varies between 93 and 97 characters, assuming 1-digit version
$bcrypt-wrap$v=0$cost=<bcrypt cost>,prehash=<cshake256|sha512>$<salt>$<hash>
v = Version
cost = Bcrypt cost, but not 0-padded
prehash = The underlying prehash primitive
salt = B64-encoded salt, 22 bytes (technically expandable, as the prehash isn't limited by Bcrypt)
hash = B64-encoded Bcrypt hash, 31 bytes
Most of that is self-explanatory even without reading the PHCs basics. Here are the non-obvious parts:
password_hash() pseudo-code:
password = <user input>
version = 0
bcrypt_cost = <developer-chosen; existing PHP default>
prehash_func = <developer-chosen; default "cshake256">
prehash_size = 54 // yields 72 bytes base64 to fill bcrypt space
salt_raw = random_bytes(16)
// Domain separation
prehash_salt = "bcrypt-wrap:" || version || ":" || salt_raw
if (prehash_func == "cshake256") {
prehash_raw = cshake256(
password,
prehash_salt, // domain customization string
prehash_size
)
} else if (prehash_func == "sha512") {
prehash_raw = hmac_sha512(
password,
prehash_salt // HMAC key
)
prehash_raw = truncate(prehash_raw, prehash_size)
} else {
// failure
}
// crypt() and bcrypt quirks, salt_raw is used under the hood
bcrypt_hash = crypt(
unpadded_base64(prehash_raw), // password
sprintf("$2y$%02u$%s", bcrypt_cost, bcrypt_base64(salt_raw))
)
output_hash = sprintf(
"$bcrypt-wrap$v=%u$cost=%u,prehash=%s$%s$%s",
version,
bcrypt_cost,
prehash_func,
unpadded_base64(salt_raw),
base64_bcrypt_to_standard(substring(bcrypt_hash, -31))
)
password_verify() pseudo-code:
password = <user input>
input_hash = <retrieved from storage>
prehash_size = 54 // yields 72 bytes base64 to fill bcrypt space
validate_and_parse_hash(
input_hash,
version, // out param
bcrypt_cost, // out param
prehash_func, // out param
salt_b64, // out param
hash_b64 // out param
)
salt_raw = unpadded_base64_decode(salt_b64)
prehash_salt = "bcrypt-wrap:" || version || ":" || salt_raw
if (prehash_func == "cshake256") {
prehash_raw = cshake256(
password,
prehash_salt, // domain customization string
prehash_size
)
} else if (prehash_func == "sha512") {
prehash_raw = hmac_sha512(
password,
prehash_salt // HMAC key
)
prehash_raw = truncate(prehash_raw, prehash_size)
} else {
// failure
}
hash_prefix = sprintf(
"$2y$%02u$%s",
bcrypt_cost,
base64_standard_to_bcrypt(salt_b64)
)
stringed_hash = sprintf(
"%s%s",
hash_prefix,
base64_standard_to_bcrypt(hash_b64)
)
computed_hash = crypt(unpadded_base64(prehash_raw), hash_prefix)
valid = ts_strcmp(computed_hash, stringed_hash)
Most of these are anticipated questions likely to be raised during discussion. If more come up, they'll be added at the bottom.
No. This is a well-known pattern adjusted to modern standards. Every building block is standard, applied correctly, and has a meaningful purpose. As far as it is possible to know that mistakes are avoided, every choice made about the algorithm is backed by reputable public sources and not speculative. Since we are in the AI craze era, it needs to be said: no, none of this is invented by an LLM, and there are zero “hallucinations” involved.
Additionally, the target release version is PHP 8.7, more than a year away as of this first publication. That's plenty of time for peer review, including from outside experts.
The easy answer is Cryptographic agility. A secondary answer is that (in this context) the two are equal and no unbiased comparison today would have a clear winner. In absence of reasons other than someone's preference, it would be a mistake to lock onto one path.
cSHAKE256 is significantly more elegant and its adoption is rapidly increasing in Post-Quantum Cryptography, practically ensuring that it's here to stay. The bcrypt-wrap design actually evolved from a cshake256-bcrypt prototype.
SHA-512 has a slight edge in terms of having been around for longer and users are more familiar with it. It has to be used as a HMAC and truncated, but this is a provably safe technique.
While a few vanity metrics could make a head-to-head comparison more quantitative, they are completely meaningless when wrapped by Bcrypt. The one number to care about is 256 bits of collision resistance provided by both functions. A common point of confusion might be the different numbers in the function names, which don't mean the same thing. In SHA-512, the number is the output size; in cSHAKE256, the number is the security strength.
This is a fair question, as the prehash merely patches up Bcrypt's limitations. In a pragmatic sense, it's not that important. A bigger number is not good for the sake of being bigger, and there is such a thing as too much security; otherwise we'd have cost 31 Bcrypt. However, defaulting to an acceptable minimum and opposing higher standards on a “not needed” basis is also irrational, particularly when the cost is negligible. All reasons for weaker hashes to even exist are based on computational speed, which would be a drop in the ocean of what Bcrypt does.
It is a matter of keeping up with standards, having solid grounds for trust, and (with PHP's poor reputation in mind) disarming critics.
In addition to already provided details, SHA-512 runs on historical momentum, while cSHAKE256 would be ahead if the same history was being shaped today. This choice is the cryptographic equivalent of parameterized SQL queries over escaped string interpolation.
HMACs are well-known for the part they play in security, but the way they are used in this pattern is a double clutch. First for what the “c” (customizable) in cSHAKE naturally provides against password shucking, and second to prevent length-extension attacks, which cSHAKE is immune to by design.
It might sound that way because it's not an all-familiar “SHAnumber” name, except that's what the SHA in SHAKE stands for. This is a SHA3-family function standardized by NIST; a slightly modified SHA3-512.
Wait, SHA-512 and SHA3-512? Yes, this can be confusing. SHA-512 is from the SHA2 family, which superseded SHA1. SHA3 was introduced to replace SHA2 in case it gets broken, and so that we can have two alternatives that can't be simultaneously broken. It warrants saying that either can fail first, or be proven to be categorically better, so that's not a factor. This is merely a historical summary and a note that we're comparing SHA2 and SHA3, not standard vs exotic.
The format is extensible and two options must exist for cryptographic agility, but there is no intent to support many algorithms. Aside from technical merits, SHA-512 and cSHAKE256 were selected also for their wide availability. A meaningful third option of the same strength that could (and should) be added is BLAKE2b. The one bias that has played a role here is that SHA2 and SHA3 are already part of PHP core, and investing more effort before a conceptual proposal has approval makes little sense. If it gains traction though, consider BLAKE2b a realistic third that shouldn't be discounted (certainly not on the basis that we don't already have it; it is very popular in other ecosystems).
Overall though, a goal to have in mind is ensuring the algorithm is uniformly portable, without dropping one of its options. With two or three, that is easy to demand. If there were more, that would risk degrading interoperability.
This RFC exists in large part because Bcrypt is already in the standard library, while good alternatives to it require external dependencies. New additions that don't satisfy practical needs or can't meet the same criteria should be discouraged.
In reference to OWASP Password Storage Cheat Sheet (archive link).
OWASP's considerations are not all about cryptographic strength. They have chosen the largest fixed-output hash that fits within 72 bytes after Base64 encoding, because their advice has to be idiomatic. It delivers practical security for the average developer in as many ecosystems as possible.
The recommendation doesn't apply to PHP, because it is PHP's role to make better things idiomatic. A side benefit to OWASP is also not having to argue about truncation. In the very same text where SHA-384 is endorsed, SHA-512 truncation is explicitly described as negligible. However, SHA-384 is a truncated SHA-512, with different initial values for the sole purpose of not producing a literal substring.
This question is in reference to the recent RFC proposing a port of bcrypt-sha256 from Python's PassLib (archive).
bcrypt-sha256 is functionally solid, but inferior all around. It's one of those things that gets a default assumption of maturity, but in fact has many subtle deficiencies visible to a trained eye. Some may be seen as petty outside of cryptography circles, but nonetheless:
t=2b) is redundant and only adds confusion, since any string-safe encoded prehash eliminates the differences between 2a/2b/2x/2y variations. Similarly, “only” supporting 2b as of version 1.7.3 is functionally meaningless.r=<cost> for “rounds” is a similar misunderstanding. In Bcrypt, internal “rounds” grow exponentially with the cost. A cost of 4 results in 16 rounds, cost of 12 does 4096.Something that had remained entirely unnoticed until version 1.1 of this RFC: PassLib maintenance itself had been in limbo for years, with the original author being unreachable until recently. Ownership of the package officially changed in late October 2026 (after the bcrypt-sha256 RFC and before this one). See https://github.com/pypi/support/issues/9290 and https://foss.heptapod.net/python-libs/passlib/-/work_items/187.
A lot of the above is because PassLib is simply stuck with a lot of legacy, and that's understandable, but PHP has no reason to inherit the same historical baggage and we have our own reputational problem of the same nature.
The petty nitpicks however also show a certain lack of expertise, which is a serious dent in the argument for copying an existing library on the basis of it being mature. Cryptographic implementations should be copied to avoid repeating mistakes, not to propagate them, and with the intent to benefit from good design choices that may not be obvious. PassLib's bcrypt-sha256 fails that test and is at best a “not insecure” option, mostly riding on availability bias.
bcrypt-wrap by comparison is stronger on all of the above-listed issues, easier to port by design (not for LoC, but avoiding error-prone techniques), and not being tied to a specific prehash primitive makes it future-proof as long as Bcrypt itself remains solid.
There actually was intent to build in a peppering mechanism. It just can't be done without either making it so oversimplified that it has no benefit over userspace code, or imposing significant architectural constraints on the application.
A pepper is a secret that is never stored in the database, but gets mixed into password hashes (alongside salts, not replacing them). The idea being that a database can be stolen, but not with the codebase and configuration alongside it, therefore preventing attackers from cracking password hashes because they don't have the application-layer secret.
That's all great, but a pepper comes with the same management problems as encryption keys. How do you store it? How do you rotate it? How do you not lose it? What if the security department demands it never leaves an HSM?
Without accounting for all of these questions, adding a simple “pepper” element to the password_hash() options array would just be goading developers into using something they don't necessarily understand, and may lead to nasty situations. As bad as a bricked database if the pepper is lost. User-provided salt was rightfully retired for a lot less than that.
It's a single algorithm that works the same way regardless of the chosen prehash primitive. That may not seem like a big deal upfront, but it is significantly easier to maintain with bcrypt-wrap as a single package.
PASSWORD_DEFAULT for a particular prehash to be set as preferred.PASSWORD_BCRYPT_* constants.This question came in reference to the Introduction text. There's plenty of evidence, though in hindsight it not obvious in the PHP ecosystem:
If we are stuck with Bcrypt, which is part of the premise here, it's the only reasonable thing to do.
This concern may've been raised because of some ambiguity in the first draft of this RFC, but to answer it more directly: no.
Password shucking, or hash shucking, is a weakness that allows hash crackers to directly attack an inner hash layer instead of the full algorithm.
It is typically presented as a threat to “upgraded” old hashes, as breached databases and user correlation allows for layered hashes like bcrypt(sha256(password), salt, cost) to be cracked instantly in some cases. However, prior existence of weak hashes isn't necessarily a requirement. As long as the inner hash isn't domain-separated at least per database, it can be worthwhile for attackers to try shucking with yet uncracked plain hashes from unrelated breaches, even if your database never contained plain fast hashes.
The solution to this, using standard hash functions is to apply them as HMACs, with a key that is unique to your database: bcrypt(hmac-sha512(password, key), salt, cost)
That key can be the salt itself (public, but unique per hash), pepper (global to app, but never stored in database), or even a hard-coded static string.
There are some possible points of confusion here:
bcrypt-wrap first applies a bcrypt-wrap:0: prefix to the random salt, then uses that prefixed salt as either the HMAC key with HMAC-SHA-512 or cSHAKE256's native domain customization parameter. This is a step beyond common defense against shucking, as it locks the inner hash to the outer bcrypt layer and a specific bcrypt-wrap version, making it impossible to attack independently.
Variations of this fear seem to be rooted on the too many incorrect ways in which hash layering has been done in the past.
sha1(md5(password)), written years ago by developers who naively thought that more hashing automatically means better hashing. This was unfortunately so common that “don't double-hash” became a necessary mantra.bcrypt(sha512(password), salt, cost). That correctly notes the caveat that strong hashes aren't magically better and can still be used incorrectly.ext/hash functions default to hex-encoded output, it is also easy to miss that a lot of the Bcrypt-specific warnings are related to the dangers of raw, unencoded prehashes. In cryptography and most other ecosystems, the default assumption is a raw binary array.As the dangers are subtle and generic advice easy to remember, it is not hard to be left with the takeaway that all double-hashing is bad, doubly so with Bcrypt involved. Anthony's post explicitly describes The Fix though, referencing the Base64-encoded HMAC pattern used by Mozilla.
That is the exact same pattern used by bcrypt-wrap, with one minor difference - Mozilla's team opted for a pepper (just calling it a “local salt”) as the HMAC key, while bcrypt-wrap uses a domain-separated salt in the true sense of the term. Both approaches are valid, a pepper just isn't feasible for the goals of this RFC.
Quote: [...] implementation decreases security of bcrypt [...] The input space is decreased from 2^576 to 2^432, which is a magnitude of 2^144. Sure, these are astronomical values, but it's a significant decrease nonetheless.
No. These numbers are meaningless and not just for being astronomical.
That to this day Bcrypt truncates any password at the null byte is a major flaw in this argument, because no algorithm intended to fully utilize this space can ignore the possibility of a null byte appearing at random positions. 1 in 5 of all uniformly random 576-bit inputs will contain a null byte. It is a usability nightmare specifically for anyone intent on getting close to 2^576, and has proven to be a security footgun for anyone else.
The 2^576 number is inherited from the maximum key size of Bcrypt's underlying Blowfish cipher. There are many reasons why this comes with a lot of caveats, but most importantly a cryptographic key is not the same thing as a password.
Bcrypt is explicitly a password-hashing algorithm, which means it was designed for use with printable characters, capping the “target” input space to at most 2^474. This is useful in the modern world for Unicode characters, but is in itself a happy coincidence.
At the time of Bcrypt's creation (circa 1997-1999) everybody was using 7bit ASCII (~2^237). 8bit character passwords weren't a thing for a long time, as evident by the 2x variant 8th bit bug remaining undiscovered until 2011. A claim that such a large input space is part of Bcrypt's security considerations doesn't hold up, and this is actually the least important reason why.
So why is 2^576 theoretically possible then? Primarily, it makes no sense to artificially restrict the already mentioned inheritance from Blowfish. In fact, we have the opposite issue with information density, since even the longest passwords created by humans contain a lot less entropy per byte. To get anywhere close to 2^474 would require pushing the strongest password generators to their limits, and this is a modern era ceiling, not what Bcrypt was originally designed for. The prehash step in bcrypt-wrap works around the information density problem by allowing much longer passwords.
Most notably though, 2^576 is Bcrypt's internal state size, and this is the important part - a cryptographic algorithm's internal state doesn't work like a bucket completely filled from 0 to SIZE_MAX. The algorithm's constants, multiple 32-bit boxes that the input is funneled through, binding to a 128-bit salt, and mathematical operations being performed restrict the number of unique states realistically closer to half that size!
Actual security strength is further affected by other factors, including the 184-bit output; less than a third of 576.
Does that mean the input space is entirely irrelevant? No, of course not, but as long as the prehash is sufficiently large, there is zero drop in security stemming from this factor. It is also not a relevant bottleneck though, as the low information density in user passwords is the real problem, well-worth the perceived tradeoff to allow for long passphrases.
A more space-efficient encoding was actually considered during the design, in part for elegancy and in part to diffuse arguments like this one. That's still an option if these fears persist. However, Base64 is a pragmatic choice that makes implementation less complex and portability easier, with reduced risk of mistakes.
None.
PHP 8.7.
Developers no longer have to make a tradeoff between working around Bcrypt's limitations or potentially unavailable extensions. Code dealing with this tradeoff can be phased-out, which has the second-order effect of reduced risks in security-critical code, even beyond password handling. A lot of end users get a free security upgrade without realizing it.
ext/standard will now depend on ext/hash, but this is a non-issue since RFC: Always available hash extension.
ext/hash ABI is affected, since it needs to expose low-level functionality for use by ext/standard/password.c
None.
None yet.
Potentially become the PASSWORD_DEFAULT in a later major PHP release.
Primary Vote requiring a 2/3 majority to accept the RFC:
Vote: Add PASSWORD_BCRYPT_WRAP to password_hash() as outlined in the RFC? — Yes: 0 · No: 0 · Abstain: 0 (closed)
https://github.com/php/php-src/pull/25040
After the RFC is implemented, this section should contain:
None.