MCPcopy Create free account
hub / github.com/SeanDragon/protools / ToolPBE2

Class ToolPBE2

security/src/main/java/pro/tools/security/ToolPBE2.java:17–129  ·  view source on GitHub ↗

密码加密类 Create At 2017/03/14 @author SeanDragon

Source from the content-addressed store, hash-verified

15 * @author SeanDragon
16 */
17public final class ToolPBE2 {
18 private ToolPBE2() {
19 throw new UnsupportedOperationException("我是工具类,别初始化我。。。");
20 }
21
22 private static final String PBKDF2_ALGORITHM = "PBKDF2WithHmacSHA1";
23
24 /**
25 * 盐的长度
26 */
27 private static final int SALT_BYTE_SIZE = 32 / 2;
28
29 /**
30 * 生成密文的长度
31 */
32 private static final int HASH_BIT_SIZE = 128 * 4;
33
34 /**
35 * 迭代次数
36 */
37 private static final int PBKDF2_ITERATIONS = 1000;
38
39 /**
40 * 对输入的密码进行验证
41 *
42 * @param attemptedPassword
43 * 待验证的密码
44 * @param encryptedPassword
45 * 密文
46 * @param salt
47 * 盐值
48 *
49 * @return 是否验证成功
50 *
51 * @throws NoSuchAlgorithmException
52 * @throws InvalidKeySpecException
53 */
54 public static boolean authenticate(String attemptedPassword, String encryptedPassword, String salt) throws InvalidKeySpecException, NoSuchAlgorithmException {
55 // 用相同的盐值对用户输入的密码进行加密
56 String encryptedAttemptedPassword = getEncryptedPassword(attemptedPassword, salt);
57 // 把加密后的密文和原密文进行比较,相同则验证成功,否则失败
58 return encryptedAttemptedPassword.equals(encryptedPassword);
59 }
60
61 /**
62 * 生成密文
63 *
64 * @param password
65 * 明文密码
66 * @param salt
67 * 盐值
68 *
69 * @return
70 *
71 * @throws NoSuchAlgorithmException
72 * @throws InvalidKeySpecException
73 */
74 public static String getEncryptedPassword(String password, String salt) throws NoSuchAlgorithmException,

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected