如何从Java中的字符串生成SHA256和SHA512哈希值

SHA256可以使用jdk中的 MessageDigest 类生成。

句法:

  MessageDigest digest = MessageDigest.getInstance("SHA-256");
  digest.reset();
  digest.update(input.getBytes("utf8"));
  toReturn = String.format("%064x", new BigInteger(1, digest.digest()));

SHA-512也可以在没有额外的java库的情况下生成:

句法:

  MessageDigest digest = MessageDigest.getInstance("SHA-512");
  digest.reset();
  digest.update(input.getBytes("utf8"));
  toReturn = String.format("%0128x", new BigInteger(1, digest.digest()));

以下代码使用“this is an example”作为输入和SHA-2哈希函数来创建SHA256和SHA512。 前两种方法是为每个哈希函数创建的。 第一个用于SHA-256,并使用 MessageDigest 类生成散列。 第二种方法是以相同的方式创建的,但使用SHA-512。 验证输出结果,我们也使用Apache commons的 DigestUtils 类生成哈希。 所有生成的散列都写在控制台中。

1-创建以下java文件:

import java.math.BigInteger;
import java.security.MessageDigest;

public class SHA2 {

    public static String getSHA256(String input){

	String toReturn = null;
	try {
	    MessageDigest digest = MessageDigest.getInstance("SHA-256");
	    digest.reset();
	    digest.update(input.getBytes("utf8"));
	    toReturn = String.format("%064x", new BigInteger(1, digest.digest()));
	} catch (Exception e) {
	    e.printStackTrace();
	}
	
	return toReturn;
    }

    public static String getSHA512(String input){

	String toReturn = null;
	try {
	    MessageDigest digest = MessageDigest.getInstance("SHA-512");
	    digest.reset();
	    digest.update(input.getBytes("utf8"));
	    toReturn = String.format("%0128x", new BigInteger(1, digest.digest()));
	} catch (Exception e) {
	    e.printStackTrace();
	}
	
	return toReturn;
    }
    
    public static void main(String[] argv) {

	String inputValue = "this is an example";

	// With the java libraries
	String sha256 = getSHA256(inputValue);

	System.out.println("The SHA-256 of \"" + inputValue + "\" is:");
	System.out.println(sha256);
	System.out.println();

	// With Apache commons
	sha256 = org.apache.commons.codec.digest.DigestUtils.sha256Hex(inputValue);

	System.out.println("The SHA-256 of \"" + inputValue + "\" is:");
	System.out.println(sha256);
	System.out.println();

	// With the java libraries
	String sha512 = getSHA512( inputValue );

	System.out.println("The SHA-512 of \"" + inputValue + "\" is:");
	System.out.println(sha512);
	System.out.println();

	// With Apache commons
	sha512 = org.apache.commons.codec.digest.DigestUtils.sha512Hex(inputValue);

	System.out.println("The SHA-512 of \"" + inputValue + "\" is:");
	System.out.println(sha512);

    }
}

输出将是:

The SHA-256 of "this is an example13" is:
066f61646a92c8cf04943577b3c4d9ee6e5d125fe850b4c8c4d315ddd3aa1f50

The SHA-256 of "this is an example13" is:
066f61646a92c8cf04943577b3c4d9ee6e5d125fe850b4c8c4d315ddd3aa1f50

The SHA-512 of "this is an example4" is:
05c1649335528b45ab4965619db876e5090e42dfc526b486dde9368df2b77830c4e523cee1fd0054d9009729a6a11e697a1dd121e36be3ada593b8d76cb80ced

The SHA-512 of "this is an example4" is:
05c1649335528b45ab4965619db876e5090e42dfc526b486dde9368df2b77830c4e523cee1fd0054d9009729a6a11e697a1dd121e36be3ada593b8d76cb80ced

参考文献:

Java 8
SHA-2 cryptographic hash functions

最近评论