CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-springframework-security--spring-security-crypto

Spring Security Crypto provides cryptographic utilities including password encoding, key generation, encryption, and various hashing functions

Pending
Overview
Eval results
Files

key-generation.mddocs/

Key Generation

Secure random key generation utilities for creating cryptographic keys and salts using various algorithms and encoding schemes.

Capabilities

BytesKeyGenerator Interface

Generator for unique byte array-based keys.

/**
 * A generator for unique byte array-based keys
 */
interface BytesKeyGenerator {
    /**
     * Get the key length in bytes
     * @return the key length
     */
    int getKeyLength();
    
    /**
     * Generate a new key
     * @return the generated key bytes
     */
    byte[] generateKey();
}

StringKeyGenerator Interface

Generator for unique string keys.

/**
 * A generator for unique string keys
 */
interface StringKeyGenerator {
    /**
     * Generate a new key string
     * @return the generated key string
     */
    String generateKey();
}

KeyGenerators Factory

Factory for commonly used key generator implementations.

/**
 * Factory for commonly used key generators
 */
class KeyGenerators {
    /**
     * Creates a secure random bytes key generator with default length (8 bytes)
     * @return the bytes key generator
     */
    static BytesKeyGenerator secureRandom();
    
    /**
     * Creates a secure random bytes key generator with custom length
     * @param keyLength the key length in bytes
     * @return the bytes key generator
     */
    static BytesKeyGenerator secureRandom(int keyLength);
    
    /**
     * Creates a shared key generator that always returns the same key
     * @param keyLength the key length in bytes
     * @return the shared key generator
     */
    static BytesKeyGenerator shared(int keyLength);
    
    /**
     * Creates a hex-encoded string key generator
     * @return the string key generator
     */
    static StringKeyGenerator string();
}

Usage Example:

import org.springframework.security.crypto.keygen.KeyGenerators;
import org.springframework.security.crypto.keygen.BytesKeyGenerator;
import org.springframework.security.crypto.keygen.StringKeyGenerator;

// Generate secure random bytes
BytesKeyGenerator bytesGenerator = KeyGenerators.secureRandom();
byte[] key = bytesGenerator.generateKey(); // 8 bytes by default

// Generate secure random bytes with custom length
BytesKeyGenerator customBytesGenerator = KeyGenerators.secureRandom(16);
byte[] customKey = customBytesGenerator.generateKey(); // 16 bytes

// Generate hex-encoded string keys
StringKeyGenerator stringGenerator = KeyGenerators.string();
String stringKey = stringGenerator.generateKey(); // hex-encoded string

// Shared key generator (for testing or specific use cases)
BytesKeyGenerator sharedGenerator = KeyGenerators.shared(12);
byte[] sharedKey1 = sharedGenerator.generateKey();
byte[] sharedKey2 = sharedGenerator.generateKey(); // same as sharedKey1

Secure Random Bytes Key Generator

SecureRandom-based key generator for generating cryptographically secure random bytes.

/**
 * A BytesKeyGenerator that uses SecureRandom to generate keys
 */
class SecureRandomBytesKeyGenerator implements BytesKeyGenerator {
    /**
     * Creates a secure random bytes key generator with default length (8 bytes)
     */
    SecureRandomBytesKeyGenerator();
    
    /**
     * Creates a secure random bytes key generator with custom length  
     * @param keyLength the key length in bytes
     */
    SecureRandomBytesKeyGenerator(int keyLength);
    
    /**
     * Creates a secure random bytes key generator with custom SecureRandom
     * @param keyLength the key length in bytes
     * @param random the SecureRandom instance to use
     */
    SecureRandomBytesKeyGenerator(int keyLength, SecureRandom random);
}

Shared Key Generator

Key generator that always returns the same key value.

/**
 * A BytesKeyGenerator that always returns the same key
 */
