zoukankan      html  css  js  c++  java
  • MD5加密

    MD5用的是哈希函数,在计算机网络中应用较多的不可逆加密算法有RSA公司发明的MD5算法和由美国国家技术标准研究所建议的安全散列算法SHA.

    MD5可以查询MD5字典

    public class MD5 {
    private static final Logger log = LoggerFactory.getLogger(MD5.class);
    private static char[] digits = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
    private static Map<Character, Integer> rDigits = new HashMap<Character, Integer>(16);
    static {
    for (int i = 0; i < digits.length; ++i) {
    rDigits.put(digits[i], i);
    }
    }
    private static MD5 me = new MD5();
    private MessageDigest mHasher;
    private final ReentrantLock opLock = new ReentrantLock();

    private MD5() {
    try {
    this.mHasher = MessageDigest.getInstance("md5");
    } catch (Exception e) {
    throw new RuntimeException(e);
    }
    }

    public static MD5 getInstance() {
    return me;
    }

    public String getMD5String(String content) {
    return this.bytes2string(this.hash(content));
    }

    public String getMD5String(byte[] content) {
    return this.bytes2string(this.hash(content));
    }

    public byte[] getMD5Bytes(byte[] content) {
    return this.hash(content);
    }

    public byte[] hash(String str) {
    this.opLock.lock();
    try {
    byte[] bt = this.mHasher.digest(str.getBytes("utf-8"));
    if (null == bt || bt.length != 16) {
    throw new IllegalArgumentException("md5 need");
    }
    return bt;
    } catch (UnsupportedEncodingException e) {
    throw new RuntimeException("unsupported utf-8 encoding", e);
    } finally {
    this.opLock.unlock();
    }
    }

    public byte[] hash(byte[] data) {
    this.opLock.lock();
    try {
    byte[] bt = this.mHasher.digest(data);
    if (null == bt || bt.length != 16) {
    throw new IllegalArgumentException("md5 need");
    }
    return bt;
    } finally {
    this.opLock.unlock();
    }
    }

    public String bytes2string(byte[] bt) {
    int l = bt.length;
    char[] out = new char[l << 1];
    for (int i = 0, j = 0; i < l; i++) {
    out[j++] = digits[(0xF0 & bt[i]) >>> 4];
    out[j++] = digits[0x0F & bt[i]];
    }
    if (log.isDebugEnabled()) {
    log.debug("[hash]" + new String(out));
    }
    return new String(out);
    }

    public byte[] string2bytes(String str) {
    if (null == str) {
    throw new NullPointerException("Argument is not allowed empty");
    }
    if (str.length() != 32) {
    throw new IllegalArgumentException("String length must equals 32");
    }
    byte[] data = new byte[16];
    char[] chs = str.toCharArray();
    for (int i = 0; i < 16; ++i) {
    int h = rDigits.get(chs[i * 2]).intValue();
    int l = rDigits.get(chs[i * 2 + 1]).intValue();
    data[i] = (byte) ((h & 0x0F) << 4 | l & 0x0F);
    }
    return data;
    }

    public static void main(String[] args) {
    System.out.println(MD5.getInstance().getMD5String("ymapp20171017015316" +
    "{"MDL_CustomerInfoB":{"CustomerGUID":"A5D4C6FE-7D38-41AF-84E3-D4E3BAA74123","customerLoginName":"ling","password":"123","customerName":"\u51cc\u5b87","mailbox":"1000@qq.com","CustomerType":0,"CustomerLevel":11,"documentType":"","idNumber":"","legalRepresentative":"","unitOfficeAddress":"","unitSize":"","CorporateType":0,"unitOrganizationCode":"","address":"","zipCode":"","contactsPhoneCode":86,"contactsPhone":"","qqNumber":"","state":1,"ifVerified":0,"LastSetLevelUserGUID":"00000000-0000-0000-0000-000000000000","lastSetLevelTime":"2017-10-17 09:53:16","createTime":"2017-10-17 09:53:16"}}wqevds2135fs1sf"));
    }
    }
  • 相关阅读:
    BOOST库 消息队列 紧急注意事项 what(): boost::interprocess_exception::library_error
    BOOST 环形队列circular_buffer
    Linux ALSA音频库(二) 环境测试+音频合成+语音切换 项目代码分享
    系统编程-进程-先后fork或open一个文件的区别
    实战:单例的析构,为什么可以析构,重复析构等注意事项
    系统编程-进程间通信-无名管道
    4.1 urllib--通过URL打开任意资源--2
    4.1 urllib--通过URL打开任意资源
    第四章 4.1 urllib--通过URL打开任意资源
    3.5 爬虫身份识别与实现网络爬虫技术语言
  • 原文地址:https://www.cnblogs.com/dzcWeb/p/7773724.html
Copyright © 2011-2022 走看看