package com.chenwc; /** * MessageDigest 支持的算法枚举 * * @author 陈为成 * */ public enum AlgorithmEnum { MD2("MD2"), MD5("MD5"), SHA_1("SHA-1"), SHA_224("SHA-224"), SHA_256("SHA-256"), SHA_384("SHA-384"), SHA_512("SHA-512"); //JDK 9 及之后版本支持 //SHA_512_224("SHA-512/224"), //SHA_512_256("SHA-512/256"), //SHA3_224("SHA3-224"), //SHA3_256("SHA3-256"), //SHA3_384("SHA3-384"), //SHA3_512("SHA3-512"); private String algorithm; AlgorithmEnum(String algorithm) { this.algorithm = algorithm; } public String getAlgorithm() { return algorithm; } public void setAlgorithm(String algorithm) { this.algorithm = algorithm; } /** * 根据算法名称获取 AlgorithmEnum * @param algorithm 算法名称 * @return AlgorithmEnum */ public AlgorithmEnum getAlgorithmEnum(String algo) { for (AlgorithmEnum algorithmEnum : AlgorithmEnum.values()) { if (algo.equals(algorithmEnum.getAlgorithm())) { return algorithmEnum; } } return null; } }
package com.chenwc; import java.nio.charset.StandardCharsets; import java.security.MessageDigest; public class ShaUtilsByJavaApi { /** * SHA哈希字符串 * @param original 待加密字符串 * @param algorithmEnum MessageDigest 支持的算法枚举 * @return SHA哈希加密后字符串 */ public static String getShaHash(String original, AlgorithmEnum algorithmEnum) { try { MessageDigest messageDigest = MessageDigest.getInstance(algorithmEnum.getAlgorithm()); byte[] hash = messageDigest.digest(original.getBytes(StandardCharsets.UTF_8)); return bytesToHex(hash); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } return null; } /** * 自定义字节到十六进制转换器来获取十六进制的哈希值 * @param hash 二进制哈希值 * @return 十六进制字符串哈希值 */ private static String bytesToHex(byte[] hash) { StringBuilder hexString = new StringBuilder(); if (null != hash) { for (byte md5Byte : hash) { hexString.append(Integer.toHexString(md5Byte | 0xFFFFFF00).substring(6)); } } return hexString.toString(); } }
package com.chenwc; import java.nio.charset.StandardCharsets; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import org.apache.commons.codec.binary.Hex; import org.apache.commons.codec.digest.DigestUtils; public class ShaUtilsByApacheCommons { /** * SHA哈希字符串 * * @param original 待加密字符串 * @param algorithmEnum MessageDigest 支持的算法枚举 * @return SHA哈希加密后字符串 */ public static String getShaHash(String original, AlgorithmEnum algorithmEnum) { byte data[] = null; try { data = DigestUtils.digest(MessageDigest.getInstance(algorithmEnum.getAlgorithm()), original.getBytes(StandardCharsets.UTF_8)); } catch (NoSuchAlgorithmException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } return Hex.encodeHexString(data); } /** * SHA-256哈希字符串 * * @param original 待加密字符串 * @return SHA哈希加密后字符串 */ public static String getSha256(String original) { return DigestUtils.sha256Hex(original); } /** * SHA-1哈希字符串 * * @param original 待加密字符串 * @return SHA哈希加密后字符串 */ public static String getSha1(String original) { return DigestUtils.sha1Hex(original); } /** * SHA-384哈希字符串 * * @param original 待加密字符串 * @return SHA哈希加密后字符串 */ public static String getSha384(String original) { return DigestUtils.sha384Hex(original); } /** * SHA-512哈希字符串 * * @param original 待加密字符串 * @return SHA哈希加密后字符串 */ public static String getSha512(String original) { return DigestUtils.sha512Hex(original); } /** * MD2哈希字符串 * * @param original 待加密字符串 * @return SHA哈希加密后字符串 */ public static String getMd2(String original) { return DigestUtils.md2Hex(original); } /** * MD5哈希字符串 * * @param original 待加密字符串 * @return SHA哈希加密后字符串 */ public static String getMd5(String original) { return DigestUtils.md5Hex(original); } }
package com.chenwc; import java.nio.charset.StandardCharsets; import com.google.common.hash.Hashing; public class ShaUtilsByGuava { /** * SHA哈希字符串 * * @param original 待加密字符串 * @param algorithmEnum MessageDigest 支持的算法枚举 * @return SHA哈希加密后字符串 */ @SuppressWarnings("deprecation") public static String getShaHash(String original, AlgorithmEnum algorithmEnum) { String hash = ""; switch(algorithmEnum) { case MD5: hash = Hashing.md5().hashString(original, StandardCharsets.UTF_8).toString(); break; case SHA_1: hash = Hashing.sha1().hashString(original, StandardCharsets.UTF_8).toString(); break; case SHA_256: hash = Hashing.sha256().hashString(original, StandardCharsets.UTF_8).toString(); break; case SHA_384: hash = Hashing.sha384().hashString(original, StandardCharsets.UTF_8).toString(); break; case SHA_512: hash = Hashing.sha512().hashString(original, StandardCharsets.UTF_8).toString(); break; default: hash = null; break; } return hash; } }
同 Java API 类似,只是最后一步将字节转换为十六进制使用了 org.bouncycastle.util.encoders.Hex 实现
package com.chenwc; import java.nio.charset.StandardCharsets; import java.security.MessageDigest; import org.bouncycastle.util.encoders.Hex; public class ShaUtilsByBouncyCastle { /** * SHA哈希字符串 * @param original 待加密字符串 * @param algorithmEnum MessageDigest 支持的算法枚举 * @return SHA哈希加密后字符串 */ public static String getShaHash(String original, AlgorithmEnum algorithmEnum) { try { MessageDigest digest = MessageDigest.getInstance(algorithmEnum.getAlgorithm()); byte[] hash = digest.digest(original.getBytes(StandardCharsets.UTF_8)); return new String(Hex.encode(hash)); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } return null; } }
package com.chenwc; import java.util.UUID; public class Main { public static void main(String[] args) { // TODO 自动生成的方法存根 String uuid = UUID.randomUUID().toString().replace("-", "").toUpperCase(); System.out.println("UUID: " + uuid); System.out.println("-------------------------Java API--------------------"); for (AlgorithmEnum algorithmEnum : AlgorithmEnum.values()) { System.out.println(algorithmEnum.getAlgorithm() + ": \t" + ShaUtilsByJavaApi.getShaHash(uuid, algorithmEnum)); } System.out.println(); System.out.println("-------------------------Apache Commons--------------------"); for (AlgorithmEnum algorithmEnum : AlgorithmEnum.values()) { System.out.println(algorithmEnum.getAlgorithm() + ": \t" + ShaUtilsByApacheCommons.getShaHash(uuid, algorithmEnum)); } System.out.println(); System.out.println("-------------------------Google Guava--------------------"); for (AlgorithmEnum algorithmEnum : AlgorithmEnum.values()) { System.out.println(algorithmEnum.getAlgorithm() + ": \t" + ShaUtilsByGuava.getShaHash(uuid, algorithmEnum)); } System.out.println(); System.out.println("-------------------------Bouncy Castle--------------------"); for (AlgorithmEnum algorithmEnum : AlgorithmEnum.values()) { System.out.println(algorithmEnum.getAlgorithm() + ": \t" + ShaUtilsByBouncyCastle.getShaHash(uuid, algorithmEnum)); } } }