2

I am new to java security so it may sound stupid to you guys. I am using triple-des algorithm for encryption decryption in that i am using hash value as keys. I am using sha-512 for hashing i have heard that two same strings hash will be same but i am getting different output for same string. I am affixing the code of sha-512. Let me know what the problem is if possible.

public class SHA256Algo {

    public static String createHash(String text) throws UnsupportedEncodingException, NoSuchAlgorithmException 
    {
        String encryptedText = "" ;
        MessageDigest md = MessageDigest.getInstance("SHA-512");

        md.update(text.getBytes("UTF-16")); // Change this to "UTF-16" if needed
        byte[] digest = md.digest();
        String str = digest.toString() ;
        return str ;
    }

    public static void main(String[] args) {
        try {
            System.out.println(createHash("tarun")) ;
            System.out.println(createHash("tarun")) ;
        } catch (UnsupportedEncodingException | NoSuchAlgorithmException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

output :
[B@32d2bb53
[B@29086036

tarun verma
  • 221
  • 3
  • 5
  • 14
  • 1
    toString() on array does not have the effect you expect. –  Sep 03 '14 at 16:53
  • Can you use third-party libraries? This would be much simpler if you could use Guava's `Hashing.sha512().hashString(text, Charsets.UTF_16).toString()`, which would accomplish exactly what you want. – Louis Wasserman Sep 03 '14 at 17:28

1 Answers1

4

Arrays are objects too in Java, but they don't override Object's toString() method. The output is not the hashed output, but the result of Object's toString() method.

[T]his method returns a string equal to the value of:

getClass().getName() + '@' + Integer.toHexString(hashCode())

The getClass().getName() is responsible for [B, then there is the @ character, and the rest of the characters are the hexadecimal hash code, also from Object.

You need to convert the byte array to a String in another way besides calling toString(). This may involve conversion of the byte array to Base 64 or to hexadecimal characters.

rgettman
  • 176,041
  • 30
  • 275
  • 357
  • thanks for your time can you give me a sample code to that particular conversion. – tarun verma Sep 03 '14 at 16:59
  • @tarunverma take a look at the Byte and String API for a method something like *parse* or *value*. Look at their javadoc. All of this should be easy if you have [eclipse](http://www.eclipse.org/downloads/index-developer.php) set up [nicely](http://superuser.com/questions/167458/how-to-add-javadocs-to-eclipse). – AncientSwordRage Sep 03 '14 at 17:07
  • thanks all i found the way out to convert it to string i am sharing the code hope it helps others... `byte[] digest = md.digest(); StringBuffer hexString = new StringBuffer(); for (int i=0;i – tarun verma Sep 03 '14 at 17:15
  • That probably doesn't quite work, in that it'll trim leading zeroes? `String.format("%02x", digest[i] & 0xFF)` would be more correct. – Louis Wasserman Sep 03 '14 at 17:28