Password encryption MD5 and SHA

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.