Package rs.baselib.crypto
Class KeyGen
java.lang.Object
rs.baselib.crypto.KeyGen
The class for generating key pairs.
- Author:
- ralph
-
Field Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic KeyPair
Generate a key pair using default settings (EC, 256 bit).static KeyPair
generateKeyPair
(String algorithm) Generate a key pair using default settings for the given algorithm.static KeyPair
generateKeyPair
(String algorithm, int keySize) Generate a key pair using default settings for the given algorithm.static KeyPair
generateKeyPair
(KeyPairGenerator generator) Generates a key pair using the configured generator.static KeyPair
generateKeyPairBySeed
(byte[] seed) Generates a public/private DSA key pair.static KeyPair
generateKeyPairBySeed
(String seed) Generates a public/private key pair.static KeyPair
generateKeyPairBySeed
(String seed, Charset charset) Generates a public/private key pair.static SecretKey
generateSecretKey
(int iterationCount, String passPhrase, byte[] salt) Generates a secret key (PBE) based on the given parameters.static int
getPreferredKeySize
(String algorithm) Returns preferred key size for the given algorithm.static void
Log a key pair using the SLF4JLogger
.static void
static void
Log a key pair using theSystem.out
stream.
-
Field Details
-
RSA_KEY_ALGORITHM
- See Also:
-
ECS_KEY_ALGORITHM
- See Also:
-
DEFAULT_KEY_ALGORITHM
Default key generating algorithm- See Also:
-
-
Constructor Details
-
KeyGen
public KeyGen()
-
-
Method Details
-
generateSecretKey
public static SecretKey generateSecretKey(int iterationCount, String passPhrase, byte[] salt) throws NoSuchAlgorithmException, InvalidKeySpecException Generates a secret key (PBE) based on the given parameters.- Parameters:
iterationCount
- the cumber of iterations (if less than 1 thenEncryptionUtils.DEFAULT_ITERATIONS
will be used)passPhrase
- the passphrase (required)salt
- the slat (can be null)- Returns:
- a secret key based on parameters
- Throws:
NoSuchAlgorithmException
- when the algorithm does not existInvalidKeySpecException
- when the key spec is invalid
-
generateKeyPairBySeed
public static KeyPair generateKeyPairBySeed(String seed) throws NoSuchProviderException, NoSuchAlgorithmException Generates a public/private key pair.- Parameters:
seed
- seed to be used.- Returns:
- key pair
- Throws:
NoSuchProviderException
- when the algorithm provider does not existNoSuchAlgorithmException
- when the algorithm does not exist
-
generateKeyPairBySeed
public static KeyPair generateKeyPairBySeed(String seed, Charset charset) throws NoSuchProviderException, NoSuchAlgorithmException Generates a public/private key pair.- Parameters:
seed
- seed to be used.charset
- the charset to be used for string encoding (null
fordefault charset
)- Returns:
- key pair
- Throws:
NoSuchProviderException
- when the algorithm provider does not existNoSuchAlgorithmException
- when the algorithm does not exist
-
generateKeyPairBySeed
public static KeyPair generateKeyPairBySeed(byte[] seed) throws NoSuchProviderException, NoSuchAlgorithmException Generates a public/private DSA key pair.- Parameters:
seed
- seed to be used.- Returns:
- key pair
- Throws:
NoSuchProviderException
- when the algorithm provider does not existNoSuchAlgorithmException
- when the algorithm does not exist
-
log
Log a key pair using the SLF4JLogger
.- Parameters:
keyPair
- key pair to be logged
-
print
Log a key pair using theSystem.out
stream.- Parameters:
keyPair
- key pair to be logged
-
generateKeyPair
Generate a key pair using default settings (EC, 256 bit).- Returns:
- a new key pair
- Throws:
NoSuchAlgorithmException
- when the default algorithm does not exist
-
generateKeyPair
Generate a key pair using default settings for the given algorithm.- Parameters:
algorithm
- key generation algorithm- Returns:
- a new key pair
- Throws:
NoSuchAlgorithmException
- when the default algorithm does not exist- See Also:
-
generateKeyPair
public static KeyPair generateKeyPair(String algorithm, int keySize) throws NoSuchAlgorithmException Generate a key pair using default settings for the given algorithm.- Parameters:
algorithm
- key generation algorithm- Returns:
- a new key pair
- Throws:
NoSuchAlgorithmException
- when the default algorithm does not exist- See Also:
-
getPreferredKeySize
Returns preferred key size for the given algorithm.The method knows about RSA, DSA and EC. All others will receive a 1024 bit key size
- Parameters:
algorithm
- the algorithm- Returns:
- the preferred key size
-
generateKeyPair
Generates a key pair using the configured generator.- Parameters:
generator
- the key pair generator- Returns:
- a new key pair
-
main
-