class SharedKeyGenerator implements BytesKeyGenerator {
    /**
     * Creates a shared key generator
     * @param keyLength the key length in bytes
     */
    SharedKeyGenerator(int keyLength);
    
    /**
     * Creates a shared key generator with custom SecureRandom for initial key generation
     * @param keyLength the key length in bytes
     * @param random the SecureRandom instance to use for generating the shared key
     */
    SharedKeyGenerator(int keyLength, SecureRandom random);
}

Base64 String Key Generator

String key generator that produces Base64-encoded keys.

/**
 * A StringKeyGenerator that generates Base64-encoded string keys
 */
class Base64StringKeyGenerator implements StringKeyGenerator {
    /**
     * Creates a Base64 string key generator with default length (8 bytes before encoding)
     */
    Base64StringKeyGenerator();
    
    /**
     * Creates a Base64 string key generator with custom byte length
     * @param keyLength the key length in bytes before Base64 encoding
     */
    Base64StringKeyGenerator(int keyLength);
    
    /**
     * Creates a Base64 string key generator with custom BytesKeyGenerator
     * @param keyGenerator the underlying bytes key generator
     */
    Base64StringKeyGenerator(BytesKeyGenerator keyGenerator);
}

Hex Encoding String Key Generator

String key generator that produces hexadecimal-encoded keys.

/**
 * A StringKeyGenerator that generates hex-encoded string keys
 */
class HexEncodingStringKeyGenerator implements StringKeyGenerator {
    /**
     * Creates a hex encoding string key generator with default length (8 bytes before encoding)
     */
    HexEncodingStringKeyGenerator();
    
    /**
     * Creates a hex encoding string key generator with custom byte length
     * @param keyLength the key length in bytes before hex encoding
     */
    HexEncodingStringKeyGenerator(int keyLength);
    
    /**
     * Creates a hex encoding string key generator with custom BytesKeyGenerator
     * @param keyGenerator the underlying bytes key generator
     */
    HexEncodingStringKeyGenerator(BytesKeyGenerator keyGenerator);
}

Advanced Usage Examples:

import org.springframework.security.crypto.keygen.*;
import java.security.SecureRandom;

// Custom SecureRandom with specific algorithm
SecureRandom customRandom = SecureRandom.getInstance("SHA1PRNG");
SecureRandomBytesKeyGenerator customGenerator = 
    new SecureRandomBytesKeyGenerator(32, customRandom);
byte[] secureKey = customGenerator.generateKey();

// Base64 string keys for URLs or tokens
Base64StringKeyGenerator base64Generator = new Base64StringKeyGenerator(24);
String base64Key = base64Generator.generateKey();

// Hex string keys for display or logging
HexEncodingStringKeyGenerator hexGenerator = new HexEncodingStringKeyGenerator(16);
String hexKey = hexGenerator.generateKey();

// Combining generators
BytesKeyGenerator bytesGen = KeyGenerators.secureRandom(20);
HexEncodingStringKeyGenerator combinedGen = new HexEncodingStringKeyGenerator(bytesGen);
String combinedKey = combinedGen.generateKey();

// Shared keys for testing scenarios
SharedKeyGenerator testGenerator = new SharedKeyGenerator(16);
byte[] testKey1 = testGenerator.generateKey();
byte[] testKey2 = testGenerator.generateKey();
// testKey1 and testKey2 are identical

Security Considerations

When using key generators, consider the following:

  • Key Length: Use appropriate key lengths for your security requirements (minimum 128 bits for AES)
  • SecureRandom: The default SecureRandom implementation is cryptographically secure
  • Shared Keys: Only use SharedKeyGenerator for testing or specific architectural requirements
  • Key Storage: Generated keys should be stored securely and never logged or exposed
  • Key Rotation: Implement proper key rotation policies for long-lived applications

Install with Tessl CLI

npx tessl i tessl/maven-org-springframework-security--spring-security-crypto

docs

codecs.md

encryption.md

index.md

key-generation.md

password-encoding.md

tile.json