package com.payease.chains.risk.utils;

/**
* md5密码加密工具类
* Created by liuxiaoming on 2017/8/28.
*/
public class MD5 {
static final int S11 = 7; static final int S12 = 12; static final int S13 = 17; static final int S14 = 22; static final int S21 = 5; static final int S22 = 9; static final int S23 = 14; static final int S24 = 20; static final int S31 = 4; static final int S32 = 11; static final int S33 = 16; static final int S34 = 23; static final int S41 = 6; static final int S42 = 10; static final int S43 = 15; static final int S44 = 21; static final byte PADDING[] = { -128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0 }; public static long b2iu(byte b) {
return (b >= 0 ? b : b & 0xff);
} public static String byteHEX(byte ib) {
char Digit[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A',
'B', 'C', 'D', 'E', 'F' };
char ob[] = new char[2];
ob[0] = Digit[ib >>> 4 & 0xf];
ob[1] = Digit[ib & 0xf];
String s = new String(ob);
return s;
} public static String encode(String s) {
MD5 m = new MD5();
return m.getMD5ofStr(s);
} private long state[]; private long count[]; private byte buffer[]; public String digestHexStr; private byte digest[]; public MD5() {
state = new long[4];
count = new long[2];
buffer = new byte[64];
digest = new byte[16];
md5Init();
} private void Decode(long output[], byte input[], int len) {
int i = 0;
for (int j = 0; j < len; j += 4) {
output[i] = b2iu(input[j]) | b2iu(input[j + 1]) << 8
| b2iu(input[j + 2]) << 16 | b2iu(input[j + 3]) << 24;
i++;
} } private void Encode(byte output[], long input[], int len) {
int i = 0;
for (int j = 0; j < len; j += 4) {
output[j] = (byte) (int) (input[i] & 255L);
output[j + 1] = (byte) (int) (input[i] >>> 8 & 255L);
output[j + 2] = (byte) (int) (input[i] >>> 16 & 255L);
output[j + 3] = (byte) (int) (input[i] >>> 24 & 255L);
i++;
} } private long F(long x, long y, long z) {
return x & y | (x ^ 0L - 1L) & z;
} private long FF(long a, long b, long c, long d, long x, long s, long ac) {
a += F(b, c, d) + x + ac;
a = (int) a << (int) s | (int) a >>> (int) (32 - s);
a += b;
return a;
} private long G(long x, long y, long z) {
return x & z | y & (z ^ 0L - 1L);
} public String getMD5ofStr(String inbuf) { md5Init(); md5Update(inbuf.getBytes(), inbuf.length());
md5Final();
digestHexStr = "";
for (int i = 0; i < 16; i++)
digestHexStr = String.valueOf(digestHexStr)
+ String.valueOf(byteHEX(digest[i])); return digestHexStr;
} private long GG(long a, long b, long c, long d, long x, long s, long ac) {
a += G(b, c, d) + x + ac;
a = (int) a << (int) s | (int) a >>> (int) (32 - s);
a += b;
return a;
} private long H(long x, long y, long z) {
return x ^ y ^ z;
} private long HH(long a, long b, long c, long d, long x, long s, long ac) {
a += H(b, c, d) + x + ac;
a = (int) a << (int) s | (int) a >>> (int) (32 - s);
a += b;
return a;
} private long I(long x, long y, long z) {
return y ^ (x | z ^ 0L - 1L);
} private long II(long a, long b, long c, long d, long x, long s, long ac) {
a += I(b, c, d) + x + ac;
a = (int) a << (int) s | (int) a >>> (int) (32 - s);
a += b;
return a;
} private void md5Final() {
byte bits[] = new byte[8];
Encode(bits, count, 8);
int index = (int) (count[0] >>> 3) & 0x3f;
int padLen = index >= 56 ? 120 - index : 56 - index;
md5Update(PADDING, padLen);
md5Update(bits, 8);
Encode(digest, state, 16);
} private void md5Init() {
count[0] = 0L;
count[1] = 0L;
state[0] = 0x67452301L;
state[1] = 0xefcdab89L;
state[2] = 0x98badcfeL;
state[3] = 0x10325476L;
} private void md5Memcpy(byte output[], byte input[], int outpos, int inpos,
int len) {
for (int i = 0; i < len; i++)
output[outpos + i] = input[inpos + i]; } private void md5Transform(byte block[]) {
long a = state[0];
long b = state[1];
long c = state[2];
long d = state[3];
long x[] = new long[16];
Decode(x, block, 64);
a = FF(a, b, c, d, x[0], 7L, 0xd76aa478L);
d = FF(d, a, b, c, x[1], 12L, 0xe8c7b756L);
c = FF(c, d, a, b, x[2], 17L, 0x242070dbL);
b = FF(b, c, d, a, x[3], 22L, 0xc1bdceeeL);
a = FF(a, b, c, d, x[4], 7L, 0xf57c0fafL);
d = FF(d, a, b, c, x[5], 12L, 0x4787c62aL);
c = FF(c, d, a, b, x[6], 17L, 0xa8304613L);
b = FF(b, c, d, a, x[7], 22L, 0xfd469501L);
a = FF(a, b, c, d, x[8], 7L, 0x698098d8L);
d = FF(d, a, b, c, x[9], 12L, 0x8b44f7afL);
c = FF(c, d, a, b, x[10], 17L, 0xffff5bb1L);
b = FF(b, c, d, a, x[11], 22L, 0x895cd7beL);
a = FF(a, b, c, d, x[12], 7L, 0x6b901122L);
d = FF(d, a, b, c, x[13], 12L, 0xfd987193L);
c = FF(c, d, a, b, x[14], 17L, 0xa679438eL);
b = FF(b, c, d, a, x[15], 22L, 0x49b40821L);
a = GG(a, b, c, d, x[1], 5L, 0xf61e2562L);
d = GG(d, a, b, c, x[6], 9L, 0xc040b340L);
c = GG(c, d, a, b, x[11], 14L, 0x265e5a51L);
b = GG(b, c, d, a, x[0], 20L, 0xe9b6c7aaL);
a = GG(a, b, c, d, x[5], 5L, 0xd62f105dL);
d = GG(d, a, b, c, x[10], 9L, 0x2441453L);
c = GG(c, d, a, b, x[15], 14L, 0xd8a1e681L);
b = GG(b, c, d, a, x[4], 20L, 0xe7d3fbc8L);
a = GG(a, b, c, d, x[9], 5L, 0x21e1cde6L);
d = GG(d, a, b, c, x[14], 9L, 0xc33707d6L);
c = GG(c, d, a, b, x[3], 14L, 0xf4d50d87L);
b = GG(b, c, d, a, x[8], 20L, 0x455a14edL);
a = GG(a, b, c, d, x[13], 5L, 0xa9e3e905L);
d = GG(d, a, b, c, x[2], 9L, 0xfcefa3f8L);
c = GG(c, d, a, b, x[7], 14L, 0x676f02d9L);
b = GG(b, c, d, a, x[12], 20L, 0x8d2a4c8aL);
a = HH(a, b, c, d, x[5], 4L, 0xfffa3942L);
d = HH(d, a, b, c, x[8], 11L, 0x8771f681L);
c = HH(c, d, a, b, x[11], 16L, 0x6d9d6122L);
b = HH(b, c, d, a, x[14], 23L, 0xfde5380cL);
a = HH(a, b, c, d, x[1], 4L, 0xa4beea44L);
d = HH(d, a, b, c, x[4], 11L, 0x4bdecfa9L);
c = HH(c, d, a, b, x[7], 16L, 0xf6bb4b60L);
b = HH(b, c, d, a, x[10], 23L, 0xbebfbc70L);
a = HH(a, b, c, d, x[13], 4L, 0x289b7ec6L);
d = HH(d, a, b, c, x[0], 11L, 0xeaa127faL);
c = HH(c, d, a, b, x[3], 16L, 0xd4ef3085L);
b = HH(b, c, d, a, x[6], 23L, 0x4881d05L);
a = HH(a, b, c, d, x[9], 4L, 0xd9d4d039L);
d = HH(d, a, b, c, x[12], 11L, 0xe6db99e5L);
c = HH(c, d, a, b, x[15], 16L, 0x1fa27cf8L);
b = HH(b, c, d, a, x[2], 23L, 0xc4ac5665L);
a = II(a, b, c, d, x[0], 6L, 0xf4292244L);
d = II(d, a, b, c, x[7], 10L, 0x432aff97L);
c = II(c, d, a, b, x[14], 15L, 0xab9423a7L);
b = II(b, c, d, a, x[5], 21L, 0xfc93a039L);
a = II(a, b, c, d, x[12], 6L, 0x655b59c3L);
d = II(d, a, b, c, x[3], 10L, 0x8f0ccc92L);
c = II(c, d, a, b, x[10], 15L, 0xffeff47dL);
b = II(b, c, d, a, x[1], 21L, 0x85845dd1L);
a = II(a, b, c, d, x[8], 6L, 0x6fa87e4fL);
d = II(d, a, b, c, x[15], 10L, 0xfe2ce6e0L);
c = II(c, d, a, b, x[6], 15L, 0xa3014314L);
b = II(b, c, d, a, x[13], 21L, 0x4e0811a1L);
a = II(a, b, c, d, x[4], 6L, 0xf7537e82L);
d = II(d, a, b, c, x[11], 10L, 0xbd3af235L);
c = II(c, d, a, b, x[2], 15L, 0x2ad7d2bbL);
b = II(b, c, d, a, x[9], 21L, 0xeb86d391L);
state[0] += a;
state[1] += b;
state[2] += c;
state[3] += d;
} private void md5Update(byte inbuf[], int inputLen) {
byte block[] = new byte[64];
int index = (int) (count[0] >>> 3) & 0x3f;
if ((count[0] += inputLen << 3) < (inputLen << 3))
count[1]++;
count[1] += inputLen >>> 29;
int partLen = 64 - index;
int i;
if (inputLen >= partLen) {
md5Memcpy(buffer, inbuf, index, 0, partLen);
md5Transform(buffer);
for (i = partLen; i + 63 < inputLen; i += 64) {
md5Memcpy(block, inbuf, 0, i, 64);
md5Transform(block);
} index = 0;
} else
i = 0;
md5Memcpy(buffer, inbuf, index, i, inputLen - i);
}
public static void main(String[] args) {
System.out.println(MD5.encode("1"));
}
}

