Package cn.springmvc.util;
Import Java.security.MessageDigest;
Import java.security.NoSuchAlgorithmException;
public class Securityidutil {
/**
* Encrypt string using MD5 algorithm
*/
Public final static string encryptMD5 (string source) {
if (Source = = null) {
Source = "";
}
String result = "";
try {
result = Encrypt (source, "MD5");
} catch (NoSuchAlgorithmException ex) {
This should never happen
throw new RuntimeException (ex);
}
return result;
}
/**
* Encrypt string using SHA algorithm
*/
Public final static string Encryptsha (string source) {
if (Source = = null) {
Source = "";
}
String result = "";
try {
result = Encrypt (source, "SHA");
} catch (NoSuchAlgorithmException ex) {
This should never happen
throw new RuntimeException (ex);
}
return result;
}
/**
* Encrypt String
*/
Private final static string encrypt (string source, string algorithm)
Throws NoSuchAlgorithmException {
byte[] Resbytearray = Encrypt (Source.getbytes (), algorithm);
Return tohexstring (Resbytearray);
}
/**
* Encrypt byte array.
*/
Private final static byte[] Encrypt (byte[] source, String algorithm)
Throws NoSuchAlgorithmException {
MessageDigest MD = messagedigest.getinstance (algorithm);
Md.reset ();
Md.update (source);
return Md.digest ();
}
/**
* Get hex string from byte array
*/
Private final static String tohexstring (byte[] res) {
StringBuffer sb = new StringBuffer (res.length << 1);
for (int i = 0; i < res.length; i++) {
String digit = integer.tohexstring (0xFF & Res[i]);
if (digit.length () = = 1) {
digit = ' 0 ' + digit;
}
Sb.append (digit);
}
Return sb.tostring (). toUpperCase ();
}
}
is doing a project recording project point by bit drop
Password encryption MD5 and SHA