Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hashing password using salt

I am producing a hashed value of a password with salt.My code:

  `String psw="hello";  
   String tobehashed="";
   tobehashed=salt+psw;
   MessageDigest md = MessageDigest.getInstance("SHA-256");
   byte[] digest = md.digest(tobehashed.getBytes());
   System.out.println("Digest:"+digest);` 

I have produced salt by providing seed as current time in milliseconds,I have no issues with salt(I m getting random values) but irrespective of salt I am getting same hash value..

Actually the purpose of salt is to get different hash values.. This is my output; Random nubr:-2098016229(this keeps changing) Digest:[B@ca0b6(this remains same) Waht is the solution??

like image 646
user10101 Avatar asked Mar 19 '26 15:03

user10101


1 Answers

You are printing out the result of digest.toString(), which in the case of a byte array does not convert the bytes to meaningful output. It just prints [B (which is the code for byte array), @, and then a hex address. You will see this output often as you get more experience with Java.

You will need to use a loop to iterate through the bytes in the digest and print them individually.

like image 174
Greg Hewgill Avatar answered Mar 21 '26 05:03

Greg Hewgill



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!