MD5加密技术
2016-05-23 11:13
288 查看
直接上代码,如下:
public class MD5Demoe01 {
public static String hashKeyForDisk(String key) {
String cacheKey;
try {
final MessageDigest mDigest = MessageDigest.getInstance("MD5");
mDigest.update(key.getBytes());
cacheKey = bytesToHexString(mDigest.digest());
} catch (NoSuchAlgorithmException e) {
cacheKey = String.valueOf(key.hashCode());
}
return cacheKey;
}
private static String bytesToHexString(byte[] bytes) {
// http://stackoverflow.com/questions/332079 StringBuilder sb = new StringBuilder();
for (int i = 0; i < bytes.length; i++) {
String hex = Integer.toHexString(0xFF & bytes[i]);
if (hex.length() == 1) {
sb.append('0');
}
sb.append(hex);
}
return sb.toString();
}
public static void main(String[] args) {
String key="123456";
key=hashKeyForDisk(key);
System.out.println(key);
}
}
public class MD5Demoe01 {
public static String hashKeyForDisk(String key) {
String cacheKey;
try {
final MessageDigest mDigest = MessageDigest.getInstance("MD5");
mDigest.update(key.getBytes());
cacheKey = bytesToHexString(mDigest.digest());
} catch (NoSuchAlgorithmException e) {
cacheKey = String.valueOf(key.hashCode());
}
return cacheKey;
}
private static String bytesToHexString(byte[] bytes) {
// http://stackoverflow.com/questions/332079 StringBuilder sb = new StringBuilder();
for (int i = 0; i < bytes.length; i++) {
String hex = Integer.toHexString(0xFF & bytes[i]);
if (hex.length() == 1) {
sb.append('0');
}
sb.append(hex);
}
return sb.toString();
}
public static void main(String[] args) {
String key="123456";
key=hashKeyForDisk(key);
System.out.println(key);
}
}
相关文章推荐
- 安卓自定义view的步骤
- 一、策略模式--小伟带你学设计模式
- [POJ3352]Road Construction(缩点,割边,桥,环)
- xtrabackup如何备份mysql数据库
- 线索二叉树
- 196. Delete Duplicate Emails
- Objective-C Runtime
- ReactiveCocoa之RACSignal核心类的使用(三)
- java学习笔记5.23对象和面向对象的思考,继承和多态
- mysql导入导出sql文件
- js循环json字符串
- 通过可视化途径理解你的Spark应用程序
- shell常用操作
- C#多线程之二:ManualResetEvent和AutoResetEvent
- 第1课:通过案例对SparkStreaming 透彻理解三板斧之一
- php 数组随机取值的简单实例
- 【转】第六届蓝桥杯决赛 第二题 完美正方形 (线段树)
- JTA入门:http://blog.chinaunix.net/uid-122937-id-3793220.html
- CentOS修改主机名(hostname)
- JSON详解