how to SHA512-CRYPT for Dovecot in JAVA? -


i have dovecot server mysql database storing usernames , passwords. passwords in database in sha512-crypt scheme.

i inserting hashed passwords in database using script.

doveadm pw -s sha512-crypt -p password -r 500000 

i want hash passwords using java application. found this questions , tried create same resulting hash using same password firstpassword , salt foobarbaz. reason resulting hash different, although using same hashing algorithm, salt , password.

here 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 hash:

{sha512-crypt}$6$foobarbaz$5wptonxvi/a6f003wbygkicsfa6x0ansxiye8uefj0te5pi+rv9kcmlgdzbokg7zswqgwfg+piqruvdg6aip/g== 

i tried swapping order of salt + password make it:

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

this gives me:

{sha512-crypt}$6$foobarbaz$qws8+w5ewhmodf+uo2tcsd55tdxzdzgj5furibegwvcwkft5uqwivbng1oyws8bzefdeggyd0u6zs1karvgf9q== 

does have idea how can accomplish same hash results in java if use same password , salt?

the hash looking is:

{sha512-crypt}$6$foobarbaz$.t.g.7frjqz6n2ff7b3bekr5j37cwhwgvpoooccrr0bvkbbnmmlcxzqqqkjbnhnhc.583dtbleuzcduqe7nee. 

doveadm uses unix crypt family of functions generate hash , outputs hash base64 encoded string. alphabet used encoding (by crypt) [a-za-z0-9./] (as mentioned on man page functions). however, alphabet used java.util.base64 class [a-za-z0-9+/] (compliant rfc 4648, mentioned on javadoc page base64 class). therefore, if hashed values same, encoded differently.

a reliable option use crypt class apache commons codec crypt.crypt("firstpassword", "$6$foobarbaz") (the prefix $6$ mandatory instruct crypt sha512-crypt algorithm needs used). generate expected hash value.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -