了解SHA-256哈希



我在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;
}
BigInteger no = new BigInteger( 1, messageDigest ); 

将字节转换为正号幅值表示。阅读Javadoc了解更多信息。

hashtext = no.toString( 16 );

BigInteger数字转换为16进制(十六进制-十进制(字符串

while ( hashtext.length() < 32 ) { 
hashtext = "0" + hashtext;
}

准备0,直到hashtext的大小为32。

最新更新