123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 |
- package com.steerinfo.dil.util;
- import org.bouncycastle.crypto.digests.SHA256Digest;
- import org.bouncycastle.jcajce.provider.digest.SHA256;
- import java.io.UnsupportedEncodingException;
- import java.security.MessageDigest;
- import java.security.NoSuchAlgorithmException;
- 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());
- String appkey = "p3w1aUqaAx7jDaXsg5bss4";
- String mastersecret = "ENLIzfdMSc55VghR97lqJ4";
- String sign = appkey + formatTimeTmp() + mastersecret;
- //SHA256 sha256 = new SHA256();
- //System.out.println(sha256(1))
- System.out.println(getSHA256StrJava(sign));
- }
- public static String getSHA256StrJava(String str){
- MessageDigest messageDigest;
- String encodeStr = "";
- try {
- messageDigest = MessageDigest.getInstance("SHA-256");
- messageDigest.update(str.getBytes("UTF-8"));
- encodeStr = byte2Hex(messageDigest.digest());
- } catch (NoSuchAlgorithmException e) {
- e.printStackTrace();
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- }
- return encodeStr;
- }
- /**
- * 将byte转为16进制
- * @param bytes
- * @return
- */
- private static String byte2Hex(byte[] bytes){
- StringBuffer stringBuffer = new StringBuffer();
- String temp = null;
- for (int i=0;i<bytes.length;i++){
- temp = Integer.toHexString(bytes[i] & 0xFF);
- if (temp.length()==1){
- //1得到一位的进行补0操作
- stringBuffer.append("0");
- }
- stringBuffer.append(temp);
- }
- return stringBuffer.toString();
- }
- }
|