Spring Security Crypto provides cryptographic utilities including password encoding, key generation, encryption, and various hashing functions
npx @tessl/cli install tessl/maven-org-springframework-security--spring-security-crypto@6.5.0Spring Security Crypto provides a comprehensive suite of cryptographic utilities for Spring Security applications, including secure password encoding algorithms, key generation utilities, encryption/decryption services, and various encoding/decoding functions. The library is designed with security best practices and provides industry-standard cryptographic implementations.
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-crypto</artifactId>
<version>6.5.1</version>
</dependency>// Password encoding
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.crypto.password.BCryptPasswordEncoder;
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
// Encryption
import org.springframework.security.crypto.encrypt.BytesEncryptor;
import org.springframework.security.crypto.encrypt.TextEncryptor;
import org.springframework.security.crypto.encrypt.Encryptors;
// Key generation
import org.springframework.security.crypto.keygen.BytesKeyGenerator;
import org.springframework.security.crypto.keygen.StringKeyGenerator;
import org.springframework.security.crypto.keygen.KeyGenerators;
// Codec utilities
import org.springframework.security.crypto.codec.Base64;
import org.springframework.security.crypto.codec.Hex;
import org.springframework.security.crypto.codec.Utf8;
import org.springframework.security.crypto.util.EncodingUtils;import org.springframework.security.crypto.factory.PasswordEncoderFactories;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.crypto.encrypt.Encryptors;
import org.springframework.security.crypto.encrypt.TextEncryptor;
import org.springframework.security.crypto.keygen.KeyGenerators;
import org.springframework.security.crypto.keygen.StringKeyGenerator;
// Password encoding
PasswordEncoder passwordEncoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();
String encodedPassword = passwordEncoder.encode("mySecretPassword");
boolean matches = passwordEncoder.matches("mySecretPassword", encodedPassword);
// Text encryption
TextEncryptor textEncryptor = Encryptors.text("myPassword", "mySalt");
String encrypted = textEncryptor.encrypt("Hello World");
String decrypted = textEncryptor.decrypt(encrypted);
// Key generation
StringKeyGenerator keyGenerator = KeyGenerators.string();
String generatedKey = keyGenerator.generateKey();Spring Security Crypto is organized around several key components:
Secure password hashing with support for multiple industry-standard algorithms including BCrypt, Argon2, SCrypt, and PBKDF2.
interface PasswordEncoder {
String encode(CharSequence rawPassword);
boolean matches(CharSequence rawPassword, String encodedPassword);
default boolean upgradeEncoding(String encodedPassword);
}Symmetric encryption services for both text and binary data using AES and RSA algorithms with various cipher modes.
interface BytesEncryptor {
byte[] encrypt(byte[] byteArray);
byte[] decrypt(byte[] encryptedByteArray);
}
interface TextEncryptor {
String encrypt(String text);
String decrypt(String encryptedText);
}Secure random key generation utilities for creating cryptographic keys and salts.
interface BytesKeyGenerator {
int getKeyLength();
byte[] generateKey();
}
interface StringKeyGenerator {
String generateKey();
}Encoding and decoding utilities for Base64, Hexadecimal, and UTF-8 conversions.
class Base64 {
static byte[] encode(byte[] src);
static byte[] decode(byte[] src);
static boolean isBase64(byte[] bytes);
}
class Hex {
static char[] encode(byte[] bytes);
static byte[] decode(CharSequence hexString);
}
class Utf8 {
static byte[] encode(CharSequence string);
static String decode(byte[] bytes);
}
class EncodingUtils {
static byte[] concatenate(byte[]... arrays);
static byte[] subArray(byte[] array, int beginIndex, int endIndex);
}