001: /*
002: * Implements MD5 functionality on a stream.
003: *
004: * written Santeri Paavolainen, Helsinki Finland 1996
005: * (c) Santeri Paavolainen, Helsinki Finland 1996
006: * modifications Copyright (C) 2002 Stephen Ostermiller
007: * http://ostermiller.org/contact.pl?regarding=Java+Utilities
008: *
009: * This program is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * This program is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
017: * GNU General Public License for more details.
018: *
019: * See COPYING.TXT for details.
020: *
021: * The original work by Santeri Paavolainen can be found a
022: * http://www.helsinki.fi/~sjpaavol/programs/md5/
023: */
024: package com.Ostermiller.util;
025:
026: import java.io.*;
027:
028: /**
029: * Implements MD5 functionality on a stream.
030: * More information about this class is available from <a target="_top" href=
031: * "http://ostermiller.org/utils/MD5.html">ostermiller.org</a>.
032: * <p>
033: * This class produces a 128-bit "fingerprint" or "message digest" for
034: * all data written to this stream.
035: * It is conjectured that it is computationally infeasible to produce
036: * two messages having the same message digest, or to produce any
037: * message having a given pre-specified target message digest. The MD5
038: * algorithm is intended for digital signature applications, where a
039: * large file must be "compressed" in a secure manner before being
040: * encrypted with a private (secret) key under a public-key cryptosystem
041: * such as RSA.
042: * <p>
043: * For more information see RFC1321.
044: *
045: * @see MD5
046: * @see MD5InputStream
047: *
048: * @author Santeri Paavolainen http://www.helsinki.fi/~sjpaavol/programs/md5/
049: * @author Stephen Ostermiller http://ostermiller.org/contact.pl?regarding=Java+Utilities
050: * @since ostermillerutils 1.00.00
051: */
052: public class MD5OutputStream extends FilterOutputStream {
053:
054: /**
055: * MD5 context
056: */
057: private MD5 md5;
058:
059: /**
060: * Creates MD5OutputStream
061: * @param out The output stream
062: *
063: * @since ostermillerutils 1.00.00
064: */
065: public MD5OutputStream(OutputStream out) {
066: super (out);
067: md5 = new MD5();
068: }
069:
070: /**
071: * Writes the specified byte to this output stream.
072: *
073: * @param b the byte.
074: * @throws IOException if an I/O error occurs.
075: *
076: * @since ostermillerutils 1.00.00
077: */
078: @Override
079: public void write(int b) throws IOException {
080: out.write(b);
081: md5.update((byte) (b & 0xff));
082: }
083:
084: /**
085: * Writes length bytes from the specified byte array starting a
086: * offset off to this output stream.
087: *
088: * @param b the data.
089: * @param off the start offset in the data.
090: * @param len the number of bytes to write.
091: * @throws IOException if an I/O error occurs.
092: *
093: * @since ostermillerutils 1.00.00
094: */
095: @Override
096: public void write(byte b[], int off, int len) throws IOException {
097: out.write(b, off, len);
098: md5.update(b, off, len);
099: }
100:
101: /**
102: * Returns array of bytes representing hash of the stream so far.
103: *
104: * @return Array of 16 bytes, the hash of all written bytes.
105: *
106: * @since ostermillerutils 1.00.00
107: */
108: public byte[] getHash() {
109: return md5.getHash();
110: }
111:
112: /**
113: * Get a 32-character hex representation representing hash of the stream so far.
114: *
115: * @return A string containing the hash of all written bytes.
116: *
117: * @since ostermillerutils 1.00.00
118: */
119: public String getHashString() {
120: return md5.getHashString();
121: }
122: }
|