Package com.unboundid.util
Class PassphraseEncryptedInputStream
- java.lang.Object
-
- java.io.InputStream
-
- com.unboundid.util.PassphraseEncryptedInputStream
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable
@ThreadSafety(level=NOT_THREADSAFE) public final class PassphraseEncryptedInputStream extends java.io.InputStream
This class provides anInputStreamimplementation that can read encrypted data written by thePassphraseEncryptedOutputStream. It will use a provided password in conjunction with aPassphraseEncryptedStreamHeaderthat will either be read from the beginning of the stream or provided in the constructor.
-
-
Constructor Summary
Constructors Constructor Description PassphraseEncryptedInputStream(char[] passphrase, java.io.InputStream wrappedInputStream)Creates a new passphrase-encrypted input stream that will read thePassphraseEncryptedStreamHeaderfrom the underlying input stream.PassphraseEncryptedInputStream(java.io.InputStream wrappedInputStream, PassphraseEncryptedStreamHeader encryptionHeader)Creates a new passphrase-encrypted input stream using the provided information.PassphraseEncryptedInputStream(java.lang.String passphrase, java.io.InputStream wrappedInputStream)Creates a new passphrase-encrypted input stream that will read thePassphraseEncryptedStreamHeaderfrom the underlying input stream.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description intavailable()Retrieves an estimate of the number of decrypted byte that are available to read from the underlying stream without blocking.voidclose()Closes this input stream and the underlying stream.PassphraseEncryptedStreamHeadergetEncryptionHeader()Retrieves an encryption header with details about the encryption used when the data was originally written.voidmark(int readLimit)Marks the current position in this input stream so that the caller may return to that spot (and re-read the data) using thereset()method.booleanmarkSupported()intread()Retrieves a single byte of decrypted data read from the underlying input stream.intread(byte[] b)Reads decrypted data and writes it into the provided byte array.intread(byte[] b, int offset, int length)Reads decrypted data and writes it into the specified portion of the provided byte array.voidreset()Attempts to reset the position of this input stream to the position of the last call tomark(int).longskip(long maxBytesToSkip)Skips over and discards up to the specified number of bytes of decrypted data obtained from the underlying input stream.
-
-
-
Constructor Detail
-
PassphraseEncryptedInputStream
public PassphraseEncryptedInputStream(@NotNull java.lang.String passphrase, @NotNull java.io.InputStream wrappedInputStream) throws java.io.IOException, LDAPException, java.security.InvalidKeyException, java.security.GeneralSecurityException
Creates a new passphrase-encrypted input stream that will read thePassphraseEncryptedStreamHeaderfrom the underlying input stream.- Parameters:
passphrase- The passphrase used to generate the encryption key when the correspondingPassphraseEncryptedOutputStreamwas created.wrappedInputStream- The input stream from which the encryption header and encrypted data will be read.- Throws:
java.io.IOException- If a problem is encountered while trying to read the encryption header from the provided input stream.LDAPException- If s problem is encountered while trying to parse the encryption header read from the provided input stream.java.security.InvalidKeyException- If the MAC contained in the header does not match the expected value.java.security.GeneralSecurityException- If a problem occurs while attempting to initialize the decryption.
-
PassphraseEncryptedInputStream
public PassphraseEncryptedInputStream(@NotNull char[] passphrase, @NotNull java.io.InputStream wrappedInputStream) throws java.io.IOException, LDAPException, java.security.InvalidKeyException, java.security.GeneralSecurityException
Creates a new passphrase-encrypted input stream that will read thePassphraseEncryptedStreamHeaderfrom the underlying input stream.- Parameters:
passphrase- The passphrase used to generate the encryption key when the correspondingPassphraseEncryptedOutputStreamwas created.wrappedInputStream- The input stream from which the encryption header and encrypted data will be read.- Throws:
java.io.IOException- If a problem is encountered while trying to read the encryption header from the provided input stream.LDAPException- If s problem is encountered while trying to parse the encryption header read from the provided input stream.java.security.InvalidKeyException- If the MAC contained in the header does not match the expected value.java.security.GeneralSecurityException- If a problem occurs while attempting to initialize the decryption.
-
PassphraseEncryptedInputStream
public PassphraseEncryptedInputStream(@NotNull java.io.InputStream wrappedInputStream, @NotNull PassphraseEncryptedStreamHeader encryptionHeader) throws java.security.GeneralSecurityException
Creates a new passphrase-encrypted input stream using the provided information.- Parameters:
wrappedInputStream- The input stream from which the encrypted data will be read.encryptionHeader- The encryption header with the information needed (in conjunction with the given passphrase) to decrypt the data read from the provided input stream.- Throws:
java.security.GeneralSecurityException- If a problem occurs while attempting to initialize the decryption.
-
-
Method Detail
-
read
public int read() throws java.io.IOException
Retrieves a single byte of decrypted data read from the underlying input stream.- Specified by:
readin classjava.io.InputStream- Returns:
- A value that is between 0 and 255 representing the byte that was read, or -1 to indicate that the end of the input stream has been reached.
- Throws:
java.io.IOException- If a problem is encountered while reading or decrypting the data.
-
read
public int read(@NotNull byte[] b) throws java.io.IOException
Reads decrypted data and writes it into the provided byte array.- Overrides:
readin classjava.io.InputStream- Parameters:
b- The byte array into which the decrypted data will be placed, starting with an index of zero. It must not benullor empty.- Returns:
- The number of bytes added to the provided buffer, or -1 if the end of the input stream has been reached and there is no more data to read.
- Throws:
java.io.IOException- If a problem is encountered while reading or decrypting the data.
-
read
public int read(@NotNull byte[] b, int offset, int length) throws java.io.IOException
Reads decrypted data and writes it into the specified portion of the provided byte array.- Overrides:
readin classjava.io.InputStream- Parameters:
b- The byte array into which the decrypted data will be placed. It must not benullor empty.offset- The position in the provided array at which to begin adding the decrypted data. It must be greater than or equal to zero and less than the length of the provided array.length- The maximum number of bytes to be added to the given array. This must be greater than zero, and the sum of theoffsetandlengthmust be less than or equal to the length of the provided array.- Returns:
- The number of bytes added to the provided buffer, or -1 if the end of the input stream has been reached and there is no more data to read.
- Throws:
java.io.IOException- If a problem is encountered while reading or decrypting the data.
-
skip
public long skip(long maxBytesToSkip) throws java.io.IOException
Skips over and discards up to the specified number of bytes of decrypted data obtained from the underlying input stream.- Overrides:
skipin classjava.io.InputStream- Parameters:
maxBytesToSkip- The maximum number of bytes to skip.- Returns:
- The number of bytes that were actually skipped.
- Throws:
java.io.IOException- If a problem is encountered while skipping data from the stream.
-
available
public int available() throws java.io.IOException
Retrieves an estimate of the number of decrypted byte that are available to read from the underlying stream without blocking. Note that some implementations always return a value of zero, so a return value of zero does not necessarily mean that there is no data available to read.- Overrides:
availablein classjava.io.InputStream- Returns:
- An estimate of the number of decrypted bytes that are available to read from the underlying stream without blocking.
- Throws:
java.io.IOException- If a problem is encountered while attempting to determine the number of bytes available to read.
-
close
public void close() throws java.io.IOException
Closes this input stream and the underlying stream.- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Overrides:
closein classjava.io.InputStream- Throws:
java.io.IOException- If a problem is encountered while closing the stream.
-
markSupported
public boolean markSupported()
- Overrides:
markSupportedin classjava.io.InputStream- Returns:
trueif this input stream supports themarkandresetmethods, orfalseif not.
-
mark
public void mark(int readLimit)
Marks the current position in this input stream so that the caller may return to that spot (and re-read the data) using thereset()method. Use themarkSupported()method to determine whether this feature is supported for this input stream.- Overrides:
markin classjava.io.InputStream- Parameters:
readLimit- The maximum number of bytes expected to be read between the mark and the call to theresetmethod.
-
reset
public void reset() throws java.io.IOException
Attempts to reset the position of this input stream to the position of the last call tomark(int). Use themarkSupported()method to determine whether this feature is supported for ths input stream.- Overrides:
resetin classjava.io.InputStream- Throws:
java.io.IOException- If a problem is encountered while performing the reset (e.g., no mark has been set, if too much data has been read since setting the mark, or if themarkandresetmethods are not supported).
-
getEncryptionHeader
@NotNull public PassphraseEncryptedStreamHeader getEncryptionHeader()
Retrieves an encryption header with details about the encryption used when the data was originally written.- Returns:
- An encryption header with details about the encryption used when the data was originally written.
-
-