新需求更新 同步更新
This commit is contained in:
+27
-8
@@ -1,11 +1,13 @@
|
||||
package com.nanri.aiimage.modules.auth.util;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.bouncycastle.crypto.generators.SCrypt;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.crypto.SecretKeyFactory;
|
||||
import javax.crypto.spec.PBEKeySpec;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.SecureRandom;
|
||||
import java.util.HexFormat;
|
||||
|
||||
@@ -51,21 +53,38 @@ public class WerkzeugPasswordEncoder {
|
||||
String method = storedHash.substring(0, firstSep);
|
||||
String salt = storedHash.substring(firstSep + 1, secondSep);
|
||||
String expectedHex = storedHash.substring(secondSep + 1);
|
||||
if (!method.startsWith("pbkdf2:sha256")) {
|
||||
log.warn("[auth] unsupported password hash scheme: {}", method);
|
||||
return false;
|
||||
if (method.startsWith("scrypt:")) {
|
||||
return matchesScrypt(rawPassword, method, salt, expectedHex);
|
||||
}
|
||||
String[] parts = method.split(":");
|
||||
int iterations = parts.length >= 3 ? Integer.parseInt(parts[2]) : 600_000;
|
||||
byte[] derived = pbkdf2Sha256(rawPassword.toCharArray(), salt.getBytes(StandardCharsets.UTF_8), iterations, DK_BITS);
|
||||
String actualHex = HexFormat.of().formatHex(derived);
|
||||
return constantTimeEquals(actualHex, expectedHex);
|
||||
if (method.startsWith("pbkdf2:sha256")) {
|
||||
String[] parts = method.split(":");
|
||||
int iterations = parts.length >= 3 ? Integer.parseInt(parts[2]) : 600_000;
|
||||
byte[] derived = pbkdf2Sha256(rawPassword.toCharArray(), salt.getBytes(StandardCharsets.UTF_8), iterations, DK_BITS);
|
||||
String actualHex = HexFormat.of().formatHex(derived);
|
||||
return constantTimeEquals(actualHex, expectedHex);
|
||||
}
|
||||
log.warn("[auth] unsupported password hash scheme: {}", method);
|
||||
return false;
|
||||
} catch (Exception e) {
|
||||
log.warn("[auth] verify password failed: {}", e.getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean matchesScrypt(String rawPassword, String method, String salt, String expectedHex) {
|
||||
String[] parts = method.split(":");
|
||||
if (parts.length != 4 || expectedHex.isEmpty() || (expectedHex.length() & 1) != 0) {
|
||||
return false;
|
||||
}
|
||||
int cost = Integer.parseInt(parts[1]);
|
||||
int blockSize = Integer.parseInt(parts[2]);
|
||||
int parallelization = Integer.parseInt(parts[3]);
|
||||
byte[] expected = HexFormat.of().parseHex(expectedHex);
|
||||
byte[] actual = SCrypt.generate(rawPassword.getBytes(StandardCharsets.UTF_8),
|
||||
salt.getBytes(StandardCharsets.UTF_8), cost, blockSize, parallelization, expected.length);
|
||||
return MessageDigest.isEqual(actual, expected);
|
||||
}
|
||||
|
||||
private static byte[] pbkdf2Sha256(char[] password, byte[] salt, int iterations, int keyBits) throws Exception {
|
||||
PBEKeySpec spec = new PBEKeySpec(password, salt, iterations, keyBits);
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user