add argon2id digest

This commit is contained in:
Timi
2025-10-30 17:08:35 +08:00
parent 4a633765e8
commit 53985cd358
3 changed files with 45 additions and 1 deletions

View File

@ -6,7 +6,7 @@
<groupId>com.imyeyu.utils</groupId> <groupId>com.imyeyu.utils</groupId>
<artifactId>timi-utils</artifactId> <artifactId>timi-utils</artifactId>
<version>0.0.1</version> <version>0.0.2</version>
<properties> <properties>
<maven.compiler.source>21</maven.compiler.source> <maven.compiler.source>21</maven.compiler.source>
@ -79,6 +79,11 @@
<artifactId>timi-java</artifactId> <artifactId>timi-java</artifactId>
<version>0.0.1</version> <version>0.0.1</version>
</dependency> </dependency>
<dependency>
<groupId>de.mkammerer</groupId>
<artifactId>argon2-jvm</artifactId>
<version>2.12</version>
</dependency>
<dependency> <dependency>
<groupId>org.junit.jupiter</groupId> <groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId> <artifactId>junit-jupiter</artifactId>

View File

@ -1,5 +1,8 @@
package com.imyeyu.utils; package com.imyeyu.utils;
import de.mkammerer.argon2.Argon2;
import de.mkammerer.argon2.Argon2Factory;
import java.math.BigInteger; import java.math.BigInteger;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
@ -80,4 +83,18 @@ public class Digest {
} }
return result.toString(); return result.toString();
} }
public static String argon2id(String password) {
return argon2id(10, 65536, 1, password);
}
public static String argon2id(int iterations, int memory, int parallelism, String password) {
Argon2 argon2id = Argon2Factory.create(Argon2Factory.Argon2Types.ARGON2id);
return argon2id.hash(iterations, memory, parallelism, password.toCharArray());
}
public static boolean argon2idVerify(String passwordHash, String password) {
Argon2 argon2id = Argon2Factory.create(Argon2Factory.Argon2Types.ARGON2id);
return argon2id.verify(passwordHash, password.toCharArray());
}
} }

View File

@ -0,0 +1,22 @@
package test;
import com.imyeyu.utils.Digest;
import com.imyeyu.utils.Time;
import org.junit.jupiter.api.Test;
/**
* @author 夜雨
* @since 2025-10-30 16:55
*/
public class TestDigest {
@Test
public void testArgon2id() {
String pwd = "hello argon";
long start = Time.now();
String hash = Digest.argon2id(pwd);
System.out.println("digest time: " + (Time.now() - start) + "ms");
System.out.println(hash);
assert Digest.argon2idVerify(hash, pwd);
}
}