Overview
Thejava.security package is the core of the Java security architecture, providing classes and interfaces for security-related functionality including:
- Message Digests: Cryptographic hash functions (SHA-256, SHA-384, etc.)
- Digital Signatures: Creating and verifying digital signatures
- Key Management: Key generation, storage, and retrieval
- Secure Random: Cryptographically strong random number generation
- Access Control: Security permissions and policy enforcement
- Provider Architecture: Pluggable cryptographic service providers
Core Classes
MessageDigest
Provides applications with message digest algorithms (secure one-way hash functions).Returns a MessageDigest object that implements the specified algorithm.Parameters:
algorithm- the name of the algorithm (e.g., “SHA-256”, “SHA-384”, “SHA-1”)
NoSuchAlgorithmException- if no provider supports the algorithm
Updates the digest using the specified byte.
Updates the digest using the specified array of bytes.
Completes the hash computation and returns the resulting hash value. The digest is reset after this call.
Performs a final update on the digest using the specified array, then completes the digest computation.
Every Java platform implementation is required to support: SHA-1, SHA-256, and SHA-384
Signature
Provides the functionality of a digital signature algorithm for authentication and integrity assurance.Returns a Signature object that implements the specified signature algorithm.Standard algorithms include:
SHA1withDSASHA256withDSASHA256withRSASHA384withRSASHA256withECDSASHA384withECDSA
Initializes this signature object for signing.Throws:
InvalidKeyException- if the key is invalid
Initializes this signature object for verification.Throws:
InvalidKeyException- if the key is invalid
Updates the data to be signed or verified.
Returns the signature bytes of all the data updated. Resets the signature object to its initialized state.Throws:
SignatureException- if object is not initialized for signing
Verifies the passed-in signature. Returns true if verified, false otherwise.Throws:
SignatureException- if object is not initialized for verification
KeyStore
Represents a storage facility for cryptographic keys and certificates.Returns a KeyStore object of the specified type.Standard type:
PKCS12 (required on all Java platforms)Throws:KeyStoreException- if no provider supports the specified type
Loads this KeyStore from the given input stream. Pass null for stream to create an empty keystore.Throws:
IOException- if there is an I/O or format problemNoSuchAlgorithmException- if algorithm for checking integrity cannot be foundCertificateException- if certificates cannot be loaded
Assigns the given key to the given alias, protecting it with the given password.Throws:
KeyStoreException- if keystore has not been initialized or the operation fails
Returns the key associated with the given alias, using the given password to recover it.Returns: The requested key, or null if the alias does not exist or does not identify a key-related entry.
SecureRandom
Provides a cryptographically strong random number generator (RNG).Constructs a secure random number generator (RNG) implementing the default random number algorithm.
Returns a SecureRandom object that implements the specified RNG algorithm.Throws:
NoSuchAlgorithmException- if no provider supports the specified algorithm
Generates a user-specified number of random bytes.
Returns the given number of seed bytes, computed using the seed generation algorithm.Throws:
IllegalArgumentException- if numBytes is negative
Returns a SecureRandom object that was selected using the algorithms/providers specified in the securerandom.strongAlgorithms Security property.Throws:
NoSuchAlgorithmException- if no algorithm is available
Key Interfaces
Key
The top-level interface for all cryptographic keys.Returns the standard algorithm name for this key (e.g., “RSA”, “AES”, “DSA”).
Returns the name of the primary encoding format of this key (e.g., “X.509”, “PKCS#8”).
Returns the key in its primary encoding format, or null if the key does not support encoding.
PublicKey
A public key interface (extends Key). This interface contains no methods or constants.PrivateKey
A private key interface (extends Key). This interface contains no methods or constants.Provider Architecture
The Java Security API uses a provider-based architecture:- Default Provider
- Specific Provider
- Provider Instance
Thread Safety
Most classes in java.security are NOT thread-safe unless explicitly stated:SecureRandom- Thread-safeMessageDigest- Not thread-safe (but can be cloned)Signature- Not thread-safeKeyStore- Not thread-safe
See Also
- Cryptography APIs - javax.crypto package for encryption/decryption
- JGSS API - Java Generic Security Services for authentication