java.lang.Object
net.i2p.crypto.KeyGenerator
Comprehensive key generation facility for I2P cryptographic operations.
This class provides methods to generate various types of cryptographic keys used throughout the I2P network, including:
- Asymmetric key pairs (ElGamal, DSA, ECDSA, EdDSA, RSA)
- Symmetric session keys (AES)
- Key derivation and conversion utilities
Supports both legacy algorithms (DSA, ElGamal) and modern cryptographic schemes (EdDSA, ECDSA) with configurable parameters for security and performance optimization.
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final I2PAppContextSupplies the random used to generate keys.private static final booleanUse long ElGamal exponents unless the CPU is slow.private static final intNew rounds (1M) - OWASP compliant.private static final intLegacy rounds (1K) - for migration.private static final StringProperty asking for a 2048-bit ElGamal key.private static final intStandard exponent size.private static final intShort exponent size, considered safe for use with Oakley primes.Constructor Summary
ConstructorsMethod Summary
Modifier and TypeMethodDescriptionObject[]ElGamal only.ElGamal only.generatePKIKeys(EncType type) Supports EncTypes.Generate a private 256 bit session key.generateSessionKey(byte[] salt, byte[] passphrase) PBE the passphrase with the salt.generateSessionKey(byte[] salt, byte[] passphrase, int rounds) PBE the passphrase with the salt using specified rounds.Object[]Generate a pair of DSA keys, where index 0 is a SigningPublicKey, and index 1 is a SigningPrivateKey.DSA-SHA1 only.generateSigningKeys(SigType type) Generic signature type, supports DSA, RSA, ECDSA, EdDSA.intSize of the ElGamal exponent in bits.static KeyGeneratorThe global KeyGenerator instance.intgetPBERT()Get current rounds for new encryptions.intGet legacy rounds for migration.static PublicKeygetPublicKey(PrivateKey priv) Convert a PrivateKey to its corresponding PublicKey.static SigningPublicKeyConvert a SigningPrivateKey to a SigningPublicKey.static voidUsage: KeyGenerator [sigtype...].private static voidUsage: KeyGenerator [sigtype...].private static voidTimes signing and verifying with one type.booleanWhether to use a long (2048-bit) ElGamal exponent.
Field Details
_context
Supplies the random used to generate keys.PBE_ROUNDS
PBE_ROUNDS_LEGACY
private static final int PBE_ROUNDS_LEGACYLegacy rounds (1K) - for migration.- See Also:
PUBKEY_EXPONENT_SIZE_FULL
private static final int PUBKEY_EXPONENT_SIZE_FULLStandard exponent size.- See Also:
PUBKEY_EXPONENT_SIZE_SHORT
private static final int PUBKEY_EXPONENT_SIZE_SHORTShort exponent size, considered safe for use with Oakley primes. Per "On Diffie-Hellman Key Agreement with Short Exponents" - van Oorschot, Weiner at EuroCrypt 96, and crypto++'s benchmarks Also, "Koshiba & Kurosawa: Short Exponent Diffie-Hellman Problems" (PKC 2004, LNCS 2947, pp. 173-186) apparently supports this, according to sci.crypt discussion- See Also:
DEFAULT_USE_LONG_EXPONENT
private static final boolean DEFAULT_USE_LONG_EXPONENTUse long ElGamal exponents unless the CPU is slow.PROP_LONG_EXPONENT
Property asking for a 2048-bit ElGamal key.- See Also:
Constructor Details
KeyGenerator
Create a new KeyGenerator.- Parameters:
context- the I2P app context
Method Details
getInstance
The global KeyGenerator instance.- Returns:
- The global instance.
generateSessionKey
generateSessionKey
PBE the passphrase with the salt. Warning - SLOW.- Parameters:
salt- the random salt; only the first 16 bytes are mixed inpassphrase- the password bytes stretched into the session key- Returns:
- the session key
generateSessionKey
PBE the passphrase with the salt using specified rounds. Warning - SLOW.- Parameters:
salt- 16 bytespassphrase- the password bytes, appended whole after the 16-byte salt prefixrounds- iteration count (1000 for legacy, 1000000 for current)- Returns:
- derived key
getPBERT
public int getPBERT()Get current rounds for new encryptions.- Returns:
- 1000000
getPBERTLegacy
public int getPBERTLegacy()Get legacy rounds for migration.- Returns:
- 1000
useLongElGamalExponent
public boolean useLongElGamalExponent()Whether to use a long (2048-bit) ElGamal exponent.- Returns:
- true if using long exponent
getElGamalExponentSize
public int getElGamalExponentSize()Size of the ElGamal exponent in bits.- Returns:
- the size in bits
generatePKIKeypair
ElGamal only. Generate a pair of keys, where index 0 is a PublicKey, and index 1 is a PrivateKey.- Returns:
- pair of keys
generatePKIKeys
ElGamal only. Same asgeneratePKIKeypair()but different return type.- Returns:
- pair of keys
generatePKIKeys
getPublicKey
Convert a PrivateKey to its corresponding PublicKey. As of 0.9.38, supports EncTypes.- Parameters:
priv- PrivateKey object- Returns:
- the corresponding PublicKey object
- Throws:
IllegalArgumentException- on bad key
generateSigningKeypair
Generate a pair of DSA keys, where index 0 is a SigningPublicKey, and index 1 is a SigningPrivateKey. DSA-SHA1 only.- Returns:
- pair of keys
generateSigningKeys
DSA-SHA1 only.Same as above but different return type.
- Returns:
- pair of keys
generateSigningKeys
Generic signature type, supports DSA, RSA, ECDSA, EdDSA.- Parameters:
type- the signature type- Returns:
- the key pair
- Throws:
GeneralSecurityException- if key generation fails
getSigningPublicKey
Convert a SigningPrivateKey to a SigningPublicKey. As of 0.9.16, supports all key types.- Parameters:
priv- a SigningPrivateKey object- Returns:
- a SigningPublicKey object
- Throws:
IllegalArgumentException- on bad key or unknown type
main
Usage: KeyGenerator [sigtype...].- Parameters:
args- command line arguments
main2
Usage: KeyGenerator [sigtype...].- Parameters:
args- signature type names to generate keys for, or none for the default set
testSig
Times signing and verifying with one type.- Parameters:
type- the signature type to testruns- how many signatures to make- Throws:
GeneralSecurityException- if the type is unusable