1

Here I attach my code regards "SHA-1" algorithm based Java code for same input value given manually:

import java.security.MessageDigest;    
import java.security.NoSuchAlgorithmException;    
import sun.misc.BASE64Encoder;

public class NewClass {       
 public static void main(String args[]) throws NoSuchAlgorithmException        
 {       
    MessageDigest digest = MessageDigest.getInstance("SHA-1");    
    System.out.println("Algorithm :"+digest.getAlgorithm());    
    digest.update("welcome".getBytes());    
    byte[] result = digest.digest();     
    hash = (new BASE64Encoder()).encode(result);     
    MessageDigest digest1 = MessageDigest.getInstance("SHA-1");    
    digest1.update("welcome".getBytes());    
    byte[] result1 = digest1.digest();    
    System.out.println(result);    
    System.out.println(result1);    
    String hash1 = (new BASE64Encoder()).encode(result);    
    System.out.println("Digest value"+hash);    
    System.out.println("Digest value"+hash1);       
 }

}

output:

Algorithm :SHA-1

[B@42e816

[B@9304b1

Digest value wLE3/i15JFnyb/djzORFdKW1qwM=

Digest value wLE3/i15JFnyb/djzORFdKW1qwM=

The above algorithm produces the same hash code, but the intermediate values are not same. Why is that?

1
  • I don't answer your question, but I think you have miss take String hash1 = (new BASE64Encoder()).encode(result);, it should be result1. Commented Jun 2, 2012 at 12:40

2 Answers 2

9

[B@42e816 is what you get when you try to print an array in Java. It just prints its memory address, not the contents. Obviously, two different arrays will have different addresses.

Sign up to request clarification or add additional context in comments.

Comments

5

These two lines make no sense:

System.out.println(result);
System.out.println(result1);

The don't print the contents of the two byte arrays as you obviously expect. Instead, the print the type ([B stands for byte array) and a number which can be thought of as the memory address of the array.

To print the contents of the byte array, use the Base 64 encoding and print the resulting string. I'm confident they will be the same.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.