java - SHA hash does not seem to be working correctly -


i trying build simple password authenticator passwords have been hashed using sha-256 .

i found couple calculators online (http://onlinemd5.com/) hashed "password" "5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8"

i tried couple other passwords expected results.

so tried implement straight forward set of code (or thought)

string pswd="password"; string storedpswd="5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8"; //first byte[]  arrays.equals(hashword(pswd),storedpswd.getbytes("utf-8") ); ... private byte[] hashword(string word)     {     try {         return messagedigest.getinstance("sha-256").digest(word.getbytes("utf-8"));     } catch (exception e)        {        throw new badcredentialsexception("could not hash supplied password", e);     } } 

i tried without success.

return storedpswd.touppercase().equals(digestutils.sha256hex(password));

the apache codec library (v1.10) , java 1.6 gives me :

113459eb7bb31bddee85ade5230d6ad5d8b2fb52879e00a84ff6ae1067a210d3 

instead of

5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8 

what missing ??

the solution (wrong inputs):

updated test code:

string passwordsha="5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8"; string complexsha="8849fb9b221ddec0117e2796d16929179accf3a6012f738e1ed6c11af9cc2081"; @test public void testdigest() throws interruptedexception{     system.out.println("starting digest test");     string complexpassword = "a7$h1uc8";     try {         assert.asserttrue(authenticateuser(complexpassword, complexsha));         assert.asserttrue(authenticateuser("password", passwordsha));                    assert.asserttrue( hashword(complexpassword).equals(complexsha) );     } catch (exception e) {         assert.fail();     } } public boolean authenticateuser(string word, string stored) throws exception {     string apache2pswd = hashapache(word);     system.out.println(apache2pswd);                     return stored.equals(apache2pswd); } private string hashapache(string pswd){     return digestutils.sha256hex(pswd);      } public static string hashword(string word) throws exception{     byte[] digest = messagedigest.getinstance("sha-256").digest(word.getbytes("utf-8"));     stringbuilder sb = new stringbuilder();     (byte b : digest) {         sb.append(string.format("%02x", b));     }     system.out.println(sb.tostring());     return sb.tostring(); } 

with results:

starting digest test 8849fb9b221ddec0117e2796d16929179accf3a6012f738e1ed6c11af9cc2081 5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8 8849fb9b221ddec0117e2796d16929179accf3a6012f738e1ed6c11af9cc2081 

the hashword method posted not correct, not compile (is actual code?); it's not returning value.

with this:

byte[] digest = messagedigest.getinstance("sha-256").digest("password".getbytes("utf-8"));  (byte b : digest) {     system.out.printf("%02x", b); } 

i expected output:

5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8

the output 113459eb7bb31bddee85ade5230d6ad5d8b2fb52879e00a84ff6ae1067a210d3 when calculate sha-256 hash on string 5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8 instead of original string password.

you calculating hash on hex string containing hash, instead of hash of original password.


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -