赞
踩
MD5 (Message-Digest Algorithm 5)
SHA-1 (Secure Hash Algorithm 1)
SHA-2 (Secure Hash Algorithm 2)
SHA-3 (Keccak)
RIPEMD-160 (RACE Integrity Primitives Evaluation Message Digest)
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.70</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcpkix-jdk15on</artifactId>
<version>1.70</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>33.2.1-jre</version>
</dependency>
package cn.zhangsan.tools.enums; /** * @ClassName Algorithm * @Description TODO * @Author ZhangSan_Plus * @Date 2024/7/29 20:45 * @Version 1.0 **/ public enum Algorithm { MD5, SHA1, SHA256, SHA512, RIPEMD160, SHA224, SHA384, SHA3; } package cn.zhangsan.tools.utils; import cn.zhangsan.tools.enums.Algorithm; import com.google.common.collect.Maps; import org.bouncycastle.jce.provider.BouncyCastleProvider; import org.bouncycastle.util.encoders.Hex; import java.net.MalformedURLException; import java.net.URL; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.security.NoSuchProviderException; import java.security.Security; import java.util.Base64; import java.util.Map; /** * @ClassName HashGeneratorUtils * @Description TODO * @Author ZhangSan_Plus * @Date 2024/7/29 20:04 * @Version 1.0 **/ public class HashGeneratorUtils { static Map<Object, String> map = Maps.newLinkedHashMap(); static { Security.addProvider(new BouncyCastleProvider()); } public static void main(String[] args) { String input = "张三"; try { Map<Object, String> hashText = getHashText(input, 4); hashText.forEach((k, v) -> System.out.println(k + ":" + v)); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } } public static String extractDomain(String urlString) { try { URL url = new URL(urlString); return url.getHost(); } catch (MalformedURLException e) { e.printStackTrace(); return null; } } private static Map<Object, String> getHashText(String text, int digest) throws NoSuchAlgorithmException { map.put(Algorithm.MD5, encryption(md5(text), digest)); map.put(Algorithm.SHA1, encryption(sha1(text), digest)); map.put(Algorithm.SHA256, encryption(sha224(text), digest)); map.put(Algorithm.SHA224, encryption(sha256(text), digest)); map.put(Algorithm.SHA512, encryption(sha384(text), digest)); map.put(Algorithm.SHA384, encryption(sha512(text), digest)); map.put(Algorithm.SHA3, encryption(sha3_256(text), digest)); map.put(Algorithm.RIPEMD160, encryption(ripemd160(text), digest)); return map; } private static byte[] md5(String input) throws NoSuchAlgorithmException { MessageDigest digest = MessageDigest.getInstance("MD5"); return digest.digest(input.getBytes()); } private static byte[] sha1(String input) throws NoSuchAlgorithmException { MessageDigest digest = MessageDigest.getInstance("SHA-1"); return digest.digest(input.getBytes()); } private static byte[] sha224(String input) throws NoSuchAlgorithmException { MessageDigest digest = MessageDigest.getInstance("SHA-224"); return digest.digest(input.getBytes()); } private static byte[] sha256(String input) throws NoSuchAlgorithmException { MessageDigest digest = MessageDigest.getInstance("SHA-256"); return digest.digest(input.getBytes()); } private static byte[] sha384(String input) throws NoSuchAlgorithmException { MessageDigest digest = MessageDigest.getInstance("SHA-384"); return digest.digest(input.getBytes()); } private static byte[] sha512(String input) throws NoSuchAlgorithmException { MessageDigest digest = MessageDigest.getInstance("SHA-512"); return digest.digest(input.getBytes()); } private static byte[] sha3_256(String input) throws NoSuchAlgorithmException { MessageDigest digest = null; try { digest = MessageDigest.getInstance("SHA3-256", "BC"); } catch (NoSuchProviderException e) { throw new RuntimeException(e); } return digest.digest(input.getBytes()); } private static byte[] ripemd160(String input) throws NoSuchAlgorithmException { MessageDigest digest = MessageDigest.getInstance("RIPEMD160"); return digest.digest(input.getBytes()); } private static String toHexString(byte[] bytes) { return Hex.toHexString(bytes); } private static String toBase64(byte[] bytes) { return Base64.getEncoder().encodeToString(bytes); } private static String toBase64Url(byte[] bytes) { return Base64.getUrlEncoder().withoutPadding().encodeToString(bytes); } private static String toBinary(byte[] bytes) { StringBuilder binaryString = new StringBuilder(); for (byte b : bytes) { for (int i = 7; i >= 0; i--) { binaryString.append(((b >> i) & 1) == 1 ? '1' : '0'); } binaryString.append(""); } return binaryString.toString().trim(); } private static String encryption(byte[] bytes, int encoding) { switch (encoding) { case 1: return toHexString(bytes); case 2: return toBinary(bytes); case 3: return toBase64(bytes); case 4: return toBase64Url(bytes); default: return ""; } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。