Java【算法 05】通过时间获取8位验证码(每两个小时生成一个)源码分享

通过时间获取验证码

  • 1.需求
  • 2.代码实现
    • [2.1 依赖](#2.1 依赖)
    • [2.2 时间参数处理方法](#2.2 时间参数处理方法)
    • [2.3 截取验证码方法](#2.3 截取验证码方法)
    • [2.4 验证方法](#2.4 验证方法)
  • 3.总结

1.需求

要求是很简单的,每个验证码的有效时间是2小时,这个并不是收到验证码开始计时的,而是每个两小时的时间段使用的是相同的验证码。

2.代码实现

2.1 依赖

xml 复制代码
<dependency>
	<groupId>gov.nist.math</groupId>
	<artifactId>jama</artifactId>
	<version>1.0.3</version>
</dependency>

<dependency>
	<groupId>commons-codec</groupId>
	<artifactId>commons-codec</artifactId>
	<version>1.6</version>
</dependency>

2.2 时间参数处理方法

2个小时处理为相同的值

java 复制代码
@Slf4j
public class VerificationCodeUtil {

    /**
     * 时间字符串
     *
     * @param dateStr yyyy-MM-dd HH:mm:ss
     */
    public static String getCode(String dateStr) {
        int dataStrLength = 13;
        try {
            if (dateStr.length() >= dataStrLength) {
                String yearMonthDay = dateStr.substring(0, 10);
                int hour = Integer.parseInt(dateStr.substring(11, 13));
                int twoHour = 2;
                if (hour % twoHour != 0) {
                    hour--;
                }
                String md5Str = DigestUtils.md5Hex("vc#" + yearMonthDay + hour);
                return getCodeByMd5(md5Str);
            } else {
                log.error("dateStr [{}] not match format [yyyy-MM-dd HH:mm:ss]!", dateStr);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("dateStr [{}] not match format [yyyy-MM-dd HH:mm:ss]!", dateStr);
        }
        return dateStr;
    }
}

2.3 截取验证码方法

java 复制代码
@Slf4j
public class VerificationCodeUtil {
    // 对指定字符串生成验证码
    private static String getCodeByMd5(String md5Str) {
        try {
            byte[] md5 = md5Str.getBytes();
            double[][] preMatrix = new double[4][8];
            for (int j = 0; j < 4; j++) {
                for (int k = 0; k < 8; k++) {
                    preMatrix[j][k] = md5[j * 8 + k];
                }
            }
            Matrix matrix = new Matrix(preMatrix);
            Matrix matrix1 = matrix.getMatrix(1, 2, 2, 5);
            Matrix matrix2 = matrix.transpose();
            Matrix matrix21 = matrix2.getMatrix(0, 3, 0, 3);
            Matrix matrix22 = matrix2.getMatrix(4, 7, 0, 3);
            Matrix matrix3 = matrix21.plus(matrix22);
            Matrix result = matrix1.times(matrix3);

            double[][] re = result.getArray();
            StringBuilder str = new StringBuilder();
            for (double[] doubles : re) {
                for (double aDouble : doubles) {
                    int a = (int) aDouble % 16;
                    str.append(Integer.toHexString(a));
                }
            }
            return str.toString().toUpperCase();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}

2.4 验证方法

java 复制代码
@Slf4j
public class VerificationCodeUtil {
    public static void main(String[] args) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        DateTime parse = DateUtil.parse("2023-11-09 23:59:59", "yyyy-MM-dd HH:mm:ss");
        String format = df.format(parse);
        System.out.println(getCode(format));
        // 00:00 3A756DFC
        // 00:59 3A756DFC
        // 01:59 3A756DFC
        // 01:59 3A756DFC
        // 02:00 9E937D4B
        // 02:59 9E937D4B
        // 03:00 9E937D4B
        // 22:00 D014DD79
        // 23:59 D014DD79        
    }
}

3.总结

很简单的算法分享。优点:

  • 不需要将生成的验证码缓存。
  • 时间入参,能够重复获取相同的值。
相关推荐
能摆一天是一天4 分钟前
JAVA stream().flatMap()
java·windows
焦耳加热25 分钟前
阿德莱德大学Nat. Commun.:盐模板策略实现废弃塑料到单原子催化剂的高值转化,推动环境与能源催化应用
人工智能·算法·机器学习·能源·材料工程
wan5555cn33 分钟前
多张图片生成视频模型技术深度解析
人工智能·笔记·深度学习·算法·音视频
颜如玉1 小时前
🤲🏻🤲🏻🤲🏻临时重定向一定要能重定向🤲🏻🤲🏻🤲🏻
java·http·源码
u6061 小时前
常用排序算法核心知识点梳理
算法·排序
程序员的世界你不懂2 小时前
【Flask】测试平台开发,新增说明书编写和展示功能 第二十三篇
java·前端·数据库
星空寻流年2 小时前
设计模式第一章(建造者模式)
java·设计模式·建造者模式
gb42152873 小时前
java中将租户ID包装为JSQLParser的StringValue表达式对象,JSQLParser指的是?
java·开发语言·python
曾经的三心草3 小时前
Python2-工具安装使用-anaconda-jupyter-PyCharm-Matplotlib
android·java·服务器
蒋星熠3 小时前
Flutter跨平台工程实践与原理透视:从渲染引擎到高质产物
开发语言·python·算法·flutter·设计模式·性能优化·硬件工程