zoukankan      html  css  js  c++  java
  • Md5Utils(密码加密类包)


    package com.lau.soft.Utils;

    import java.io.IOException;
    import java.io.InputStream;
    import java.security.MessageDigest;
    import java.security.NoSuchAlgorithmException;

    public class MD5Utils {
    /**
    * 可以把一段文字转换为MD
    * Can convert a file to MD5
    * @param text
    * @return md5
    */
    public static String encode(String text){
    try {
    MessageDigest digest = MessageDigest.getInstance("md5");
    byte[] buffer = digest.digest(text.getBytes());
    // byte -128 ---- 127
    StringBuffer sb = new StringBuffer();
    for (byte b : buffer) {
    int a = b & 0xff;
    // Log.d(TAG, "" + a);
    String hex = Integer.toHexString(a);

    if (hex.length() == 1) {
    hex = 0 + hex;
    }
    sb.append(hex);
    }
    return sb.toString();
    } catch (NoSuchAlgorithmException e) {
    e.printStackTrace();
    }
    return null;
    }

    /***
    * 任意文件转换成Md5
    * Can convert a text to MD5
    * @param inputStream
    * @return md5
    */

    public static String encode(InputStream in) {
    try {
    MessageDigest digester = MessageDigest.getInstance("MD5");
    byte[] bytes = new byte[8192];
    int byteCount;
    while ((byteCount = in.read(bytes)) > 0) {
    digester.update(bytes, 0, byteCount);
    }
    byte[] digest = digester.digest();

    // byte -128 ---- 127
    StringBuffer sb = new StringBuffer();
    for (byte b : digest) {
    int a = b & 0xff;
    // Log.d(TAG, "" + a);

    String hex = Integer.toHexString(a);

    if (hex.length() == 1) {
    hex = 0 + hex;
    }

    sb.append(hex);
    }

    return sb.toString();
    } catch (Exception e) {
    e.printStackTrace();
    } finally {
    if (in != null) {
    try {
    in.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    in = null;
    }
    }
    return null;
    }



    }
  • 相关阅读:
    深入NET框架
    解决idea中maven的pom文件不会自动下载jar包问题
    JSP中的作用域
    转发与重定向
    JSP内置对象
    JNDI与连接池
    文件上传
    七大设计原则
    第六章 初始继承和多态
    C#和.NET框架
  • 原文地址:https://www.cnblogs.com/liuyunche/p/14298237.html
Copyright © 2011-2022 走看看