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;
    }



    }
  • 相关阅读:
    Javascript一天学完系列(四)函数上下文bind &call
    Javascript一天学完系列(三)JavaScript面向对象
    Javascript一天学完系列(二)Callbacks回调函数
    Python(切片)
    水果篮子(母函数)
    判断链表是否有环
    链表部分逆置
    Python(List和Tuple类型)
    HDU1426(DFS)
    HDU4474(数位BFS)
  • 原文地址:https://www.cnblogs.com/liuyunche/p/14298237.html
Copyright © 2011-2022 走看看