tribble.security
Class StreamDigest

java.lang.Object
  extended bytribble.security.StreamDigest

public class StreamDigest
extends java.lang.Object

Simple cryptographic file digest generator. Reads an input stream and produces a cryptographic message digest from its byte contents.

Since:
2001-05-06
Version:
$Revision: 1.3 $ $Date: 2003/03/22 14:34:01 $
Author:
David R. Tribble, david@tribble.com.
Copyright ©2001 by David R. Tribble, all rights reserved.
Permission is granted to freely use and distribute this source code provided that the original copyright and authorship notices remain intact.

Field Summary
static int BLKSIZE
          Input block size (in bytes).
static java.lang.String CLASS_NAME
          This class name.
static java.lang.String DFL_ALG
          Default message digest algorithm ("MD5").
protected  java.lang.String m_alg
          Message digest algorithm.
protected  java.security.MessageDigest m_dig
          Cryptographic message digest engine.
protected  java.io.InputStream m_in
          Byte input stream.
 
Constructor Summary
StreamDigest()
          Default constructor.
StreamDigest(java.io.InputStream in)
          Constructor.
StreamDigest(java.io.InputStream in, java.lang.String alg)
          Constructor.
 
Method Summary
 void close()
          Closes the input stream.
 java.lang.String digest()
          Reads the contents of the input stream, producing a cryptographic message digest, and returning it as a printable text string.
 java.lang.String getAlgorithm()
          Retrieves the cryptographic message digest algorithm to use.
 java.io.InputStream getInput()
          Retrieves the input stream from which to read bytes.
static void main(java.lang.String[] args)
          Runs this class as a program.
protected static void run(java.lang.String[] args)
          Runs this class as a program.
 void setAlgorithm(java.lang.String alg)
          Establishes the cryptographic message digest algorithm to use.
 void setInput(java.io.InputStream in)
          Establishes the input stream from which to read bytes.
protected static void usage()
          Displays a program usage message, then punts.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

CLASS_NAME

public static final java.lang.String CLASS_NAME
This class name.


DFL_ALG

public static final java.lang.String DFL_ALG
Default message digest algorithm ("MD5").

See Also:
Constant Field Values

BLKSIZE

public static final int BLKSIZE
Input block size (in bytes).

See Also:
Constant Field Values

m_in

protected java.io.InputStream m_in
Byte input stream.


m_alg

protected java.lang.String m_alg
Message digest algorithm.


m_dig

protected java.security.MessageDigest m_dig
Cryptographic message digest engine.

Constructor Detail

StreamDigest

public StreamDigest()
Default constructor.

Since:
1.1, 2001-05-06

StreamDigest

public StreamDigest(java.io.InputStream in)
Constructor. Establishes the input stream.

Parameters:
in - The input stream from which to read bytes.
Since:
1.1, 2001-05-06

StreamDigest

public StreamDigest(java.io.InputStream in,
                    java.lang.String alg)
             throws java.security.NoSuchAlgorithmException
Constructor. Establishes the input stream and the cryptographic message digest algorithm to use.

Parameters:
in - The input stream from which to read bytes.
alg - The cryptographic message digest algorithm to use.
Throws:
java.security.NoSuchAlgorithmException - Thrown if alg does not specify a supported digest algorithm.
Since:
1.1, 2001-05-06
Method Detail

main

public static void main(java.lang.String[] args)
                 throws java.lang.Exception
Runs this class as a program.

Parameters:
args - The command line arguments.
Throws:
java.lang.Exception
Since:
1.1, 2001-05-06

setAlgorithm

public void setAlgorithm(java.lang.String alg)
                  throws java.security.NoSuchAlgorithmException
Establishes the cryptographic message digest algorithm to use.

Parameters:
alg - The cryptographic message digest algorithm to use.
Throws:
java.security.NoSuchAlgorithmException - Thrown if alg does not specify a supported digest algorithm.
Since:
1.1, 2001-05-06

getAlgorithm

public java.lang.String getAlgorithm()
Retrieves the cryptographic message digest algorithm to use.

Returns:
The cryptographic message digest algorithm to use.
Since:
1.1, 2001-05-06

setInput

public void setInput(java.io.InputStream in)
Establishes the input stream from which to read bytes.

Parameters:
in - The input stream from which to read bytes.
Since:
1.1, 2001-05-06

getInput

public java.io.InputStream getInput()
Retrieves the input stream from which to read bytes.

Returns:
The input stream from which to read bytes.
Since:
1.1, 2001-05-06

close

public void close()
Closes the input stream.

Since:
1.1, 2001-05-06

digest

public java.lang.String digest()
                        throws java.io.IOException,
                               java.security.NoSuchAlgorithmException
Reads the contents of the input stream, producing a cryptographic message digest, and returning it as a printable text string.

Returns:
The resulting cryptographic message digest as a printable string of hex digits.
Throws:
java.io.IOException - Thrown if an I/O error occurs.
java.security.NoSuchAlgorithmException - Thrown if an unsupported digest algorithm is used.
Since:
1.1, 2001-05-06

usage

protected static void usage()
Displays a program usage message, then punts.

Reads the contents of one of more files, computing a cryptographic message digest, and writes the resulting digest for each one to the standard output.

Usage

    java tribble.security.StreamDigest file...
 
If file is "-", input is read from the standard input.

Since:
1.1, 2001-05-06

run

protected static void run(java.lang.String[] args)
                   throws java.io.IOException,
                          java.security.NoSuchAlgorithmException
Runs this class as a program.

Reads the contents of one of more files, computing a cryptographic message digest, and writes the resulting digest for each one to the standard output.

Usage
See usage().

Parameters:
args - The command line arguments.
Throws:
java.io.IOException
java.security.NoSuchAlgorithmException
Since:
1.1, 2001-05-06