All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.unlaxer.util.digest.MD5 Maven / Gradle / Ivy

There is a newer version: 1.4.6
Show newest version
package org.unlaxer.util.digest;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5 {
  
//  static ThreadLocal messageDigest = new ThreadLocal<>() {
//    @Override
//    public MessageDigest initialValue() {
//      try {
//        return MessageDigest.getInstance("MD5");
//      } catch (NoSuchAlgorithmException e) {
//        throw new RuntimeException(e);
//      }
//    }
//  };
  
  public static byte[] toBytes(byte[] bytes) {
    MessageDigest instance;
    try {
      instance = MessageDigest.getInstance("MD5");
      instance.update(bytes);
      return instance.digest();
    } catch (NoSuchAlgorithmException e) {
      throw new RuntimeException(e);
    }
  }
  
  public static byte[] toBytes(String utf8String) {
    return toBytes(utf8String.getBytes(StandardCharsets.UTF_8));
  }
  
  public static String toHex(byte[] bytes) {
    byte[] bytes2 = toBytes(bytes);
    return HEX.encode(bytes2);
  }
  
  public static String toHex(String utf8String) {
    return toHex(utf8String.getBytes(StandardCharsets.UTF_8));
  }  
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy