Class CryptoStreamFactoryJ8Template
- java.lang.Object
-
- org.apache.fulcrum.jce.crypto.extended.CryptoStreamFactoryJ8Template
-
- All Implemented Interfaces:
CryptoStreamFactory,CryptoStreamFactoryJ8
- Direct Known Subclasses:
CryptoStreamGCMImpl,CryptoStreamPBEImpl
public abstract class CryptoStreamFactoryJ8Template extends Object implements CryptoStreamFactoryJ8
Concrete factory for creating encrypting/decrypting streams.
-
-
Field Summary
Fields Modifier and Type Field Description protected Stringalgorithmthe algorithm to useprotected intcountthe count parameter for the algorithm, not used for GCMprotected static Map<CryptoParametersJ8.TYPES,CryptoStreamFactoryJ8Template>instancesthe default instancesprotected StringproviderNamethe name of the JCE providerprotected static StringPROVIDERNAMEThe JCE provider name known to work.protected byte[]saltthe salt for the algorithmprotected static intSALT_SIZE
-
Constructor Summary
Constructors Constructor Description CryptoStreamFactoryJ8Template()
-
Method Summary
Modifier and Type Method Description protected abstract byte[]createCipher(InputStream is, int mode, char[] password)Create a Cipher.protected abstract KeycreateKey(char[] password, byte[] salt)Create a PBE key.protected static byte[]generateSalt()creates salt fromSecureRandom.getInstance(String)by default was algorithm SHA1PRNG changed toSecureRandom.getInstanceStrong()and let the system decide, what PRNG to use for salt random.StringgetAlgorithm()Info about used algorithm.intgetCount()InputStreamgetInputStream(InputStream is)Creates a decrypting input stream using the default password.InputStreamgetInputStream(InputStream is, char[] password)Creates an decrypting input stream using a given password.InputStreamgetInputStream(InputStream is, String decryptionMode)not used / implemented methodsInputStreamgetInputStream(InputStream is, String decryptionMode, char[] password)Creates input stream based on the decryption mode using the given password.static CryptoStreamFactoryJ8getInstance()Factory method to get a default instance creating instance of typeCryptoParametersJ8.DEFAULT_TYPE.static CryptoStreamFactoryJ8getInstance(CryptoParametersJ8.TYPES type)Factory method to get a default instancestatic CryptoStreamFactoryJ8getInstance(CryptoParametersJ8.TYPES type, byte[] salt, int count)Factory method to get a default instanceOutputStreamgetOutputStream(InputStream is, OutputStream os, char[] password)Creates an encrypting output stream using the given password.OutputStreamgetOutputStream(OutputStream os)Creates an encrypting output stream using the default password.OutputStreamgetOutputStream(OutputStream os, char[] password)Creates an encrypting output stream using the given password.StringgetProviderName()byte[]getSalt()InputStreamgetSmartInputStream(InputStream is)Creates a smart decrypting input stream using the default password.InputStreamgetSmartInputStream(InputStream is, char[] password)Creates a smart decrypting input stream using a given password.CryptoParametersJ8.TYPESgetType()static voidresetInstances()resets the default instancesvoidsetAlgorithm(String algorithm)voidsetCount(int count)voidsetProviderName(String providerName)protected voidsetSalt(byte[] salt)voidsetType(CryptoParametersJ8.TYPES type)
-
-
-
Field Detail
-
salt
protected byte[] salt
the salt for the algorithm
-
count
protected int count
the count parameter for the algorithm, not used for GCM
-
providerName
protected String providerName
the name of the JCE provider
-
algorithm
protected String algorithm
the algorithm to use
-
PROVIDERNAME
protected static final String PROVIDERNAME
The JCE provider name known to work. If the value is set to null an appropriate provider will be used.
-
SALT_SIZE
protected static final int SALT_SIZE
- See Also:
- Constant Field Values
-
instances
protected static final Map<CryptoParametersJ8.TYPES,CryptoStreamFactoryJ8Template> instances
the default instances
-
-
Method Detail
-
getType
public CryptoParametersJ8.TYPES getType()
-
setType
public void setType(CryptoParametersJ8.TYPES type)
-
getInstance
public static CryptoStreamFactoryJ8 getInstance()
Factory method to get a default instance creating instance of typeCryptoParametersJ8.DEFAULT_TYPE.- Returns:
- an instance of the CryptoStreamFactory
-
getInstance
public static CryptoStreamFactoryJ8 getInstance(CryptoParametersJ8.TYPES type)
Factory method to get a default instance- Parameters:
type- the @seeCryptoParametersJ8.TYPESof the instance.- Returns:
- an instance of the CryptoStreamFactory
-
getInstance
public static CryptoStreamFactoryJ8 getInstance(CryptoParametersJ8.TYPES type, byte[] salt, int count)
Factory method to get a default instance- Parameters:
type- the @seeCryptoParametersJ8.TYPESof the instance.salt- provided saltcount- provided count, used only forCryptoParametersJ8.TYPES.PBE.- Returns:
- an instance of the CryptoStreamFactory
-
getSmartInputStream
public InputStream getSmartInputStream(InputStream is) throws GeneralSecurityException, IOException
Description copied from interface:CryptoStreamFactoryCreates a smart decrypting input stream using the default password. The implementation looks at the binary content to decide if it was encrypted or not thereby providing transparent access to encrypted/unencrypted files.- Specified by:
getSmartInputStreamin interfaceCryptoStreamFactory- Parameters:
is- the input stream to be wrapped- Returns:
- an decrypting input stream
- Throws:
GeneralSecurityException- creating the input stream failedIOException- creating the input stream failed- See Also:
CryptoStreamFactory.getSmartInputStream(java.io.InputStream)
-
getInputStream
public InputStream getInputStream(InputStream is, char[] password) throws GeneralSecurityException, IOException
Description copied from interface:CryptoStreamFactoryCreates an decrypting input stream using a given password.- Specified by:
getInputStreamin interfaceCryptoStreamFactory- Parameters:
is- the input stream to be wrappedpassword- the password to be used- Returns:
- an decrypting input stream
- Throws:
GeneralSecurityException- creating the input stream failedIOException- creating the input stream failed- See Also:
CryptoStreamFactory.getInputStream(java.io.InputStream,char[])
-
getOutputStream
public OutputStream getOutputStream(InputStream is, OutputStream os, char[] password) throws GeneralSecurityException, IOException
Description copied from interface:CryptoStreamFactoryJ8Creates an encrypting output stream using the given password.- Specified by:
getOutputStreamin interfaceCryptoStreamFactoryJ8- Parameters:
is- the input stream to be encodedos- the output stream to be wrappedpassword- the password to be used- Returns:
- the encrypting output stream
- Throws:
GeneralSecurityException- creating the output stream failedIOException- creating the output stream failed- See Also:
CryptoStreamFactoryJ8.getOutputStream(InputStream, OutputStream, char[])
-
resetInstances
public static void resetInstances()
resets the default instances
-
getInputStream
public InputStream getInputStream(InputStream is, String decryptionMode) throws GeneralSecurityException, IOException
not used / implemented methods- Specified by:
getInputStreamin interfaceCryptoStreamFactory- Parameters:
is- the input stream to be wrappeddecryptionMode- the decryption mode (true|false|auto)- Returns:
- an decrypting input stream
- Throws:
GeneralSecurityException- creating the input stream failedIOException- creating the input stream failed
-
getInputStream
public InputStream getInputStream(InputStream is, String decryptionMode, char[] password) throws GeneralSecurityException, IOException
Description copied from interface:CryptoStreamFactoryCreates input stream based on the decryption mode using the given password.- Specified by:
getInputStreamin interfaceCryptoStreamFactory- Parameters:
is- the input stream to be wrappeddecryptionMode- the decryption mode (true|false|auto)password- the password to be used- Returns:
- an decrypting input stream
- Throws:
GeneralSecurityException- creating the input stream failedIOException- creating the input stream failed
-
getInputStream
public InputStream getInputStream(InputStream is) throws GeneralSecurityException, IOException
Description copied from interface:CryptoStreamFactoryCreates a decrypting input stream using the default password.- Specified by:
getInputStreamin interfaceCryptoStreamFactory- Parameters:
is- the input stream to be wrapped- Returns:
- an decrypting input stream
- Throws:
GeneralSecurityException- creating the input stream failedIOException- creating the input stream failed
-
getSmartInputStream
public InputStream getSmartInputStream(InputStream is, char[] password) throws GeneralSecurityException, IOException
Description copied from interface:CryptoStreamFactoryCreates a smart decrypting input stream using a given password. The implementation looks at the binary content to decide if it was encrypted or not thereby providing transparent access to encrypted/unencrypted files.- Specified by:
getSmartInputStreamin interfaceCryptoStreamFactory- Parameters:
is- the input stream to be wrappedpassword- the password to be used- Returns:
- an decrypting input stream
- Throws:
GeneralSecurityException- creating the input stream failedIOException- creating the input stream failed
-
getOutputStream
public OutputStream getOutputStream(OutputStream os) throws GeneralSecurityException, IOException
Description copied from interface:CryptoStreamFactoryCreates an encrypting output stream using the default password.- Specified by:
getOutputStreamin interfaceCryptoStreamFactory- Parameters:
os- the output stream to be wrapped- Returns:
- an encrypting output stream
- Throws:
GeneralSecurityException- creating the output stream failedIOException- creating the output stream failed
-
getOutputStream
public OutputStream getOutputStream(OutputStream os, char[] password) throws GeneralSecurityException, IOException
Description copied from interface:CryptoStreamFactoryCreates an encrypting output stream using the given password.- Specified by:
getOutputStreamin interfaceCryptoStreamFactory- Parameters:
os- the output stream to be wrappedpassword- the password to be used- Returns:
- an encrypting output stream
- Throws:
GeneralSecurityException- creating the output stream failedIOException- creating the output stream failed
-
createKey
protected abstract Key createKey(char[] password, byte[] salt) throws GeneralSecurityException
Create a PBE key.- Parameters:
password- the password to use.salt- if provided this is used, otherweisegetSalt().- Returns:
- the key
- Throws:
GeneralSecurityException- creating the key failed
-
createCipher
protected abstract byte[] createCipher(InputStream is, int mode, char[] password) throws GeneralSecurityException, IOException
Create a Cipher.- Parameters:
is- the input streammode- the cipher modepassword- the password- Returns:
- an instance of a cipher
- Throws:
GeneralSecurityException- creating a cipher failedIOException- creating a cipher failed
-
generateSalt
protected static byte[] generateSalt() throws GeneralSecurityExceptioncreates salt fromSecureRandom.getInstance(String)by default was algorithm SHA1PRNG changed toSecureRandom.getInstanceStrong()and let the system decide, what PRNG to use for salt random. salt size is by default @link 16.- Returns:
- the generated salt as byte array
- Throws:
GeneralSecurityException- if no algo could be found.
-
getSalt
public byte[] getSalt()
-
setSalt
protected void setSalt(byte[] salt)
-
getCount
public int getCount()
-
setCount
public void setCount(int count)
-
getProviderName
public String getProviderName()
-
setProviderName
public void setProviderName(String providerName)
-
getAlgorithm
public String getAlgorithm()
Description copied from interface:CryptoStreamFactoryInfo about used algorithm.- Specified by:
getAlgorithmin interfaceCryptoStreamFactory- Returns:
- algorithm string
-
setAlgorithm
public void setAlgorithm(String algorithm)
-
-