【发布时间】:2023-03-18 02:59:02
【问题描述】:
我在我的 Java 程序中使用 Sha-256 散列,它按照行为工作。
我实际上对我用于 SHA-256 的函数有点困惑。
以下是函数的代码:
// Function for generating to Hash of the file content..
public static String generateHash( String fileContent )
{
String hashtext = EMPTY_STRING;
try {
// SHA - 256 Message Digest..
MessageDigest shaDigest = MessageDigest.getInstance( "SHA-256" );
// digest() method is called
// to calculate message digest of the input string
// returned as array of byte
byte[] messageDigest = shaDigest.digest( fileContent.getBytes() );
// Convert byte array into signum representation
BigInteger no = new BigInteger( 1, messageDigest );
// Convert message digest into hex value
hashtext = no.toString( 16 );
// Add preceding 0s to make it 32 bit
while ( hashtext.length() < 32 ) {
hashtext = "0" + hashtext;
}
}
catch ( Exception hashingException ) {
System.out.println( "Exception in Hashing of Content = " + hashingException );
}
// return the HashText
return hashtext;
}
现在,我对三个陈述感到困惑;因为我不知道它们的实际目的是什么,因为我在互联网上浏览过它们但没有得到任何解释性的东西。谁能给我详细说明这三个步骤?
声明 1
BigInteger no = new BigInteger( 1, messageDigest );
声明 2
hashtext = no.toString( 16 );
声明 3
while ( hashtext.length() < 32 ) {
hashtext = "0" + hashtext;
}
【问题讨论】: