字符串处理类
来源:互联网 发布:tensorflow 指定显卡 编辑:程序博客网 时间:2024/05/17 06:59
import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.security.MessageDigest;/** * MD5加密工具类 */public class MD5EncoderUtil { private final static String[] hexDigits = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f" }; private static String algorithm = "MD5"; public static String encode(String salt, String rawPass) { return encode(rawPass.toLowerCase() + salt); } public static String encode(String rawPass) { String result = null; try { MessageDigest md5 = MessageDigest.getInstance(algorithm); // 加密后的字符串 result = byteArrayToHexString(md5.digest(rawPass.getBytes("utf-8"))); } catch (Exception ex) { ex.printStackTrace(); } return result; } public static String getFileMd5(File file) { FileInputStream fileInputStream = null; try { MessageDigest md5 = MessageDigest.getInstance(algorithm); fileInputStream = new FileInputStream(file); byte[] buffer = new byte[8192]; int length; while ((length = fileInputStream.read(buffer)) != -1) { md5.update(buffer, 0, length); } return byteArrayToHexString(md5.digest()); } catch (Exception e) { e.printStackTrace(); return null; } finally { if (fileInputStream != null) { try { fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } } public static boolean isPasswordValid(String encPass, String loginName, String rawPass) { String pass1 = encPass; String pass2 = encode(loginName, rawPass); return pass1.toUpperCase().equals(pass2.toUpperCase()); } /* * 转换字节数组为16进制字串 */ private static String byteArrayToHexString(byte[] b) { StringBuffer resultSb = new StringBuffer(); for (int i = 0; i < b.length; i++) { resultSb.append(byteToHexString(b[i])); } return resultSb.toString(); } private static String byteToHexString(byte b) { int n = b; if (n < 0) n = 256 + n; int d1 = n / 16; int d2 = n % 16; return hexDigits[d1] + hexDigits[d2]; }}
0 0
- C#字符串处理类
- C#字符串处理类
- C#字符串处理类
- C#字符串处理类
- C#字符串处理类
- 字符串处理类
- 字符串处理类
- Java 字符串处理类
- C#字符串处理类
- 字符串处理类库
- 字符串处理类
- C#字符串处理类
- 工具类:字符串处理
- 字符串处理工具类
- 字符串处理类StringUtils
- StringUtils处理字符串类
- C#字符串处理类
- 字符串处理类
- android studio转eclipse
- OpenGL使用libPng读取png图片
- guava学习目录
- win7实现多用户同时登陆
- 台大林轩田《机器学习基石》学习笔记:相关领域与三大原则
- 字符串处理类
- NSWindowDelegate 关于窗口大小 位置 显示 等api说明
- CS229 Lecture Notes(4): Generative Learning Algorithm
- 偶然间发现的,大家可以看看
- HUD 1881 毕业bg(01背包变形)
- Android情景案例——Activity与Service通讯交互
- android studio转eclipse(二)
- 专利申请流程
- 解决通过UIAlertView按钮点击事件pop回上级页面键盘会响应问题