-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Client secrets now are protected with Argon2id. (#124)
- Loading branch information
1 parent
b3958ba
commit 76b269f
Showing
5 changed files
with
138 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
src/main/java/it/pagopa/swclient/mil/auth/util/SecretVerifier.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* PasswordVerifier.java | ||
* | ||
* 20 mar 2023 | ||
*/ | ||
package it.pagopa.swclient.mil.auth.util; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.util.Arrays; | ||
import java.util.Base64; | ||
|
||
import org.bouncycastle.crypto.generators.Argon2BytesGenerator; | ||
import org.bouncycastle.crypto.params.Argon2Parameters; | ||
|
||
/** | ||
* | ||
* @author Antonio Tarricone | ||
*/ | ||
public class SecretVerifier { | ||
/* | ||
* The following parameters are suggested by OWASP. | ||
* https://cheatsheetseries.owasp.org/cheatsheets/Password_Storage_Cheat_Sheet.html#argon2id | ||
*/ | ||
private static final int ITERATIONS = 2; | ||
private static final int MEM_LIMIT = 19 * 1024; // 19MB | ||
private static final int PARALLELISM = 1; | ||
|
||
/** | ||
* | ||
*/ | ||
private SecretVerifier() { | ||
} | ||
|
||
/** | ||
* @param password | ||
* @param salt | ||
* @param hash | ||
* @return | ||
* @throws NoSuchAlgorithmException | ||
*/ | ||
public static boolean verify(String password, String salt, String hash) { | ||
byte[] passwordBytes = password.getBytes(StandardCharsets.UTF_8); | ||
byte[] saltBytes = Base64.getDecoder().decode(salt); | ||
byte[] hashBytes = Base64.getDecoder().decode(hash); | ||
|
||
Argon2Parameters.Builder builder = new Argon2Parameters.Builder(Argon2Parameters.ARGON2_id) | ||
.withVersion(Argon2Parameters.ARGON2_VERSION_13) | ||
.withIterations(ITERATIONS) | ||
.withMemoryAsKB(MEM_LIMIT) | ||
.withParallelism(PARALLELISM) | ||
.withSalt(saltBytes); | ||
|
||
Argon2BytesGenerator verifier = new Argon2BytesGenerator(); | ||
verifier.init(builder.build()); | ||
|
||
byte[] testHash = new byte[hashBytes.length]; | ||
verifier.generateBytes(passwordBytes, testHash, 0, hashBytes.length); | ||
|
||
return Arrays.equals(hashBytes, testHash); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters