Калибровка и проверка Argon2id на стенде

This commit is contained in:
EgorAnti
2026-11-26 19:40:21 +03:00
parent 1ee35c54a7
commit 953c1986aa
11 changed files with 168 additions and 16 deletions
@@ -0,0 +1,93 @@
package com.example.mtusi_messenger.config;
import org.springframework.security.crypto.password.PasswordEncoder;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Locale;
import java.util.Set;
/**
* Small stand-specific calibration, not a general-purpose microbenchmark.
* Warmups remove provider/class-loading cost; every measured encode gets a new salt.
*/
public final class Argon2Benchmark {
private static final String PASSWORD = "benchmark-password-not-used-by-the-application";
private static final int WARMUPS = 5;
private static final int SAMPLES = 21;
private record Profile(int memoryKiB, int iterations, int parallelism) {}
private Argon2Benchmark() {}
public static void main(String[] args) {
Profile[] profiles = {
new Profile(19456, 2, 1),
new Profile(32768, 2, 1),
new Profile(19456, 3, 1)
};
System.out.println("{\"warmups\":" + WARMUPS + ",\"samples\":" + SAMPLES + ",\"profiles\":[");
for (int i = 0; i < profiles.length; i++) {
if (i > 0) {
System.out.println(",");
}
measure(profiles[i]);
}
System.out.println("\n]}");
}
private static void measure(Profile profile) {
PasswordEncoder encoder = new PasswordConfig().passwordEncoder(
profile.memoryKiB(), profile.iterations(), profile.parallelism());
for (int i = 0; i < WARMUPS; i++) {
String encoded = encoder.encode(PASSWORD);
require(encoder.matches(PASSWORD, encoded), "warmup verification failed");
}
double[] encodeMs = new double[SAMPLES];
double[] verifyMs = new double[SAMPLES];
Set<String> salts = new HashSet<>();
String prefix = "$argon2id$v=19$m=" + profile.memoryKiB()
+ ",t=" + profile.iterations() + ",p=" + profile.parallelism() + "$";
for (int i = 0; i < SAMPLES; i++) {
long started = System.nanoTime();
String encoded = encoder.encode(PASSWORD);
encodeMs[i] = elapsedMillis(started);
require(encoded.startsWith(prefix), "unexpected PHC parameters");
String[] parts = encoded.split("\\$");
require(parts.length == 6 && !parts[4].isBlank(), "PHC salt is missing");
salts.add(parts[4]);
started = System.nanoTime();
require(encoder.matches(PASSWORD, encoded), "verification failed");
verifyMs[i] = elapsedMillis(started);
}
require(salts.size() == SAMPLES, "salt was reused across measured encodes");
System.out.printf(Locale.ROOT,
"{\"memory_kib\":%d,\"iterations\":%d,\"parallelism\":%d,"
+ "\"salt_bytes\":16,\"samples_with_unique_salt\":%d,"
+ "\"encode_ms\":{\"p50\":%.3f,\"p95\":%.3f},"
+ "\"verify_ms\":{\"p50\":%.3f,\"p95\":%.3f}}",
profile.memoryKiB(), profile.iterations(), profile.parallelism(), salts.size(),
percentile(encodeMs, 0.50), percentile(encodeMs, 0.95),
percentile(verifyMs, 0.50), percentile(verifyMs, 0.95));
}
private static double elapsedMillis(long started) {
return (System.nanoTime() - started) / 1_000_000.0;
}
private static double percentile(double[] input, double percentile) {
double[] values = input.clone();
Arrays.sort(values);
int index = Math.max(0, (int) Math.ceil(percentile * values.length) - 1);
return values[index];
}
private static void require(boolean condition, String message) {
if (!condition) {
throw new IllegalStateException(message);
}
}
}
@@ -7,24 +7,24 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
class PasswordConfigTest {
@Test
void usesArgon2idWithTheDocumentedBaseline() {
var encoder = new PasswordConfig().passwordEncoder(19456, 2, 1);
void usesArgon2idWithTheMeasuredRuntimeProfile() {
var encoder = new PasswordConfig().passwordEncoder(32768, 2, 1);
String encoded = encoder.encode("correct horse battery staple");
assertTrue(encoded.startsWith("$argon2id$v=19$m=19456,t=2,p=1$"));
assertTrue(encoded.startsWith("$argon2id$v=19$m=32768,t=2,p=1$"));
assertTrue(encoder.matches("correct horse battery staple", encoded));
assertFalse(encoder.matches("wrong password", encoded));
}
@Test
void requestsUpgradeForWeakerArgon2idHash() {
var baseline = new PasswordConfig().passwordEncoder(19456, 2, 1);
var weak = new PasswordConfig().passwordEncoder(19456, 2, 1);
// A hash with one iteration is created directly to model a legacy row;
// PasswordConfig itself deliberately refuses such a weak runtime setup.
var current = new PasswordConfig().passwordEncoder(32768, 2, 1);
var same = new PasswordConfig().passwordEncoder(32768, 2, 1);
// The lab7 minimum remains readable but is upgraded to the measured
// 32 MiB runtime profile after a successful login.
var legacy = new org.springframework.security.crypto.argon2.Argon2PasswordEncoder(
16, 32, 1, 19456, 1);
assertTrue(baseline.upgradeEncoding(legacy.encode("legacy password")));
assertFalse(baseline.upgradeEncoding(weak.encode("current password")));
16, 32, 1, 19456, 2);
assertTrue(current.upgradeEncoding(legacy.encode("legacy password")));
assertFalse(current.upgradeEncoding(same.encode("current password")));
}
}