【加密算法】MD5、SHA算法加密工具类

加密代码如下,支持MD5、SHA、SHA256、SHA512

 

其中,

MD5生成128位长度的密文

SHA生成160位长度的密文

SHA256生成256位长度的密文

SHA512生成512位长度的密文

 

package com.cheng2839.md5;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.MessageDigest;

/**
 * MD5信息摘要算法
 * 安全散列算法SHA
 * https://www.cnblogs.com/cheng2839/
 */
public class MD5Utils {

    private static final int BASE_NUM = 0x80;
    private static final String BASE_S = "iIHlFP4nxs&gM#J0jeSkhN6LXZQyC*qvrGAfzTK!29Uo-ORm8pWEDa$%tu3bYBVd15w7c@iwhY2@R4F-6WK$xTN1XqAMZJQU97vcsalVIrkf3pj!CHneE5*&got8D#0Odm%uBPyLGzbSj6sDCnvEuqXgZJ@M%kcp2!o1z5FfYy*GOhSIRt$8L0PQ-bax7VBreTlWHAK93&U4imwd#NOqS8kElMRNBIaLs3QnJ7&do4-fF2#P91mwr!pcbZGHU%06";
    private static final int BASE_LEN = BASE_S.length();

    private static final String MD5 = "MD5";
    private static final String SHA_1 = "SHA";
    private static final String SHA_256 = "SHA-256";
    private static final String SHA_512 = "SHA-512";

    public static String encodeMD5(String password) {
        return encodeMD5(password.getBytes());
    }
    public static String encodeSHA(String password) {
        return encodeSHA(password.getBytes());
    }
    public static String encodeSHA256(String password) {
        return encodeSHA256(password.getBytes());
    }
    public static String encodeSHA512(String password) {
        return encodeSHA512(password.getBytes());
    }

    public static String encodeMD5(File file) {
        FileInputStream fis = null;
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            fis = new FileInputStream(file);
            byte[] buffer = new byte[512];
            int len;
            while ((len=fis.read(buffer, 0, buffer.length)) > 0) {
                baos.write(buffer, 0, len);
            }
            fis.close();
            baos.close();
            return encodeMD5(baos.toByteArray());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {}
            }
        }
        return null;
    }

    public static String encodeMD5(byte[] bytes) {
        return encode(bytes, MD5);
    }
    public static String encodeSHA(byte[] bytes) {
        return encode(bytes, SHA_1);
    }
    public static String encodeSHA256(byte[] bytes) {
        return encode(bytes, SHA_256);
    }
    public static String encodeSHA512(byte[] bytes) {
        return encode(bytes, SHA_512);
    }
    private static String encode(byte[] bytes, String algorithm) {
        try {
            MessageDigest md5 = MessageDigest.getInstance(algorithm);
            md5.update(bytes);
            return toStr(md5.digest());
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    private static String toStr(byte[] bytes) {
        StringBuilder sb = new StringBuilder();
        for (byte b : bytes)
            sb.append(BASE_S.charAt((b+BASE_NUM)%BASE_LEN));
        return sb.toString();
    }

    public static void main(String[] args) {
        System.out.println(encodeMD5("https://www.cnblogs.com/cheng2839/"));
        System.out.println(encodeSHA("https://www.cnblogs.com/cheng2839/"));
        System.out.println(encodeSHA256("https://www.cnblogs.com/cheng2839/"));
        System.out.println(encodeSHA512("https://www.cnblogs.com/cheng2839/"));
    }

}

 

打印输出结果如下:

PqkDLmvrvJ$SgMlr
L10-dkkm#5qY88-PnNTp
-zZ!hX-dZcr%ZYkkZsnL1df!dwqVQxM#
GoqdJa*rS!q1rXeS-8E5@B1!--rQOcZIssckMGB1Un90F*s9JG1NRBstbBmWdMm&

 

上一篇:R语言 模糊c均值(FCM)算法程序(转)


下一篇:java比较两个集合是否相等