/**
* 单向加密工具
*/
public class Encodes {
// 随机字符集合
private static String NONCE_STRING = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
// 安全的随机数生成器
private static SecureRandom random = new SecureRandom();
// 密码salt字节数组长度
private static int SALT_SIZE = 8;
// 密码散列次数
public static int PASSWORD_INTERATIONS = 1024;
/**
* md5加密
*/
public static String md5(String message) {
return DigestUtils.md5Hex(message);
}
/**
* 对字符串进行简单的SHA1加密
*/
public static String sha1Hex(String message) {
return DigestUtils.sha1Hex(message);
}
/**
* Hex编码
*/
public static String encodeHex(byte[] data) {
return Hex.encodeHexString(data);
}
/**
* Hex解码
*/
public static byte[] decodeHex(String message) {
byte[] result = null;
try {
result = Hex.decodeHex(message.toCharArray());
} catch (DecoderException e) {
e.printStackTrace();
}
return result;
}
/**
* 生成随机hash码
*/
public static String salt() {
byte[] bytes = new byte[SALT_SIZE];
random.nextBytes(bytes);
return encodeHex(bytes);
}
/**
* 随机字符串
*
* @return
*/
public static String nonceString(int length) {
int index;
StringBuilder sb = new StringBuilder(length);
for (int i = 0; i < length; i++) {
index = random.nextInt(62);
sb.append(NONCE_STRING.charAt(index));
}
return sb.toString();
}
/**
* 验证码
*
* @param digit
* 生成位数
* */
public static String verifyCode(int digit) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < digit; i++)
sb.append(random.nextInt(10));
return sb.toString();
}
/**
* sha256Hex加密
*/
public static String sha256Hex(String message) {
return DigestUtils.sha256Hex(message);
}
/**
* 对字节数组进行简单的sha256加密
*/
public static byte[] sha256(byte[] message) {
return DigestUtils.sha256(message);
}
/**
* 生成安全密码
*
* @param message
* 源数据
* @param salt
* 加盐
* @return
*/
public static String password(String password, String salt) {
MessageDigest digest = DigestUtils.getSha1Digest();
digest.update(hexDecode(salt));
byte[] result = digest.digest(password.getBytes());
for (int i = 1; i < PASSWORD_INTERATIONS; i++) {
digest.reset();
result = digest.digest(result);
}
return Hex.encodeHexString(result);
}
/**
* Hex解码
*
* @param message
* @return
*/
public static byte[] hexDecode(String message) {
byte[] result = null;
try {
result = Hex.decodeHex(message.toCharArray());
} catch (DecoderException e) {
e.printStackTrace();
}
return result;
}
public static void main(String[] args) {
System.out.println(Encodes.nonceString(32));
}
}
单向加密工具
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/River741132472/article/details/82382818
猜你喜欢
转载自blog.csdn.net/River741132472/article/details/82382818
今日推荐
周排行