package com.estate.expert.admin.utils;
|
|
import java.nio.charset.StandardCharsets;
|
import java.security.MessageDigest;
|
import java.security.NoSuchAlgorithmException;
|
import java.time.LocalTime;
|
|
/**
|
* MD5加密工具类
|
*/
|
public class MD5Utils {
|
|
/**
|
* 对字符串进行MD5加密
|
* @param input 输入字符串
|
* @return MD5加密后的字符串
|
*/
|
public static String encrypt(String input) {
|
try {
|
MessageDigest md = MessageDigest.getInstance("MD5");
|
byte[] hashBytes = md.digest(input.getBytes(StandardCharsets.UTF_8));
|
StringBuilder hexString = new StringBuilder();
|
for (byte hashByte : hashBytes) {
|
String hex = Integer.toHexString(0xff & hashByte);
|
if (hex.length() == 1) hexString.append('0');
|
hexString.append(hex);
|
}
|
return hexString.toString();
|
} catch (NoSuchAlgorithmException e) {
|
throw new RuntimeException("MD5 algorithm not found", e);
|
}
|
}
|
|
/**
|
* 验证密码是否匹配
|
* @param inputPassword 输入的密码
|
* @param storedHash 存储的MD5哈希值
|
* @return 是否匹配
|
*/
|
public static boolean verify(String inputPassword, String storedHash) {
|
String inputHash = encrypt(inputPassword);
|
return inputHash.equals(storedHash);
|
}
|
|
/**
|
* 判断当前时间是否在指定时间段内(字符串参数)
|
* @param startTimeStr 开始时间,格式:"HH:mm" 或 "HH:mm:ss"
|
* @param endTimeStr 结束时间,格式:"HH:mm" 或 "HH:mm:ss"
|
* @return 如果在时间段内返回true,否则返回false
|
*/
|
public static boolean isCurrentTimeInRange(String startTimeStr, String endTimeStr) {
|
LocalTime now = LocalTime.now();
|
LocalTime startTime = LocalTime.parse(fixTimeFormat(startTimeStr));
|
LocalTime endTime = LocalTime.parse(fixTimeFormat(endTimeStr));
|
|
return !now.isBefore(startTime) && now.isBefore(endTime);
|
}
|
private static String fixTimeFormat(String timeStr) {
|
// 如果只有小时和分钟,添加秒部分
|
if (timeStr.length() == 5 && timeStr.contains(":")) {
|
return timeStr + ":00";
|
}
|
return timeStr;
|
}
|
|
public static void main(String[] args) {
|
boolean currentTimeInRange = isCurrentTimeInRange("09:00", "16:50");
|
System.out.println("Current time is in range: " + currentTimeInRange);
|
}
|
}
|