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 String
algorithm
the algorithm to useprotected int
count
the count parameter for the algorithm, not used for GCMprotected static Map<CryptoParametersJ8.TYPES,CryptoStreamFactoryJ8Template>
instances
the default instancesprotected String
providerName
the name of the JCE providerprotected static String
PROVIDERNAME
The JCE provider name known to work.protected byte[]
salt
the salt for the algorithmprotected static int
SALT_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 Key
createKey(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.String
getAlgorithm()
Info about used algorithm.int
getCount()
InputStream
getInputStream(InputStream is)
Creates a decrypting input stream using the default password.InputStream
getInputStream(InputStream is, char[] password)
Creates an decrypting input stream using a given password.InputStream
getInputStream(InputStream is, String decryptionMode)
not used / implemented methodsInputStream
getInputStream(InputStream is, String decryptionMode, char[] password)
Creates input stream based on the decryption mode using the given password.static CryptoStreamFactoryJ8
getInstance()
Factory method to get a default instance creating instance of typeCryptoParametersJ8.DEFAULT_TYPE
.static CryptoStreamFactoryJ8
getInstance(CryptoParametersJ8.TYPES type)
Factory method to get a default instancestatic CryptoStreamFactoryJ8
getInstance(CryptoParametersJ8.TYPES type, byte[] salt, int count)
Factory method to get a default instanceOutputStream
getOutputStream(InputStream is, OutputStream os, char[] password)
Creates an encrypting output stream using the given password.OutputStream
getOutputStream(OutputStream os)
Creates an encrypting output stream using the default password.OutputStream
getOutputStream(OutputStream os, char[] password)
Creates an encrypting output stream using the given password.String
getProviderName()
byte[]
getSalt()
InputStream
getSmartInputStream(InputStream is)
Creates a smart decrypting input stream using the default password.InputStream
getSmartInputStream(InputStream is, char[] password)
Creates a smart decrypting input stream using a given password.CryptoParametersJ8.TYPES
getType()
static void
resetInstances()
resets the default instancesvoid
setAlgorithm(String algorithm)
void
setCount(int count)
void
setProviderName(String providerName)
protected void
setSalt(byte[] salt)
void
setType(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.TYPES
of 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.TYPES
of 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:CryptoStreamFactory
Creates 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:
getSmartInputStream
in 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:CryptoStreamFactory
Creates an decrypting input stream using a given password.- Specified by:
getInputStream
in 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:CryptoStreamFactoryJ8
Creates an encrypting output stream using the given password.- Specified by:
getOutputStream
in 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:
getInputStream
in 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:CryptoStreamFactory
Creates input stream based on the decryption mode using the given password.- Specified by:
getInputStream
in 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:CryptoStreamFactory
Creates a decrypting input stream using the default password.- Specified by:
getInputStream
in 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:CryptoStreamFactory
Creates 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:
getSmartInputStream
in 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:CryptoStreamFactory
Creates an encrypting output stream using the default password.- Specified by:
getOutputStream
in 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:CryptoStreamFactory
Creates an encrypting output stream using the given password.- Specified by:
getOutputStream
in 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 GeneralSecurityException
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. 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:CryptoStreamFactory
Info about used algorithm.- Specified by:
getAlgorithm
in interfaceCryptoStreamFactory
- Returns:
- algorithm string
-
setAlgorithm
public void setAlgorithm(String algorithm)
-
-