Package com.angsentech.ssm.util;
Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import java.io.UnsupportedEncodingException;
Import Java.security.MessageDigest;
Import java.security.NoSuchAlgorithmException;
/**
* MD5 Cryptographic Processing Tool class
* @author Administrator
*
*/
public class Md5utils {
/**
* Default password string combination, used to convert bytes to 16 binary characters, Apache check the correctness of the downloaded file is the default of this combination
*/
protected static char hexdigits[] = {' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ',
' 7 ', ' 8 ', ' 9 ', ' A ', ' B ', ' C ', ' d ', ' e ', ' f '};
protected static MessageDigest messagedigest = null;
static {
try {
MessageDigest = Messagedigest.getinstance ("MD5");
} catch (NoSuchAlgorithmException Nsaex) {
System.err.println (MD5Utils.class.getName ()
+ "Initialization failed, MessageDigest does not support Md5util. ");
Nsaex.printstacktrace ();
}
}
/**
* Generate MD5 checksum for string
*
* @param s
* @return
*/
public static string getmd5string (string s) {
Return getmd5string (S.getbytes ());
}
/**
* Determine if the MD5 check code of the string matches a known MD5 code
*
* @param password
* The string to validate
* @param md5pwdstr
* Known MD5 check code
* @return
*/
public static Boolean IsEqualsToMd5 (string password, string md5pwdstr) {
String s = getmd5string (password);
Return S.equals (MD5PWDSTR);
}
/**
* MD5 checksum value of generated file
*
* @param file
* @return
* @throws IOException
*/
public static String getfilemd5string (file file) throws IOException {
InputStream fis;
FIS = new FileInputStream (file);
byte[] buffer = new byte[1024];
int numread = 0;
while ((Numread = fis.read (buffer)) > 0) {
Messagedigest.update (buffer, 0, numread);
}
Fis.close ();
Return Buffertohex (Messagedigest.digest ());
}
/**
* Generate MD5 checksum value for byte array
*
* @param s
* @return
*/
public static String getmd5string (byte[] bytes) {
Messagedigest.update (bytes);
Return Buffertohex (Messagedigest.digest ());
}
private static String Buffertohex (byte bytes[]) {
Return Buffertohex (bytes, 0, bytes.length);
}
private static String Buffertohex (byte bytes[], int m, int n) {
StringBuffer StringBuffer = new StringBuffer (2 * n);
int k = m + N;
for (int l = m; l < K; l++) {
Appendhexpair (Bytes[l], stringbuffer);
}
return stringbuffer.tostring ();
}
private static void Appendhexpair (Byte bt, StringBuffer StringBuffer) {
char C0 = hexdigits[(BT & 0xf0) >> 4];//byte-high 4-bit numeric conversion, >>>
Move to the right of the logic, move the symbol bit to the right, there is no difference between the two symbols found here
Char C1 = HEXDIGITS[BT & 0xf];//number conversion with low 4 bits in bytes
Stringbuffer.append (C0);
Stringbuffer.append (C1);
}
/**
* Encrypt the source string to a byte array using MD5
* @param source
* @return
*/
public static byte[] Encode2bytes (String source) {
byte[] result = NULL;
try {
MessageDigest MD = messagedigest.getinstance ("MD5");
Md.reset ();
Md.update (Source.getbytes ("UTF-8"));
result = Md.digest ();
} catch (NoSuchAlgorithmException e) {
E.printstacktrace ();
} catch (Unsupportedencodingexception e) {
E.printstacktrace ();
}
return result;
}
/**
* Use MD5 to encrypt the source string to 32-bit 16 binary number
* @param source
* @return
*/
public static string Encode2hex (string source) {
byte[] data = encode2bytes (source);
StringBuffer hexstring = new StringBuffer ();
for (int i = 0; i < data.length; i++) {
String hex = integer.tohexstring (0xFF & Data[i]);
if (hex.length () = = 1) {
Hexstring.append (' 0 ');
}
Hexstring.append (hex);
}
return hexstring.tostring ();
}
/**
* Verify string matches
* @param unknown string to be validated
* @param okhex using MD5 encrypted 16 binary string
* @return Match returns True, mismatch returns false
*/
public static Boolean validate (string unknown, string okhex) {
Return Okhex.equals (Encode2hex (unknown));
}
}