MD5 Hashing in Java,Written by dimport
2007-04-09 11:10
302 查看
A wrapper for Java's MD5 class that makes life a little easier.
This class lets you produce an MD5 hashcode for some binary data. It is useful for storing passwords in databases or file systems, or for checking the validity of downloaded files. It also provides an example of the singleton design pattern.
import java.security.*;
public class MD5
{
private MessageDigest md = null;
static private MD5 md5 = null;
private static final char[] hexChars ={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
/**
* Constructor is private so you must use the getInstance method
*/
private MD5() throws NoSuchAlgorithmException
{
md = MessageDigest.getInstance("MD5");
}
/**
* This returns the singleton instance
*/
public static MD5 getInstance()throws NoSuchAlgorithmException
{
if (md5 == null)
{
md5 = new MD5();
}
return (md5);
}
public String hashData(byte[] dataToHash)
{
return hexStringFromBytes((calculateHash(dataToHash)));
}
private byte[] calculateHash(byte[] dataToHash)
{
md.update(dataToHash, 0, dataToHash.length);
return (md.digest());
}
public String hexStringFromBytes(byte[] b)
{
String hex = "";
int msb;
int lsb = 0;
int i;
// MSB maps to idx 0
for (i = 0; i < b.length; i++)
{
msb = ((int)b[i] & 0x000000FF) / 16;
lsb = ((int)b[i] & 0x000000FF) % 16;
hex = hex + hexChars[msb] + hexChars[lsb];
}
return(hex);
}
public static void main(String[] args)
{
try
{
MD5 md = MD5.getInstance();
System.out.println(md.hashData("hello".getBytes()));
}
catch(NoSuchAlgorithmException e)
{
e.printStackTrace(System.out);
}
}
}
This class lets you produce an MD5 hashcode for some binary data. It is useful for storing passwords in databases or file systems, or for checking the validity of downloaded files. It also provides an example of the singleton design pattern.
import java.security.*;
public class MD5
{
private MessageDigest md = null;
static private MD5 md5 = null;
private static final char[] hexChars ={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
/**
* Constructor is private so you must use the getInstance method
*/
private MD5() throws NoSuchAlgorithmException
{
md = MessageDigest.getInstance("MD5");
}
/**
* This returns the singleton instance
*/
public static MD5 getInstance()throws NoSuchAlgorithmException
{
if (md5 == null)
{
md5 = new MD5();
}
return (md5);
}
public String hashData(byte[] dataToHash)
{
return hexStringFromBytes((calculateHash(dataToHash)));
}
private byte[] calculateHash(byte[] dataToHash)
{
md.update(dataToHash, 0, dataToHash.length);
return (md.digest());
}
public String hexStringFromBytes(byte[] b)
{
String hex = "";
int msb;
int lsb = 0;
int i;
// MSB maps to idx 0
for (i = 0; i < b.length; i++)
{
msb = ((int)b[i] & 0x000000FF) / 16;
lsb = ((int)b[i] & 0x000000FF) % 16;
hex = hex + hexChars[msb] + hexChars[lsb];
}
return(hex);
}
public static void main(String[] args)
{
try
{
MD5 md = MD5.getInstance();
System.out.println(md.hashData("hello".getBytes()));
}
catch(NoSuchAlgorithmException e)
{
e.printStackTrace(System.out);
}
}
}
相关文章推荐
- MD5 Hashing in Java
- Mobile Java Eyed by Big Media Companies in the UK @ JDJ
- Spark Streaming(4):Spark updateStateByKey in Java
- Caused by: java.lang.ClassNotFoundException: *** in loader dalvik.sy
- String is passed by “reference” in Java?
- Six java books in one archive by qawa
- spring aop 报错:Caused by: java.lang.IllegalArgumentException: error at ::0 formal unbound in pointcut
- Step By Step guide to Read XML file in Java Using SAX Parser Example
- Why does wide file-stream in C++ narrow written data by default?
- Pass-by-reference in C++ and java
- 报表工具FineReport报错:java.sql.SQLException: The database is already in use by another process: lockFile
- There Is No ‘I’ in Architecture written by Dave Quick
- The import java.util.Collection conflicts with a type defined in the same file
- Disabling default console handler in Java Logger by codes
- 一个异常:Caused by: java.lang.UnsupportedClassVersionError: Bad version number in .class file (unable to
- Chicago Boss: a server framework inspired by Rails and written in Erlang
- Open Source Workflow Engines Written in Java
- Understanding How Graal Works - a Java JIT Compiler Written in Java
- Unable to locate JAR/zip in file system as specified by the driver definition: mysql-connector-java-
- Caused by: java.net.URISyntaxException: Relative path in absolute URI