|
|
@@ -1,56 +1,18 @@
|
|
|
package com.sckw.core.utils;
|
|
|
|
|
|
-import com.alibaba.fastjson2.JSON;
|
|
|
-
|
|
|
import java.security.MessageDigest;
|
|
|
-import java.util.UUID;
|
|
|
|
|
|
/**
|
|
|
* 密码工具类
|
|
|
- *
|
|
|
* @author Louis
|
|
|
* @date Sep 1, 2018
|
|
|
*/
|
|
|
public class PasswordUtils {
|
|
|
public static final int HASH_INTERATIONS = 1024;
|
|
|
public static final int SALT_SIZE = 8;
|
|
|
- public static final int SUB_LENGTH = 16;
|
|
|
-
|
|
|
- /**
|
|
|
- * 匹配密码
|
|
|
- *
|
|
|
- * @param salt 盐
|
|
|
- * @param rawPass 明文
|
|
|
- * @param encPass 密文
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static boolean matches(String salt, String rawPass, String encPass) {
|
|
|
- return new EncryptionUtil(salt).matches(encPass, rawPass);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 明文密码加密
|
|
|
- *
|
|
|
- * @param rawPass 明文
|
|
|
- * @param salt
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static String encode(String rawPass, String salt) {
|
|
|
- return new EncryptionUtil(salt).encode(rawPass);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取加密盐
|
|
|
- *
|
|
|
- * @return
|
|
|
- */
|
|
|
- public static String getSalt() {
|
|
|
- return UUID.randomUUID().toString().replaceAll("-", "").substring(0, 20);
|
|
|
- }
|
|
|
|
|
|
/**
|
|
|
* MD5加密
|
|
|
- *
|
|
|
* @param inStr 明文
|
|
|
* @return 32位密文
|
|
|
*/
|
|
|
@@ -78,9 +40,8 @@ public class PasswordUtils {
|
|
|
|
|
|
/**
|
|
|
* md5密码校验
|
|
|
- *
|
|
|
- * @param rawPass
|
|
|
- * @param encPass
|
|
|
+ * @param rawPass 明文密码
|
|
|
+ * @param encPass 密文密码
|
|
|
* @return
|
|
|
*/
|
|
|
public static boolean matchesMD5(String rawPass, String encPass) {
|
|
|
@@ -92,49 +53,50 @@ public class PasswordUtils {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 生成安全的密码,生成随机的16位salt并经过1024次 sha-1 hash
|
|
|
+ * 截取密文密码生成盐
|
|
|
*/
|
|
|
- public static String entryptPassword(String plainPassword) {
|
|
|
+ public static String generateSalt() {
|
|
|
byte[] salt = Digests.generateSalt(SALT_SIZE);
|
|
|
- byte[] hashPassword = Digests.sha1(plainPassword.getBytes(), salt, HASH_INTERATIONS);
|
|
|
- return Encodes.encodeHex(salt) + Encodes.encodeHex(hashPassword);
|
|
|
+ return Encodes.encodeHex(salt);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 截取密文密码生成盐
|
|
|
+ * 生成安全的密码,生成随机的16位salt并经过1024次 sha-1 hash
|
|
|
+ * @param plainPassword 明文密码
|
|
|
+ * @param salt 盐
|
|
|
+ * @return 验证成功返回true
|
|
|
*/
|
|
|
- public static String getSaltSubPwd(String password) {
|
|
|
- return password.substring(0, SUB_LENGTH);
|
|
|
+ public static String entryptPassword(String plainPassword, String salt) {
|
|
|
+ byte[] saltByte = salt.getBytes();
|
|
|
+ byte[] hashPassword = Digests.sha1(plainPassword.getBytes(), saltByte, HASH_INTERATIONS);
|
|
|
+ return Encodes.encodeHex(saltByte) + Encodes.encodeHex(hashPassword);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 验证密码
|
|
|
- *
|
|
|
* @param plainPassword 明文密码
|
|
|
* @param password 密文密码
|
|
|
+ * @param salt 盐
|
|
|
* @return 验证成功返回true
|
|
|
*/
|
|
|
- public static boolean validatePassword(String plainPassword, String password) {
|
|
|
- byte[] salt = Encodes.decodeHex(password.substring(0, SUB_LENGTH));
|
|
|
- byte[] hashPassword = Digests.sha1(plainPassword.getBytes(), salt, HASH_INTERATIONS);
|
|
|
- return password.equals(Encodes.encodeHex(salt) + Encodes.encodeHex(hashPassword));
|
|
|
+ public static boolean validatePassword(String plainPassword, String password, String salt) {
|
|
|
+ byte[] saltByte = salt.getBytes();
|
|
|
+ byte[] hashPassword = Digests.sha1(plainPassword.getBytes(), saltByte, HASH_INTERATIONS);
|
|
|
+ return password.equals(Encodes.encodeHex(saltByte) + Encodes.encodeHex(hashPassword));
|
|
|
}
|
|
|
|
|
|
- public static void main(String[] args) {
|
|
|
+ public static void main(String[] args) throws Exception {
|
|
|
+ String account = "admin";
|
|
|
+ String password = "123456";
|
|
|
+ String salt = generateSalt();
|
|
|
+ System.out.println(salt);
|
|
|
|
|
|
- String password = PasswordUtils.entryptPassword(PasswordUtils.md5("18581845668"));
|
|
|
- String md5 = PasswordUtils.md5("123456");
|
|
|
- System.out.println(password);
|
|
|
- System.out.println(md5);
|
|
|
- System.out.println(validatePassword(md5, password));
|
|
|
-// System.out.println(PasswordUtils.md5("czh"));
|
|
|
-// System.out.println(PasswordUtils.entryptPassword(PasswordUtils.md5("czh")));
|
|
|
- System.out.println(validatePassword(md5, "86e07d48c04c8a4bd9fe9dc819c608c43efda576b215995e9f138809"));
|
|
|
+ String md5 = PasswordUtils.md5(password);
|
|
|
+ String password1 = PasswordUtils.entryptPassword(account + md5, salt);
|
|
|
+ System.out.println(password1);
|
|
|
|
|
|
+ boolean bool = PasswordUtils.validatePassword(account + md5, password1, salt);
|
|
|
+ System.out.println(bool);
|
|
|
|
|
|
- byte[] salt = Digests.generateSalt(SALT_SIZE);
|
|
|
- byte[] hashPassword = Digests.sha1(md5.getBytes(), salt, HASH_INTERATIONS);
|
|
|
- System.out.println(Encodes.encodeHex(salt));
|
|
|
- System.out.println(Encodes.encodeHex(hashPassword));
|
|
|
}
|
|
|
}
|