123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- package cn.efunbox.base.util;
- import lombok.extern.slf4j.Slf4j;
- import org.apache.commons.codec.digest.DigestUtils;
- import java.io.UnsupportedEncodingException;
- import java.security.MessageDigest;
- import java.security.NoSuchAlgorithmException;
- import java.security.SignatureException;
- @Slf4j
- public class MD5 {
- public MD5() {
- }
- public static final String byte2hexString(byte[] bytes) {
- StringBuffer buf = new StringBuffer(bytes.length * 2);
- for (int i = 0; i < bytes.length; i++) {
- if (((int) bytes[i] & 0xff) < 0x10) {
- buf.append("0");
- }
- buf.append(Long.toString((int) bytes[i] & 0xff, 16));
- }
- return buf.toString();
- }
- /**
- * md5加密
- * @param sourceString
- * @return
- */
- public static String MD5Encode(String sourceString) {
- String resultString = null;
- try {
- resultString = new String(sourceString);
- MessageDigest md = MessageDigest.getInstance("MD5");
- resultString = byte2hexString(md.digest(resultString.getBytes()));
- } catch (Exception ex) {
- }
- return resultString;
- }
-
- public static String getMD5Str(String str) {
- MessageDigest messageDigest = null;
- try {
- messageDigest = MessageDigest.getInstance("MD5");
- messageDigest.reset();
- messageDigest.update(str.getBytes("UTF-8"));
- } catch (NoSuchAlgorithmException e) {
- System.out.println("NoSuchAlgorithmException caught!");
- System.exit(-1);
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- }
- byte[] byteArray = messageDigest.digest();
- StringBuffer md5StrBuff = new StringBuffer();
- for (int i = 0; i < byteArray.length; i++) {
- if (Integer.toHexString(0xFF & byteArray[i]).length() == 1) {
- md5StrBuff.append("0").append(Integer.toHexString(0xFF & byteArray[i]));
- } else {
- md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));
- }
- }
-
- return md5StrBuff.toString();
- }
- /**
- * 签名字符串
- * @param text 需要签名的字符串
- * @param sign 签名结果
- * @param key 密钥
- * @return 签名结果
- */
- public static boolean verify(String text, String sign, String key) {
- text = text + key;
- String mysign = DigestUtils.md5Hex(getContentBytes(text, "utf-8"));
- log.info("text : " + text);
- log.info("sign : " + mysign);
- 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("MD5签名过程中出现错误,指定的编码集不对,您目前指定的编码集是:" + charset);
- }
- }
- public static void main(String[] args) throws UnsupportedEncodingException {
- String md5Str = getMD5Str("ylXny2u:tUagqa8e,#1596164432");
- System.out.println(md5Str);
- }
- }
|