java 使用Spring工具类DigestUtils生成MD5

调用http接口时,sign 参数需要将appKey+requestId+long类型时间生成md5(小写)

下面将使用Spring自带的工具类生成md5(原因简单),不用自己编写md5工具类

只需一行代码即可:

String md5Str = DigestUtils.md5DigestAsHex(stb.toString().getBytes());

以下是完成代码

一:创建实体类:

package com.example.demo.mon;

public class SingCode {

    private String appappKey;
    private long requestTime;
    private String requestId;

    public SingCode() {
    }

    public SingCode(String appappKey, long requestTime, String requestId) {
        this.appappKey = appappKey;
        this.requestTime = requestTime;
        this.requestId = requestId;
    }

    public String getAppappKey() {
        return appappKey;
    }

    public void setAppappKey(String appappKey) {
        this.appappKey = appappKey;
    }

    public long getRequestTime() {
        return requestTime;
    }

    public void setRequestTime(long requestTime) {
        this.requestTime = requestTime;
    }

    public String getRequestId() {
        return requestId;
    }

    public void setRequestId(String requestId) {
        this.requestId = requestId;
    }

    @Override
    public String toString() {
        return "SingCode{" +
                "appappKey='" + appappKey + '\'' +
                ", requestTime=" + requestTime +
                ", requestId='" + requestId + '\'' +
                '}';
    }
}

二:创建生成md5类

package com.example.demo.utils;

import com.example.demo.mon.SingCode;
import org.springframework.util.DigestUtils;

import java.util.Date;

public class Md5Config {

    //appKey密钥
    private String appKey="c10b822e095d11ec954598f181b8cddc";
    //requestId
    private String requestId="eed05a3ac197475e9cdef908f621733a";

    public void toMd5(){

        SingCode singCode = new SingCode();
        singCode.setAppappKey(appKey);
        singCode.setRequestId(requestId);
        //获取long类型时间
        singCode.setRequestTime(new Date().getTime());

        String appappKey = singCode.getAppappKey();
        String requestId = singCode.getRequestId();
        long requestTime = singCode.getRequestTime();

        StringBuffer stb = new StringBuffer();
        //用StringBuffer将字符串拼接
        stb.append(appappKey).append(requestId).append(requestTime);
        System.out.println(stb);

        //使用Spring工具类DigestUtils生成MD5
        String md5Str = DigestUtils.md5DigestAsHex(stb.toString().getBytes());
        System.out.println(md5Str);
    }

    
    public static void main(String[] args) {
        new Md5Config().toMd5();
    }

}

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值