验证hash算法网址:
https://www.fileformat.info/tool/hash.htm?
POM文件
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>RELEASE</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.8.2</version> </dependency> <!-- https://mvnrepository.com/artifact/org.bouncycastle/bcprov-jdk16 --> <dependency> <groupId>org.bouncycastle</groupId> <artifactId>bcprov-jdk16</artifactId> <version>1.46</version> </dependency> </dependencies>
HashUtil.java
package com.kpwong.util; import org.bouncycastle.crypto.digests.RIPEMD160Digest; import org.bouncycastle.jce.provider.JCEMac; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public class HashUtil { //MD5 消息摘要 public static String md5(byte[] data) throws NoSuchAlgorithmException { MessageDigest md5 = MessageDigest.getInstance("MD5"); byte[] digest = md5.digest(data); return bytes2Hex(digest); } /** * 将字节数组转换成16进制表达式 * 参数:字节数组 */ public static String bytes2Hex(byte[] bytes) { StringBuffer sb = new StringBuffer(); for (byte byt : bytes) { sb.append(Integer.toString((byt & 0xff) + 0x100, 16).substring(1)); } return sb.toString(); } /** * SHA-1消息摘要方法 */ public static String sha1Bytes(byte[] data) throws NoSuchAlgorithmException { MessageDigest sha = MessageDigest.getInstance("SHA"); byte[] digest = sha.digest(data); return bytes2Hex(digest); } public static String sha256Bytes(byte[] data) throws NoSuchAlgorithmException { MessageDigest sha = MessageDigest.getInstance("SHA-256"); byte[] digest = sha.digest(data); return bytes2Hex(digest); } public static String sha512Bytes(byte[] data) throws NoSuchAlgorithmException { MessageDigest sha = MessageDigest.getInstance("SHA-512"); byte[] digest = sha.digest(data); return bytes2Hex(digest); } public static String ripemd160Bytes(byte[] data) throws NoSuchAlgorithmException { RIPEMD160Digest ripemd160Digest = new RIPEMD160Digest(); ripemd160Digest.update(data, 0, data.length); byte[] digest = new byte[ripemd160Digest.getDigestSize()]; ripemd160Digest.doFinal(digest, 0); return bytes2Hex(digest); } // 将16进制数字的字符串,按照每两位一个字节转换成数组 // 123456 ->byte[] = {0x12,0x34,0x56} // @Test public static byte[] hexStr2HexBytes(String strHex) { if (null == strHex || 0 == strHex.length()) { return null; } strHex = (strHex.length() == 1) ? "0" + strHex : strHex; byte[] arr = new byte[strHex.length() / 2]; byte[] tmp = strHex.getBytes(); for (int i = 0; i < (tmp.length / 2); i++) { arr[i] = uniteBytes(tmp[i * 2], tmp[i * 2 + 1]); } return arr; } public static byte uniteBytes(byte src0, byte src1) { byte _b0 = Byte.decode("0x" + new String(new byte[]{src0})).byteValue(); _b0 = (byte) (_b0 << 4); byte _b1 = Byte.decode("0x" + new String(new byte[]{src1})).byteValue(); byte ret = (byte) (_b0 ^ _b1); return ret; } }
TestCode:
package com.kpwong.test; import com.kpwong.util.HashUtil; import org.omg.Messaging.SYNC_WITH_TRANSPORT; public class TestHash { public static void main(String[] args) { String src = "123456"; try{ String md5 = HashUtil.md5(src.getBytes()); String sha1 = HashUtil.sha1Bytes(src.getBytes()); String sha256 = HashUtil.sha256Bytes(src.getBytes()); String sha512 = HashUtil.sha512Bytes(src.getBytes()); String ripe = HashUtil.ripemd160Bytes(src.getBytes()); System.out.println(md5); System.out.println(sha1); System.out.println(sha256); System.out.println(sha512); System.out.println(ripe); } catch (Exception e) { e.printStackTrace(); } System.out.println("----------------------------------------"); byte[] src2 = HashUtil.hexStr2HexBytes(src); try{ String md5 = HashUtil.md5(src2); String sha1 = HashUtil.sha1Bytes(src2); String sha256 = HashUtil.sha256Bytes(src2); String sha512 = HashUtil.sha512Bytes(src2); String ripe = HashUtil.ripemd160Bytes(src2); System.out.println(md5); System.out.println(sha1); System.out.println(sha256); System.out.println(sha512); System.out.println(ripe); } catch (Exception e) { e.printStackTrace(); } System.out.println("----------------------------------------"); } }