12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- package com.steerinfo.dil.util;
- import java.security.MessageDigest;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import java.util.SimpleTimeZone;
- /**
- * @author luobang
- * @create 2021-10-29 10:16
- */
- public class MD5Util {
- /***
- * MD5加密 生成32位md5码
- *
- * @param inStr 待加密字符串
- * @return 返回32位md5码
- */
- public static String md5Encode(String inStr) throws Exception {
- MessageDigest md5 = null;
- try {
- md5 = MessageDigest.getInstance("MD5");
- } catch (Exception e) {
- System.out.println(e.toString());
- e.printStackTrace();
- return "";
- }
- byte[] byteArray = inStr.getBytes("UTF-8");
- byte[] md5Bytes = md5.digest(byteArray);
- StringBuffer hexValue = new StringBuffer();
- for (int i = 0; i < md5Bytes.length; i++) {
- int val = ((int) md5Bytes[i]) & 0xff;
- if (val < 16) {
- hexValue.append("0");
- }
- hexValue.append(Integer.toHexString(val));
- }
- return hexValue.toString();
- }
- public static String formatTimeTmp(){
- SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
- Date date = new Date();
- String timeTmp = simpleDateFormat.format(date);
- return timeTmp;
- }
- public static String sign(String AppId,String secretKey) throws Exception {
- String instr = AppId + secretKey + formatTimeTmp();
- return md5Encode(instr);
- }
- /**
- *测试
- * @throws Exception
- */
- public static void main(String[] args) throws Exception {
- String ApiId = "EUCP-EMY-SMS1-10QNI";
- String sererct = "EE20B0B28B75E567";
- System.out.println(formatTimeTmp());
- System.out.println(sign(ApiId,sererct));
- }
- }
|