Java tutorial
/* * Copyright 2015-9999 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.beginner.core.utils; import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import org.apache.commons.codec.digest.DigestUtils; public class MD5 { public static String md5(String str) { try { MessageDigest md = MessageDigest.getInstance("MD5"); md.update(str.getBytes()); byte b[] = md.digest(); int i; StringBuffer buf = new StringBuffer(""); for (int offset = 0; offset < b.length; offset++) { i = b[offset]; if (i < 0) i += 256; if (i < 16) buf.append("0"); buf.append(Integer.toHexString(i)); } str = buf.toString(); } catch (Exception e) { e.printStackTrace(); } return str; } /** * sign(????) <br /> * (??? ?) * @param text ??? * @param key * @param input_charset ?? * @return * String ?? * @exception * @since 1.0.0 */ public static String sign(String text, String key, String input_charset) { text = text + key; return DigestUtils.md5Hex(getContentBytes(text, input_charset)); } /** * verify(????) <br /> * (??? ?) * @param text ??? * @param sign ?? * @param key * @param input_charset ?? * @return * boolean ?? * @exception * @since 1.0.0 */ 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; } } /** * getContentBytes(???) <br /> * (??? ?) * @param content * @param charset * @return * byte[] * @exception * @since 1.0.0 */ 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( "MD5??,??,??:" + charset); } } public static void main(String[] args) { String str = "test" + "jp150107113" + "RTEHKA" + "4"; System.out.println(str); System.out.println(md5(str)); } }