Skip to content

Add an example where the user string is a hash calculated from user submitted data #4771

@Rudloff

Description

@Rudloff

From manual page: https://php.net/function.hash-equals


I feel like the example can be slightly confusing because the user provides both the hash to check and values used to calculate the known correct hash. And I'm not sure having a user-provided hash is that common, in my experience the user string is often a hash calculated from user-provided values.

I think it could be useful to add another about comparing a hash built from a user-provided password with a stored known hash of the correct password (which is a common scenario).

Maybe something like this:

<?php
$knowPasswordHash = '$2y$10$4A9NKVnmZyGcdPKCgPE4o.2k0jHalnyyNEatk5hOIKFOUxB.ImI8y';
$sentPassword = $_POST['password]';

if (hash_equals($knowPasswordHash, password_hash($sentPassword)) {
    echo "This is the correct password.", PHP_EOL;
} else {
    echo "Wrong password.", PHP_EOL;
}
?>

(Of course you should use password_verify() for this specific use-case.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions