首页 > 其他 > 详细

封装MD5 密码加密(不可逆)

时间:2019-11-09 12:17:43      阅读:88      评论:0      收藏:0      [点我收藏+]

package com.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Random;

public class MD5Utils {
public static String getMd5(String plainText) {
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(plainText.getBytes());
byte b[] = md.digest();

int i;

StringBuffer buf = new StringBuffer("");
for (int offset = 0; offset < b.length; offset++) {
i = b[offset];
if (i < 0)
i += 256;
if (i < 16)
buf.append("0");
buf.append(Integer.toHexString(i));
}
// 32位
// return buf.toString();
// 16位
// return buf.toString().substring(0, 16);

return buf.toString();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return null;
}

}
public static String getMd5(String plainText,int length) {
return getMd5(plainText).substring(0, length);
}

public static int getRandomCode(){
int max=9999;
int min=1111;
Random random = new Random();
return random.nextInt(max)%(max-min+1) + min;
}

}

封装MD5 密码加密(不可逆)

原文:https://www.cnblogs.com/linlangtianshang/p/11824773.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!