com.objectwave.crypto
Class CryptoOutputStream

java.lang.Object
  |
  +--java.io.OutputStream
        |
        +--com.objectwave.crypto.CryptoOutputStream

public class CryptoOutputStream
extends java.io.OutputStream

Encrypt bytes written to this stream into 16-byte blocks using the Square cryptography algorithm. before writing them to the underlying OutputStream.


Constructor Summary
CryptoOutputStream(java.io.OutputStream underlying, java.lang.String password)
          Construct an instance of CryptoOutputStream for writing securely encrypted data to the unserlying output stream.
 
Method Summary
 void flush()
          Flush the stream.
static void main(java.lang.String[] args)
           
 void setPassword(java.lang.String password)
          Set the password.
 void write(int ch)
          Securely write a byte to the underlying outptt stream.
protected  void write(int ch, boolean flush)
          The protected method where the data is actually written.
 
Methods inherited from class java.io.OutputStream
close, write, write
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CryptoOutputStream

public CryptoOutputStream(java.io.OutputStream underlying,
                          java.lang.String password)
Construct an instance of CryptoOutputStream for writing securely encrypted data to the unserlying output stream.

Parameters:
password - the password to use for encrypting the data.
Method Detail

flush

public void flush()
           throws java.io.IOException
Flush the stream. This should always be called when a set of bytes has been written. (and of course, close() should be called when the writer is finished with the stream: this will in turn call flush()). If there is a data block which has not been filled, then the remainder is fullied with asecurely random data, it is encrypted and stuffed out to the underlying data stream. There is a byte prefixed to the block indicating how many bytes of the block are user data. Note that up to CRYPTO_LEN useless bytes may be written as a result of this call.

Overrides:
flush in class java.io.OutputStream
Throws:
java.io.IOException - thrown if there's an error flushing the data to the underlying output stream.

main

public static void main(java.lang.String[] args)

setPassword

public void setPassword(java.lang.String password)
Set the password. After calling this with a new password value, all subsequent blocks will be decrypted using this value. Note that this method is relatively expensive, creating a SHA1 hash and initializing a new instance of the Square crypto object. This method would not normally be called: the user would typically set the password via the constructor. Changing the password after having written data means that the reader of this stream must be informed of the change somehow.

Parameters:
password - the password string to use to decrypt the encrypted data.

write

public void write(int ch)
           throws java.io.IOException
Securely write a byte to the underlying outptt stream. Bytes are copied to an internal buffer. When that buffer is filled, the buffer is encrypted and is written to the unserlying output stream. The buffer will can be forced to send immediately by calling flush(), though up to CRYPTO_LEN useless bytes may be written as a result of this call.

Specified by:
write in class java.io.OutputStream
Parameters:
ch - int the byte to write
Throws:
java.io.IOException - thrown is there's an error writing to the underlying output stream.

write

protected void write(int ch,
                     boolean flush)
              throws java.io.IOException
The protected method where the data is actually written. Either a user's byte will be written or the stream will be flushed. If writing the user's byte to the internal buffer fills it to CRYPTO_LEN bytes, then the result is the same as is flush==true: the block will be encrypted and written to the underlying output stream. If flush is true and there's at least one byte in the interna buffer, then the remainder of the block is filled with securely random data and is written to the underlying output stream. There's always a single byte prefixed to the block when it is written indicating how many bytes (left-justified) of the block are user data.

Parameters:
ch - int The byte the write (if flush==false)
flush - boolean If true and there's at least one byte in the internal buffer, then a block is guaranteed to be written.
Throws:
java.io.IOException - thrown if there's an error writing to the underlying output stream.
See Also:
flush(), write(int)