MD5工具類

public class Md5Util {
  public static String getMd5(String password){
    String algorithm = "MD5";
    MessageDigest instance = null;

    try {
      instance = MessageDigest.getInstance(algorithm);
    } catch (NoSuchAlgorithmException e) {
      e.printStackTrace();
    }
    byte[] digest = instance.digest(password.getBytes());

    char[] encodeHex = Hex.encodeHex(digest);

    return new String(encodeHex);
  }ui

  public static String getMd5(File file) {
    //文件流
    FileInputStream fis = null;

    //獲取MD5信息
    String md5 = null;

    try {
      fis = new FileInputStream(file);
      md5 = DigestUtils.md5Hex(IOUtils.toByteArray(fis));
      } catch (IOException e) {
        e.printStackTrace();
      } finally{
      //關閉流
      IOUtils.closeQuietly(fis);
    }
  return md5;
  }
}code

相關文章
相關標籤/搜索