当前位置: 首页 > news >正文

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

通过时间获取验证码

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

1.需求

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

2.代码实现

2.1 依赖

<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个小时处理为相同的值

@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 截取验证码方法

@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 验证方法

@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.总结

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

  • 不需要将生成的验证码缓存。
  • 时间入参,能够重复获取相同的值。
http://www.lryc.cn/news/224364.html

相关文章:

  • 微服务 Spring Cloud 5,一图说透Spring Cloud微服务架构
  • conda清华源安装cuda12.1的pytorch
  • 安徽首届道医传承十八绝技发布会在合肥成功举办
  • 一款功能强大的web目录扫描器专业版
  • 【Linux网络】网卡配置与修改主机名,做好基础系统配置
  • 三大基础排序 -选择排序、冒泡排序、插入排序
  • el-form添加自定义校验规则校验el-input只能输入数字
  • ios 开发问题小集 [持续更新]
  • idea Plugins 搜索不到插件
  • 三相电机的某些实测特性曲线
  • Essential C++ 面向对象4.1 ~ 5.4
  • 数组【数据结构与算法】
  • Python克隆单个网页
  • 电脑硬盘数据恢复哪个好?值得考虑的 8 个硬盘恢复软件解决方案
  • 第二十三节——路由守卫
  • 在gitlab中的使用kaniko打造流水线
  • 【C语言 | 预处理】C语言预处理详解(一) —— #define、#under、#if、#else、#elif、#endif、#include、#error
  • 19、Flink 的Table API 和 SQL 中的自定义函数及示例(2)
  • (动手学习深度学习)第7章 残差网络---ResNet
  • 4.Pod详解
  • OCR技术狂潮:揭秘最新发展现状,引爆未来智能时代
  • 【hcie-cloud】【3】华为云Stack规划设计之华为云Stack交付综述【上】
  • Spring Ioc 容器启动流程
  • 【714. 买卖股票的最佳时机含手续费】
  • JS前端实现身份证号码合法性校验(校验码校验)
  • 操作系统 day09(线程)
  • 单通道低压 H 桥电机驱动芯片AT9110H 兼容L9110 马达驱动芯片
  • 18. 深度学习 - 从零理解神经网络
  • Pycharm加载项目时异常,看不到自己的项目文件
  • 目标检测YOLO实战应用案例100讲-基于无人机的轻量化目标检测系统设计(续)