签名认证工具类

package com.utils;

import org.apache.commons.codec.binary.Hex;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;

public class SignUtil {
    /**
     * 时间差,最长
     */
    private static final long MAX_TIMESTAMP_DIFFERENCE = 5 * 60 * 1000;
    /**
     * 时间差,最短
     */
    private static final long MIN_TIMESTAMP_DIFFERENCE = 5 * 60 * 1000;
    /**
     * 随机数长度,最长
     */
    private static final int MAX_NONCE_LENGTH = 15;
    /**
     * 随机数长度,最短
     */
    private static final int MIN_NONCE_LENGTH = 10;

    /**
     * 校验签名
     *
     * @param userAccount 当前登录账号
     * @param nonce       随机数
     * @param timestamp   时间戳,单位:毫秒
     * @param sign        待校验签名
     * @param appId       appId
     * @param appSecret   appSecret
     * @return
     * @date 2023/06/05 上午 11:06
     */
    public static boolean check(String userAccount, String nonce, long timestamp, String sign, String appId, String appSecret) {
        if (StringUtils.isAnyBlank(userAccount, nonce, sign, appId, appSecret)) {
            return false;
        }
        if ((System.currentTimeMillis() - timestamp > MAX_TIMESTAMP_DIFFERENCE) || (timestamp - System.currentTimeMillis() > MIN_TIMESTAMP_DIFFERENCE)) {
            return false;
        }
        if (nonce.length() < MIN_NONCE_LENGTH || nonce.length() > MAX_NONCE_LENGTH) {
            return false;
        }

        return hexSHA1(appId + appSecret + userAccount + nonce + timestamp).equalsIgnoreCase(sign);
    }

    /**
     * 生成签名
     *
     * @param userAccount 当前登录账号
     * @param nonce       随机数
     * @param timestamp   时间戳,单位:毫秒
     * @param appId       appId
     * @param appSecret   appSecret
     * @return
     * @date 2023/06/05 上午 11:07
     */
    public static String sign(String userAccount, String nonce, long timestamp, String appId, String appSecret) {
        if (StringUtils.isAnyBlank(userAccount, nonce, appId, appSecret)) {
            throw new IllegalArgumentException("非法参数");
        }
        if ((System.currentTimeMillis() - timestamp > MAX_TIMESTAMP_DIFFERENCE) || (timestamp - System.currentTimeMillis() > MIN_TIMESTAMP_DIFFERENCE)) {
            throw new IllegalArgumentException("非法参数");
        }
        if (nonce.length() < MIN_NONCE_LENGTH || nonce.length() > MAX_NONCE_LENGTH) {
            throw new IllegalArgumentException("非法参数");
        }

        return hexSHA1(appId + appSecret + userAccount + nonce + timestamp);
    }

    /**
     * 算法
     *
     * @param value 待处理数据
     * @return
     * @date 2023/06/05 上午 11:54
     */
    public static String hexSHA1(String value) {
        try {
            MessageDigest md = MessageDigest.getInstance("SHA-1");
            md.update(value.getBytes(StandardCharsets.UTF_8));
            return Hex.encodeHexString(md.digest());
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }

    /**
     * 获取随机数
     * 

(a-z, A-Z) and 0-9

* * @param length 随机数长度 * @return * @date 2023/06/05 上午 11:54 */ public static String nonce(int length) { // (a-z, A-Z) and 0-9 return RandomStringUtils.randomAlphanumeric(length); } public static void main(String[] args) { String userAccount = "xxx"; String nonce = nonce(RandomUtils.nextInt(MIN_NONCE_LENGTH, MAX_NONCE_LENGTH)); String appId = "xxx"; String appSecret = "xxx"; long time = System.currentTimeMillis(); String sign = SignUtil.sign(userAccount, nonce, time, appId, appSecret); boolean flag = SignUtil.check(userAccount, nonce, time, sign, appId, appSecret); System.out.println("sign is :" + sign); System.out.println("check flag is :" + flag); } }

你可能感兴趣的:(工具类,java,mybatis,开发语言)