1 package com.cy.vcard.common.utils;
2
3 import java.math.BigInteger;
4 import java.security.MessageDigest;
5 import java.security.NoSuchAlgorithmException;
6
7 import org.springframework.util.StringUtils;
8
9
10
11 public class MD5Utils {
12 /**
13 * 对字符串进行Md5加密
14 *
15 * @param input 原文
16 * @return md5后的密文
17 */
18 public static String md5(String input) {
19 byte[] code = null;
20 try {
21 code = MessageDigest.getInstance("md5").digest(input.getBytes());
22 } catch (NoSuchAlgorithmException e) {
23 code = input.getBytes();
24 }
25 BigInteger bi = new BigInteger(code);
26 return bi.abs().toString(32).toUpperCase();
27 }
28
29 /**
30 * 对字符串进行Md5加密
31 *
32 * @param input 原文
33 * @param salt 随机数
34 * @return string
35 */
36 public static String generatePasswordMD5(String input, String salt) {
37 if(StringUtils.isEmpty(salt)) {
38 salt = "";
39 }
40 return md5(salt + md5(input));
41 }
42
43 public static void main(String[] args) {
44 System.out.println(md5("111111"));
45 }
46
47 }