Password hashing using salt

I am creating a hashed password value 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 gave salt by providing the seed as the current time in milliseconds, I have no problem with the salt (I get random values), but regardless of the salt I get the same hash value.

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

Waht is the solution?

+3


source to share


3 answers


You are printing a result digest.toString()

which, in the case of a byte array, does not convert the bytes to a meaningful result. It just prints [B

(which is the code for the byte array) @

and then the hex address. You will see this result often as you get more Java experience.



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

+4


source


B @ ca0b6 is not the contents of a byte array, it is a toString () representation. Iterate the bytes of the array and print them separately.



+4


source


If you print any reference variable, then the function System.out.println()

executes a function toString()

that comes from the object class. the output toString()

shows through printf()

. Returns the HaxCode of your ref object by
default toString()

.
 If you want to change it, you need to override it toString()

in the appropriateclass.

digest.toString();

      

just prints the hex address. So you have to use Iterate for the bytes of the array and print them individually ...

0


source







All Articles