实现MD5序列的生成[Java]
2007-05-10 21:32
731 查看
/**
* 文件名:GenMD5.java
* 环境: GNU/Linux Ubuntu 7.04 + Eclipse 3.2 + JDK 1.6
* 功能:从String生成MD5序列
* 版本:0.0.1.0
* 作者:88250
* 日期:2007.5.10
* E-mail & MDN: DL88250@gmail.com
* QQ:845765
*/
import java.security.MessageDigest;
public class GenMD5
{
public static void main(String[] args)
{
System.out.println(compute("Test!"));
}
/**
* Computes the MD5 fingerprint of a string.
*
* @return the MD5 digest of the input <code>String</code>
*/
public static String compute(String inStr)
{
// convert input String to a char[]
// convert that char[] to byte[]
// get the md5 digest as byte[]
// bit-wise AND that byte[] with 0xff
// divpend "0" to the output StringBuffer to make sure that we don't end
// up with
// something like "e21ff" instead of "e201ff"
MessageDigest md5 = null;
try
{
md5 = MessageDigest.getInstance("MD5");
}
catch (Exception e)
{
System.out.println(e.toString());
e.printStackTrace();
return "";
}
char[] charArray = inStr.toCharArray();
byte[] byteArray = new byte[charArray.length];
for (int i = 0; i < charArray.length; i++)
{
byteArray[i] = (byte) charArray[i];
}
byte[] md5Bytes = md5.digest(byteArray);
StringBuffer hexValue = new StringBuffer();
for (int i = 0; i < md5Bytes.length; i++)
{
int val = ((int) md5Bytes[i]) & 0xff;
if (val < 16)
{
hexValue.append("0");
}
hexValue.append(Integer.toHexString(val));
}
return hexValue.toString();
}
}
* 文件名:GenMD5.java
* 环境: GNU/Linux Ubuntu 7.04 + Eclipse 3.2 + JDK 1.6
* 功能:从String生成MD5序列
* 版本:0.0.1.0
* 作者:88250
* 日期:2007.5.10
* E-mail & MDN: DL88250@gmail.com
* QQ:845765
*/
import java.security.MessageDigest;
public class GenMD5
{
public static void main(String[] args)
{
System.out.println(compute("Test!"));
}
/**
* Computes the MD5 fingerprint of a string.
*
* @return the MD5 digest of the input <code>String</code>
*/
public static String compute(String inStr)
{
// convert input String to a char[]
// convert that char[] to byte[]
// get the md5 digest as byte[]
// bit-wise AND that byte[] with 0xff
// divpend "0" to the output StringBuffer to make sure that we don't end
// up with
// something like "e21ff" instead of "e201ff"
MessageDigest md5 = null;
try
{
md5 = MessageDigest.getInstance("MD5");
}
catch (Exception e)
{
System.out.println(e.toString());
e.printStackTrace();
return "";
}
char[] charArray = inStr.toCharArray();
byte[] byteArray = new byte[charArray.length];
for (int i = 0; i < charArray.length; i++)
{
byteArray[i] = (byte) charArray[i];
}
byte[] md5Bytes = md5.digest(byteArray);
StringBuffer hexValue = new StringBuffer();
for (int i = 0; i < md5Bytes.length; i++)
{
int val = ((int) md5Bytes[i]) & 0xff;
if (val < 16)
{
hexValue.append("0");
}
hexValue.append(Integer.toHexString(val));
}
return hexValue.toString();
}
}
相关文章推荐
- [JAVA][MD5]关于MD5的实现和JAVA生成MD5
- 简单的Fibonacci序列生成程序-Java
- 基于JNI技术,实现数据库查询,在C++中MD5匹配后回调至Java层的方案
- java-web-PIO实现自动生成excel录入模板(实现下拉列表框)[原创]
- java实现图的最小生成树(森林)MST克鲁斯卡尔(Kruskal)算法
- 数据结构-图-Java实现:有向图 图存储(邻接矩阵),最小生成树,广度深度遍历,图的连通性,最短路径
- git中ssh-keygen 中生成的 randomart image 用java的实现
- java实现二维码生成,使用Google zxing
- java 双向链表实现序列
- MD5文件校验码生成java代码
- java生成MD5代码的方法
- Java实现生成和解析二维码
- MD5加密原理和Java实现
- java二维码生成与解析代码实现
- PowerDesigner(七)-面向对象模型(用例图,序列图,类图,生成Java源代码及Java源代码生成类图)
- java md5 的实现
- Java实现指定数据表导出生成Excel
- 利用先序和中序非递归生成二叉树(java实现)
- [JAVA] 生成MD5 java.security.MessageDigest BUG
- 生成真值组合【Java实现】