Beispiel #1
0
  /**
   * Make a new Pico output stream, wrapping the provided stream. Use the given key to encrypt the
   * data.
   *
   * @param key The key to use to encrypt.
   * @param os The stream to get the output.
   * @throws IOException An error occurred creating the temporary file.
   */
  public PicoOutputStream(byte[] key, OutputStream os) throws IOException {
    super(os);

    if (key == null) {
      throw new NullPointerException("The key is null.");
    }
    if (os == null) {
      throw new NullPointerException("The output stream is null.");
    }
    _backing = os;

    try {
      _hash = MessageDigest.getInstance(PicoStructure.HASH);
    } catch (NoSuchAlgorithmException nsae) {
      throw new RuntimeException("Failed to create hash.", nsae);
    }

    // Construct a temporary file to get the encrypted data.
    _tmpfile = File.createTempFile("pico", "pico");
    _tmpfile.deleteOnExit();
    _encrypted = new FileOutputStream(_tmpfile);
    // Build the header.
    _head = new PicoHeader();
    _head.setKey(key);
  }
Beispiel #2
0
  /**
   * Create a new Pico file instance from the given random access file. If the file exists and it is
   * not empty then it is truncated. If it does not exist then it is created.
   *
   * @param backing The random access file.
   * @param key The key to use to encrypt the file.
   * @throws IOException The file cannot be read.
   */
  protected PicoFile(RandomAccessFile backing, byte[] key) throws IOException {
    assert backing != null : "Backing is null.";
    assert key != null : "Key is null.";
    assert key.length > 0 : "Key is missing.";
    _backing = backing;
    _open = true;
    _resetDigest();
    // We are creating a new file, so truncate any existing file and
    // generate a new header.
    _backing.setLength(0L);
    _head = new PicoHeader();
    _head.setKey(key);

    // Now the Header size is fixed since we have the key and know the size
    // of the hash
    // we will write later.

    // This actually positions us to _head.offset + 0
    position(0L);
  }