How is SHA512-CRYPT for Dovecot in JAVA?

I have a Dovecot server with a MySQL database for storing usernames and passwords. Passwords in the database are in the SHA512-CRYPT schema.

I embed hashed passwords in the database using a script.

doveadm pw -s SHA512-CRYPT -p password -r 500000

I want hash passwords using a JAVA application. I found this question and I tried to create the same received hash using the same password firstpasswordand salt FooBarBaz. For some reason, the resulting hash that I receive is different, although I use the same hash algorithm, salt and password.

Here is my Java code:

byte[] password = "firstpassword".getBytes();
byte[] salt = "FooBarBaz".getBytes();

MessageDigest digest = MessageDigest.getInstance("SHA-512");
digest.reset();
digest.update(salt);
byte[] hashed = digest.digest(password);

String encodedHash = Base64.getEncoder().encodeToString(hashed);

System.out.printf("{SHA512-CRYPT}$6$%s$%s", "FooBarBaz",encodedHash);

This outputs a hash:

{SHA512-CRYPT}$6$FooBarBaz$5WPtOnXVI/a6f003WByGKIcsfa6x0ansxiyE8uEfJ0TE5pI+Rv9kcMLgdZboKg7ZSWQgWFg+pIqruvdg6aiP/g==

I also tried replacing the salt order + password in order to do this:

digest.update(password);
byte[] hashed = digest.digest(salt);

this gives me:

{SHA512-CRYPT}$6$FooBarBaz$QWS8+W5EWhModF+uO2tcsd55tDxzdzGJ5FurIbEgwVCwKfT5UqwIvBNG1Oyws8bZEFdeGgyD0u6zS1KArvGf9Q==

Does anyone know how I can execute the same hash results in Java if I use the same password and salt?

, , :

{SHA512-CRYPT}$6$FooBarBaz$.T.G.7FRJqZ6N2FF7b3BEkr5j37CWhwgvPOOoccrr0bvkBbNMmLCxzqQqKJbNhnhC.583dTBLEuZcDuQe7NEe.
+4
1

doveadm Unix crypt Base64 . , ( crypt), [a-zA-Z0-9./] ( ). , java.util.Base64, [A-Za-z0-9+/] ( RFC 4648, JavaDoc Base64). , , -.

crypt Apache Commons Codec Crypt.crypt("firstpassword", "$6$FooBarBaz") ( $6$ crypt, SHA512-CRYPT). .

+5

All Articles