forked from smallrye/smallrye-jwt
-
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.
Add method for generating secret key (smallrye#342)
- Loading branch information
Showing
6 changed files
with
177 additions
and
30 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
27 changes: 27 additions & 0 deletions
27
implementation/common/src/main/java/io/smallrye/jwt/algorithm/SymmetricKeyAlgorithm.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,27 @@ | ||
package io.smallrye.jwt.algorithm; | ||
|
||
public enum SymmetricKeyAlgorithm { | ||
HS256("HS256", 256), | ||
HS384("HS384", 384), | ||
HS512("HS512", 512); | ||
|
||
private String algorithmName; | ||
private int keySize; | ||
|
||
private SymmetricKeyAlgorithm(String algorithmName, int keySize) { | ||
this.algorithmName = algorithmName; | ||
this.keySize = keySize; | ||
} | ||
|
||
public String getAlgorithm() { | ||
return algorithmName; | ||
} | ||
|
||
public int getKeySize() { | ||
return keySize; | ||
} | ||
|
||
public static SymmetricKeyAlgorithm fromAlgorithm(String algorithmName) { | ||
return SymmetricKeyAlgorithm.valueOf(algorithmName.replaceAll("-", "_").replaceAll("\\+", "_")); | ||
} | ||
} |
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
43 changes: 43 additions & 0 deletions
43
implementation/common/src/test/java/io/smallrye/jwt/util/KeyUtilsTest.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,43 @@ | ||
package io.smallrye.jwt.util; | ||
|
||
import io.smallrye.jwt.algorithm.KeyEncryptionAlgorithm; | ||
import io.smallrye.jwt.algorithm.SignatureAlgorithm; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.EnumSource; | ||
|
||
import javax.crypto.SecretKey; | ||
import java.security.NoSuchAlgorithmException; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
class KeyUtilsTest { | ||
|
||
@ParameterizedTest | ||
@EnumSource(mode = EnumSource.Mode.INCLUDE, names = { "PBES2_HS256_A128KW", "PBES2_HS384_A192KW", "PBES2_HS512_A256KW" }) | ||
void givenSymmetricKey_thenReturnSecretKey(KeyEncryptionAlgorithm algo) throws NoSuchAlgorithmException { | ||
SecretKey keySpec = KeyUtils.generateSecretKey(algo); | ||
assertEquals(algo.getKeySizeBits() / 8, keySpec.getEncoded().length); | ||
assertEquals("HMAC", keySpec.getAlgorithm()); | ||
} | ||
|
||
@ParameterizedTest | ||
@EnumSource(mode = EnumSource.Mode.EXCLUDE, names = { "PBES2_HS256_A128KW", "PBES2_HS384_A192KW", "PBES2_HS512_A256KW" }) | ||
void givenAsSymmetricKey_thenThrowNoSuchAlgorithmException(KeyEncryptionAlgorithm algo) { | ||
assertThrows(NoSuchAlgorithmException.class, () -> KeyUtils.generateSecretKey(algo)); | ||
} | ||
|
||
@ParameterizedTest | ||
@EnumSource(mode = EnumSource.Mode.INCLUDE, names = { "HS256", "HS384", "HS512" }) | ||
void givenSymmetricKey_thenReturnSecretKey(SignatureAlgorithm algo) throws NoSuchAlgorithmException { | ||
SecretKey keySpec = KeyUtils.generateSecretKey(algo); | ||
assertEquals(algo.getKeySizeBits() / 8, keySpec.getEncoded().length); | ||
assertEquals("HMAC", keySpec.getAlgorithm()); | ||
} | ||
|
||
@ParameterizedTest | ||
@EnumSource(mode = EnumSource.Mode.EXCLUDE, names = { "HS256", "HS384", "HS512" }) | ||
void givenAsSymmetricKey_thenThrowNoSuchAlgorithmException(SignatureAlgorithm algo) { | ||
assertThrows(NoSuchAlgorithmException.class, () -> KeyUtils.generateSecretKey(algo)); | ||
} | ||
} |