Corrected program

package com.elharo.security;

import java.io.*;
import java.security.*;

public class EasyDigestOutputStream extends FilterOutputStream {

  private boolean on = true;
  protected byte[] result = null;
  protected MessageDigest digest;

  public EasyDigestOutputStream(OutputStream out, String algorithm)
   throws NoSuchAlgorithmException {
    super(out);
    digest = MessageDigest.getInstance(algorithm);
  }

  public EasyDigestOutputStream(OutputStream out, String algorithm,                                 
   String provider) throws NoSuchAlgorithmException, NoSuchProviderException {
    super(out);
    digest = MessageDigest.getInstance(algorithm, provider);
  }

  public void write(int b) throws IOException {
    out.write(b);
    if (on) digest.update((byte)b);
  }

  public void write(byte[] data, int offset, int length) throws IOException {
    out.write(data, offset, length);
    if (on) digest.update(data, offset, length);
  }

  public void on(boolean on) {
    this.on = on;
  }
  
  public void close() throws IOException {
    out.close();
    result = digest.digest();
  }
  
  public byte[] getDigest() {
    return result;
  }
}

Previous | Next | Top | Cafe con Leche

Copyright 2005 Elliotte Rusty Harold
elharo@metalab.unc.edu
Last Modified August 25, 2005