MD5 工具类的更多相关文章

  1. MD5工具类,提供字符串MD5加密、文件MD5值获取(校验)功能

    MD5工具类,提供字符串MD5加密(校验).文件MD5值获取(校验)功能 : package com.yzu.utils; import java.io.File; import java.io.Fi ...

  2. java MD5工具类

    package com.common.tools; import java.security.MessageDigest; /** * MD5加密工具类 * <功能详细描述> * * @a ...

  3. MD5工具类-详细

    public class MD5Code { /* * 下面这些S11-S44实际上是一个4*4的矩阵,在原始的C实现中是用#define 实现的, 这里把它们实现成为static * final是表 ...

  4. MD5工具类

    package com.liu.hellomavenweb.util; import java.security.MessageDigest; /** * * @author 刘楠 * */ publ ...

  5. 对指定文件生成数字摘要的MD5工具类

    md5特点:压缩性.不可逆性,经常用于传值过程中的值加密或文件加密static char hexdigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', ...

  6. 170405、java版MD5工具类

    package com.rick.utils; import java.security.MessageDigest; import java.security.NoSuchAlgorithmExce ...

  7. MD5工具类--可以直接拿来用不抛异常的MD5Util

    public class MD5Util { private static String byteArrayToHexString(byte b[]) { StringBuffer resultSb ...

  8. c# base64及MD5工具类

    using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Lin ...

  9. 我的Java开发学习之旅------>工具类:Java获取字符串和文件进行MD5值

    ps:这几天本人用百度云盘秒传了几部大片到云盘上,几个G的文件瞬秒竟然显示"上传成功"!这真让我目瞪口呆,要是这样的话,那得多快的网速,这绝对是不可能的,也许这仅是个假象.百度了一 ...

随机推荐

  1. Mac Sublime Text 浏览器 设置快捷键 让html文件在浏览器打开

    首先,进入命令行界面(这里mac和windows一样) 应用命令下载插件 输入命令二 然后就会发现在工具栏多出一个下拉选项 mac的跟它有些区别就是快捷键 mac快捷键是^+√+ V ++字母V,就会 ...

  2. android分屏

    上手了Android N Preview,第一个不能错过的新特性就是App分屏的支持.Android7.0原生系统就可以支持两个App横屏并排或者竖屏上下摆放了.第二个新特性就是在Android TV ...

  3. IPv4&&IPv6地址结构分析

    IPv4套接字地址结构: 套接字都需要有一个指向套接字地址结构的指针作为参数.每个协议簇都定义它自己的套接字地址结构.这些结构的名字均已sockaddr_开头,并以对应每个协议族的唯一后缀结尾. wi ...

  4. Android-进程理解/进程的优先级别

    进程理解 Android系统最小的控制单元是:进程 process 应用/CPU最小的控制单元是:线程 thread 一个应用一个 process 进程 一个应用一个 package(包是唯一的) 一 ...

  5. 【分分钟内搭建一个带用户系统的博客程序(一)用户系统】asp.net core的Identity真香,EF真香!

    不用不知道,一用香到爆. 老哥是个屌丝前端,但也想写点web应用耍一耍.之前弄过了NodeJs,也弄过JAVA,最近由于写游戏的原因用C#,索性上手一波asp.net core. 这篇博客记录的是,如 ...

  6. C#——获取远程xml文件

    /// <summary> /// 加载远程XML文档 /// </summary> /// <param name="URL"></pa ...

  7. ie11下ajax用escape发送中文参数失败

    一个项目中,登录请求是ajax,get模式.登录名无中文可以正常登录:登录名是中文则偶尔可以登录,大部分情况下无法登录,ajax请求无法发送成功. 登录名是用js的escape函数转码. 经过多次测试 ...

  8. 201621123023《Java程序设计》第12周学习总结

    一.本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多流与文件相关内容. 二.书面作业 本次PTA作业题集多线程 1. 面向系统综合设计-图书馆管理系统或购物车 使用流与文件改造你的图书 ...

  9. 关于Mybatis 反向生成后 查询结果全部为null 解决办法

    今天遇到了一个问题,就是mybatis通过反向生成工具 生成的pojo类(实体类) xml文件 以及Mapper之后查询时结果为null 我写的代码怎么看都没有错 就是没有结果 后来在排除错误的时候发 ...

  10. AtcoderExaWizards 2019题解

    传送门 \(A\ Regular\ Triangle\) 咕咕 \(B\ Red\ or\ Blue\) 咕咕咕 \(C\ Snuke\ the\ Wizard\) 我可能脑子真的坏掉了-- 容易发现 ...