JAVA MD5 Algorithm Code

Source: Internet
Author: User

public class MD5 {

/**
* Signature String
* @param text string that needs to be signed
* @param key key
* @param input_charset encoding format
* @return Signature Results
*/
public static string sign (string text, string key, String input_charset) {
Text = text + key;
Return Digestutils.md5hex (getcontentbytes (text, input_charset));
}

/**
* Signature String
* @param text string that needs to be signed
* @param sign Signature results
* @param key key
* @param input_charset encoding format
* @return Signature Results
*/
public static Boolean verify (string text, string sign, String key, String input_charset) {
Text = text + key;
String mysign = Digestutils.md5hex (getcontentbytes (text, input_charset));
if (Mysign.equals (sign)) {
return true;
}
else {
return false;
}
}

/**
* @param content
* @param CharSet
* @return
* @throws signatureexception
* @throws unsupportedencodingexception
*/
private static byte[] Getcontentbytes (string content, String charset) {
if (charset = = NULL | | "". Equals (CharSet)) {
return Content.getbytes ();
}
try {
Return Content.getbytes (CharSet);
} catch (Unsupportedencodingexception e) {
throw new RuntimeException ("An error occurred during the MD5 signature, the specified encoding set is incorrect, the encoding set you currently specify is:" + charset);
}
}

}

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.