This commit is contained in:
Paul Cook 2017-07-26 15:56:37 +01:00
parent f1e1eecc25
commit e8ba6192f3
2 changed files with 38 additions and 1 deletions

View File

@ -22,6 +22,8 @@ import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec; import javax.crypto.spec.SecretKeySpec;
import java.security.Key; import java.security.Key;
import java.security.SecureRandom; import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Collection;
public abstract class MacProvider extends SignatureProvider { public abstract class MacProvider extends SignatureProvider {
@ -44,6 +46,20 @@ public abstract class MacProvider extends SignatureProvider {
return generateKey(SignatureAlgorithm.HS512); return generateKey(SignatureAlgorithm.HS512);
} }
/**
* Generates a collection of new secure-random 512 bit secret key suitable for creating and verifying HMAC signatures. This is a
* convenience method that immediately delegates to {@link #generateKey(SignatureAlgorithm)} using {@link
* SignatureAlgorithm#HS512} as the method argument.
*
* @return a new secure-random 512 bit secret key suitable for creating and verifying HMAC signatures.
* @see #generateKey(SignatureAlgorithm)
* @see #generateKey(SignatureAlgorithm, SecureRandom)
* @since 0.5
*/
public static Collection<SecretKey> generateKeys(int howMany) {
return generateKeys(SignatureAlgorithm.HS512, howMany);
}
/** /**
* Generates a new secure-random secret key of a length suitable for creating and verifying HMAC signatures * Generates a new secure-random secret key of a length suitable for creating and verifying HMAC signatures
* according to the specified {@code SignatureAlgorithm} using JJWT's default {@link * according to the specified {@code SignatureAlgorithm} using JJWT's default {@link
@ -62,6 +78,27 @@ public abstract class MacProvider extends SignatureProvider {
return generateKey(alg, SignatureProvider.DEFAULT_SECURE_RANDOM); return generateKey(alg, SignatureProvider.DEFAULT_SECURE_RANDOM);
} }
/**
* Generates a new secure-random secret key of a length suitable for creating and verifying HMAC signatures
* according to the specified {@code SignatureAlgorithm} using JJWT's default {@link
* SignatureProvider#DEFAULT_SECURE_RANDOM SecureRandom instance}. This is a convenience method that immediately
* delegates to {@link #generateKey(SignatureAlgorithm, SecureRandom)}.
*
* @param alg the desired signature algorithm
* @return a new secure-random secret key of a length suitable for creating and verifying HMAC signatures according
* to the specified {@code SignatureAlgorithm} using JJWT's default {@link SignatureProvider#DEFAULT_SECURE_RANDOM
* SecureRandom instance}.
* @see #generateKey()
* @see #generateKey(SignatureAlgorithm, SecureRandom)
* @since 0.5
*/
public static Collection<SecretKey> generateKeys(SignatureAlgorithm alg, int howMany) {
Collection<SecretKey> keys = new ArrayList<SecretKey>();
for (int i=0;i<howMany;i++)
keys.add(generateKey(alg, SignatureProvider.DEFAULT_SECURE_RANDOM));
return keys;
}
/** /**
* Generates a new secure-random secret key of a length suitable for creating and verifying HMAC signatures * Generates a new secure-random secret key of a length suitable for creating and verifying HMAC signatures
* according to the specified {@code SignatureAlgorithm} using the specified SecureRandom number generator. This * according to the specified {@code SignatureAlgorithm} using the specified SecureRandom number generator. This

View File

@ -24,7 +24,7 @@ class DefaultSignatureValidatorFactoryTest {
@Test @Test
void testNoneAlgorithm() { void testNoneAlgorithm() {
try { try {
new DefaultSignatureValidatorFactory().createSignatureValidator(SignatureAlgorithm.NONE, MacProvider.generateKey()) new DefaultSignatureValidatorFactory().createSignatureValidator(SignatureAlgorithm.NONE, MacProvider.generateKeys(1))
fail() fail()
} catch (IllegalArgumentException iae) { } catch (IllegalArgumentException iae) {
assertEquals iae.message, "The 'NONE' algorithm cannot be used for signing." assertEquals iae.message, "The 'NONE' algorithm cannot be used for signing."