Bruce
  • Welcome
  • Essentials
    • Features
    • Getting Started
    • General Concepts
    • Roadmap
  • API
    • Cheat Sheet
    • Key Stores
    • Certificates
    • Keys
    • Digests
    • Signatures
    • Verification
    • Symmetric Ciphers
    • Asymmetric Ciphers
    • Message Authentication Codes
Powered by GitBook
On this page
  • Public Key
  • Usage Example
  • Private Key
  • Usage Example
  • Secret Key
  • Usage Example
  • Symmetric Key
  • Usage Example
  • Encoded Symmetric Key
  • Usage Example
  • Key Pair
  • Usage Example
  • Key Pair with Custom PRNG
  • Usage Example

Was this helpful?

Export as PDF
  1. API

Keys

Public Key

PublicKey publicKey(
    KeyStore keystore, 
    String alias
);

Loads a public key from a key store.

Usage Example

KeyStore keystore = keystore(
    "classpath:keystore.p12", 
    "password".toCharArray(), 
    "PKCS12"
);

PublicKey publicKey = publicKey(keystore, "alice");

Private Key

PrivateKey privateKey(
    KeyStore keystore, 
    String alias, 
    char[] password
);

Loads a public key from a key store. The password parameter is the private key's password.

Usage Example

KeyStore keystore = keystore(
    "classpath:keystore.p12", 
    "password".toCharArray(), 
    "PKCS12"
);

PrivateKey privateKey = 
    privateKey(keystore, "alice", "password".toCharArray());

Secret Key

Key secretKey(
    KeyStore keystore, 
    String alias, 
    char[] password
);

Loads a secret key from a key store. The password parameter is the secret key's password.

Usage Example

KeyStore keystore = keystore(
    "classpath:/keystore.p12", 
    "password".toCharArray(), 
    "PKCS12"
);

Key key = secretKey(keystore, "alice", "password".toCharArray());

Symmetric Key

byte[] symmetricKey(String algorithm);

Generates a symmetric key using the given algorithm.

Usage Example

byte[] key = symmetricKey("DESede");

Encoded Symmetric Key

String symmetricKey(String algorithm, Encoding encoding);

Generates a symmetric key using the given algorithm and encoding.

Usage Example

String key = symmetricKey("DESede", BASE64);

Key Pair

KeyPair keyPair(String algorithm, int keySize);

Generates a pair of keys for asymmetric cryptography.

Usage Example

KeyPair keyPair = keyPair("RSA", 4096);
Signer signer = signer(keyPair.getPrivate(), "SHA512withRSA");
Verifier verifier = verifier(keyPair.getPublic(), "SHA512withRSA");
byte[] signature = signer.sign(MESSAGE);
assertTrue(verifier.verify(MESSAGE, signature));

Key Pair with Custom PRNG

KeyPair keyPair(
    String algorithm, 
    int keySize, 
    SecureRandom random
);

Usage Example

SecureRandom random = SecureRandom.getInstanceStrong();
random.setSeed(new byte[]{0, 1, 2, 3, 4, 5});
KeyPair keyPair = keyPair("RSA", 4096, random);
Signer signer = signer(keyPair.getPrivate(), "SHA512withRSA");
Verifier verifier = verifier(keyPair.getPublic(), "SHA512withRSA");
byte[] signature = signer.sign(MESSAGE);
assertTrue(verifier.verify(MESSAGE, signature));

PreviousCertificatesNextDigests

Last updated 3 years ago

Was this helpful?

Same as but allows passing a SecureRandom instance for custom initialization of the pseudo random number generator used when generating the keys.

key pair