|
- package com.ningdatech.kqapi.common.util;
-
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
-
- import java.nio.charset.StandardCharsets;
- import java.security.MessageDigest;
-
- public class Md5Utils {
- private static final Logger LOGGER = LoggerFactory.getLogger(Md5Utils.class);
-
- public static byte[] md5(String s) {
- MessageDigest algorithm;
- try {
- algorithm = MessageDigest.getInstance("MD5");
- algorithm.reset();
- algorithm.update(s.getBytes(StandardCharsets.UTF_8));
- return algorithm.digest();
- } catch (Exception e) {
- LOGGER.error("MD5 Error...",e.getMessage());
- }
- return null;
- }
-
- private static String toHex(byte[] hash) {
- if (hash == null) {
- return null;
- }
- StringBuilder buf = new StringBuilder(hash.length * 2);
- int i;
- for (i = 0; i < hash.length; i++) {
- if ((hash[i] & 0xff) < 0x10) {
- buf.append("0");
- }
- buf.append(Long.toString(hash[i] & 0xff, 16));
- }
- return buf.toString();
- }
-
- public static String hash(String s) {
- try {
- String hex = toHex(md5(s));
- if (hex == null) {
- return s;
- }
- return new String(hex.getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8);
- } catch (Exception e) {
- LOGGER.error("not supported charset:", e.getMessage());
- return s;
- }
- }
- }
|