Bcrypt in java and php does not match

Asked

Viewed 260 times

2

I have an application in Php that makes a record of a person and when the person puts the password, the password is stored in the database in hash form, with the following code

password_hash($password, PASSWORD_BCRYPT)

then I have an application in Java and wanted to check a password with the hash that is in the database, I put the following code

BCrypt.hashpw(passTxt.getText(),BCrypt.gensalt())

the problem is that they do not match the hashes. I’ve been told that it may be from SALT that has to be the same, but how do I put the same SALT?

  • Here at that link has an example of using, but already check that SALT is obsolete in version 7

  • yes, but how do I do in Java the hash check is the same as in php?

1 answer

0

in PHP

<?php
 /**
 * Note that the salt here is randomly generated.
 * Never use a static salt or one that is not randomly generated.
 *
 * For the VAST majority of use-cases, let password_hash generate the salt randomly for you
 */
    $options = [
        'cost' => 11,
        'salt' => mcrypt_create_iv(22, MCRYPT_DEV_URANDOM),
    ];
echo password_hash("rasmuslerdorf", PASSWORD_BCRYPT, $options)."\n";
?>

In Java jBCrypt

// Hash a password for the first time
String hashed = BCrypt.hashpw(password, BCrypt.gensalt());

// gensalt's log_rounds parameter determines the complexity
// the work factor is 2**log_rounds, and the default is 10
String hashed = BCrypt.hashpw(password, BCrypt.gensalt(12));

// Check that an unencrypted password matches one that has
// previously been hashed
if (BCrypt.checkpw(candidate, hashed))
    System.out.println("It matches");
else
    System.out.println("It does not match");

can be the salt different so I’m putting how to change the salt of the 2.

source english stack

Normal bcrypt will not result from what you search natively but jBcrypt can bring you look at the stack in English brings you the rest of the answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.