Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.gitee.fufu669.utils.CachePasswordUtil Maven / Gradle / Ivy
package com.gitee.fufu669.utils;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Map;
import java.util.Random;
import com.gitee.fufu669.config.exception.CacheServerErrorCode;
import com.gitee.fufu669.common.CacheKeyCommon;
import com.gitee.fufu669.config.exception.CacheServerException;
import com.gitee.fufu669.service.CacheService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
/** @author wangfupeng */
public class CachePasswordUtil {
public static final Logger logger = LoggerFactory.getLogger(CachePasswordUtil.class);
public static String MD5_KEY = "666666666666666666666666666666";
private static final String[] PASSWORDF = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "a", "b", "c", "d",
"e", "f"};
public static final String BASE64_DES_KEY = "FP6PP66F66F";
public static String generatePasswordF(int length) {
String password = "";
Random random = CacheRandomUtil.random;
for (int i = 0; i < length; i++) {
password = password + PASSWORDF[random.nextInt(15)];
}
password = password.replace("4", "6").replace("d", "f");
return password;
}
public static String generatePasswordNumberF(int length) {
String password = "";
Random random = CacheRandomUtil.random;
for (int i = 0; i < length; i++) {
password = password + random.nextInt(10);
}
password = password.replace("4", "6");
return password;
}
public static String generatePassword(int length) {
String password = "";
Random random = CacheRandomUtil.random;
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < length; i++) {
stringBuilder.append(random.nextInt(9));
}
password = stringBuilder.toString().replace("4", "6");
return password;
}
public static String generateTokenNumber() {
String timeStamp = new SimpleDateFormat("yyyyMMddHH").format(Calendar.getInstance().getTime());
return timeStamp + generatePasswordNumberF(22);
}
public static String generateToken() {
String timeStamp = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(Calendar.getInstance().getTime());
return timeStamp + generatePasswordF(15);
}
public static String generateTokenNumberMilliSeconds() {
String timeStamp = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(Calendar.getInstance().getTime());
return timeStamp + generatePasswordNumberF(15);
}
public static String generateTokenNumberMilliSeconds(Integer number) {
String timeStamp = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(Calendar.getInstance().getTime());
if(number<=17){
return timeStamp;
}
return timeStamp + generatePasswordNumberF(number-17);
}
public static String generateToken(int length) {
String timeStamp = new SimpleDateFormat("yyyyMMdd").format(Calendar.getInstance().getTime());
return timeStamp + generatePasswordF(length - 8 > 0 ? length - 8 : 0);
}
public static String md5LowerCaseWithExtraKey(String s) {
return md5(s + MD5_KEY).toLowerCase();
}
public static String md5LowerCase(String s) {
return md5(s).toLowerCase();
}
public static String md5UpperCase(String s) {
return md5(s).toUpperCase();
}
public static String md5LowerCaseWithExtraKeyFine(String s) {
return md5(s + MD5_KEY).toLowerCase().replaceAll("4", "6").replaceAll("d", "f");
}
public static String md5LowerCaseFine(String s) {
return md5(s).toLowerCase().replaceAll("4", "6").replaceAll("d", "f");
}
public static String md5(String s) {
char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
try {
byte[] btInput = s.getBytes();
/* 获得MD5摘要算法的 MessageDigest 对象*/
MessageDigest mdInst = MessageDigest.getInstance("MD5");
/* 使用指定的字节更新摘要*/
mdInst.update(btInput);
/* 获得密文*/
byte[] md = mdInst.digest();
/* 把密文转换成十六进制的字符串形式*/
int j = md.length;
char str[] = new char[j * 2];
int k = 0;
for (int i = 0; i < j; i++) {
byte byte0 = md[i];
str[k++] = hexDigits[byte0 >>> 4 & 0xf];
str[k++] = hexDigits[byte0 & 0xf];
}
return new String(str);
} catch (Exception e) {
logger.info(e.getMessage(), e);
return null;
}
}
public static String encryptPasswordBASE64(String password) {
if (StringUtils.isEmpty(password)) {
return password;
}
CacheDesUtil cacheDesUtil = new CacheDesUtil(BASE64_DES_KEY);
/*密码需要加密才能和数据库的匹配上*/
String passwordEncrypt = null;
try {
passwordEncrypt = cacheDesUtil.encrypt(password);
} catch (Exception e) {
logger.info(e.getMessage(), e);
}
return passwordEncrypt;
}
public static String decryptPasswordBASE64(String password) {
if (StringUtils.isEmpty(password)) {
return password;
}
CacheDesUtil cacheDesUtil = new CacheDesUtil(BASE64_DES_KEY);
/*密码需要加密才能和数据库的匹配上*/
String passwordEncrypt = null;
try {
passwordEncrypt = cacheDesUtil.decrypt(password);
} catch (Exception e) {
logger.info(e.getMessage(), e);
}
return passwordEncrypt;
}
public static String addGeneratedKeyToString(String password) {
Map passwordMap = CacheJsonUtil.toObject(password, Map.class);
passwordMap.put("encryptKey", CachePasswordUtil.generatePasswordF(8));
return CacheJsonUtil.toJson(passwordMap);
}
public static String encryptPasswordMapMD5(String password, CacheService cacheService) {
if (StringUtils.isEmpty(password)) {
return password;
}
password = addGeneratedKeyToString(password);
String passwordEncrypt = md5LowerCaseWithExtraKey(password);
cacheService.set(CacheRedisKeyUtil.getPasswordRedisKey(passwordEncrypt), password);
cacheService.expire(CacheRedisKeyUtil.getPasswordRedisKey(passwordEncrypt), CacheKeyCommon.PASSWORDUTIL_REDIS_ENCRYPTKEY_EXPIRE_DEFAULT_TIME_IN_SECONDS);
return passwordEncrypt;
}
public static String encryptPasswordMapMD5(String password, CacheService cacheService, int expireSeconds) {
if (StringUtils.isEmpty(password)) {
return password;
}
password = addGeneratedKeyToString(password);
String passwordEncrypt = md5LowerCaseWithExtraKeyFine(password);
cacheService.set(CacheRedisKeyUtil.getPasswordRedisKey(passwordEncrypt), password);
cacheService.expire(CacheRedisKeyUtil.getPasswordRedisKey(passwordEncrypt), expireSeconds);
return passwordEncrypt;
}
public static String decryptPasswordMD5(String password, CacheService cacheService) {
if (StringUtils.isEmpty(password)) {
return password;
}
return cacheService.get(CacheRedisKeyUtil.getPasswordRedisKey(password));
}
public static String encryptPasswordBASE64(String password, CacheService cacheService) {
if (StringUtils.isEmpty(password)) {
return password;
}
String encryptKey = CachePasswordUtil.generatePasswordF(8);
CacheDesUtil cacheDesUtil = new CacheDesUtil(encryptKey);
/*密码需要加密才能和数据库的匹配上*/
String passwordEncrypt = null;
try {
passwordEncrypt = cacheDesUtil.encrypt(password);
} catch (Exception e) {
logger.info(e.getMessage(), e);
}
cacheService.set(CacheRedisKeyUtil.getPasswordRedisKey(passwordEncrypt), encryptKey);
cacheService.expire(CacheRedisKeyUtil.getPasswordRedisKey(passwordEncrypt),
CacheKeyCommon.PASSWORDUTIL_REDIS_ENCRYPTKEY_EXPIRE_DEFAULT_TIME_IN_SECONDS);
return passwordEncrypt;
}
public static String encryptPasswordBASE64(String password, CacheService cacheService, int expireSeconds) {
if (StringUtils.isEmpty(password)) {
return password;
}
String encryptKey = CachePasswordUtil.generatePasswordF(8);
CacheDesUtil cacheDesUtil = new CacheDesUtil(encryptKey);
/*密码需要加密才能和数据库的匹配上*/
String passwordEncrypt = null;
try {
passwordEncrypt = cacheDesUtil.encrypt(password);
} catch (Exception e) {
logger.info(e.getMessage(), e);
}
cacheService.set(CacheRedisKeyUtil.getPasswordRedisKey(passwordEncrypt), encryptKey);
cacheService.expire(CacheRedisKeyUtil.getPasswordRedisKey(passwordEncrypt), expireSeconds);
return passwordEncrypt;
}
public static String decryptPasswordBASE64(String password, CacheService cacheService) {
if (StringUtils.isEmpty(password)) {
return password;
}
String encryptKey = cacheService.get(CacheRedisKeyUtil.getPasswordRedisKey(password));
if (encryptKey == null) {
throw new CacheServerException(CacheServerErrorCode.PASSWORDUTIL_ENCRYPT_KEY_EXPIRED_OR_NOT_FOUND);
}
CacheDesUtil cacheDesUtil = new CacheDesUtil(encryptKey);
/*密码需要加密才能和数据库的匹配上*/
String passwordEncrypt = null;
try {
passwordEncrypt = cacheDesUtil.decrypt(password);
} catch (Exception e) {
logger.info(e.getMessage(), e);
}
return passwordEncrypt;
}
public static String sha256(String str){
MessageDigest messageDigest;
String encodeStr = "";
try {
messageDigest = MessageDigest.getInstance("SHA-256");
messageDigest.update(str.getBytes("UTF-8"));
encodeStr = byte2Hex(messageDigest.digest());
} catch (NoSuchAlgorithmException e) {
logger.info(e.getMessage(), e);
} catch (UnsupportedEncodingException e) {
logger.info(e.getMessage(), e);
}
return encodeStr;
}
public static String sha256f(String str){
return sha256(str).replaceAll("4","6").replaceAll("d","f");
}
/*** 将byte转为16进制 */
private static String byte2Hex(byte[] bytes){
StringBuffer stringBuffer = new StringBuffer();
String temp = null;
for (int i=0;i