在这里,我附上了关于手动给出相同输入值的基于“SHA-1”算法的 Java 代码的代码:
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);
}
}
输出:
Algorithm :SHA-1
[B@42e816
[B@9304b1
Digest value wLE3/i15JFnyb/djzORFdKW1qwM=
Digest value wLE3/i15JFnyb/djzORFdKW1qwM=
上述算法产生相同的哈希码,但中间值不同。这是为什么?