How to hash a string using SHA-256

hash, java, sha256, string

Solution

this will pring hex representation of hash

String s = DatatypeConverter.printHexBinary(hash)

you cannot get original string from hash

Problem

I am trying to hash my users password which is of string type using SHA-256 I am using SHA-256 to hash the string using the following method ``` String text = "abc"; MessageDigest digest = MessageDigest.getInstance("SHA-256"); byte[] hash = digest.digest(text.getBytes("UTF-8")); ``` To convert the btye array to string , I used the following method ``` String doc2 = new String(hash, "UTF-8"); ``` When i print doc2 to output , i get rubbish ``` �x����AA@�]�"#�a��z���a� ``` What am i doing wrong ??? How do i hash a string using SHA-256 and convert it back to string ??

Original source

Related problems