001: /*
002: * @(#)MessageDigest.java 1.10 02/07/24 @(#)
003: *
004: * Copyright (c) 2000-2001 Sun Microsystems, Inc. All rights reserved.
005: * PROPRIETARY/CONFIDENTIAL
006: * Use is subject to license terms.
007: */
008:
009: package com.sun.portal.ksecurity;
010:
011: /**
012: * Implements an abstract class that generalizes all message digests.
013: * It is modelled after javacard.security.MessageDigest. This version
014: * of the implementation only supports ALG_MD5 (which produces a 16-byte
015: * hash as described in RFC 2313) and ALG_SHA (which produces a 20-byte
016: * hash using NIST's SHA1 algorithm). MD2, a predecessor to MD5, is not
017: * supported.
018: */
019: public abstract class MessageDigest {
020: /** Indicates the MD5 message digest algorithm. */
021: public static final byte ALG_MD5 = 1;
022: /** Indicates the SHA message digest algorithm. */
023: public static final byte ALG_SHA = 2;
024: /** Indicates the MD2 message digest algorithm. */
025: public static final byte ALG_MD2 = 3;
026:
027: /** Protected constructor. */
028: protected MessageDigest() {
029: }
030:
031: /**
032: * Creates a MessageDigest object instance of the specified
033: * algorithm. The second parameter is ignored for now -- it is
034: * here only for compatibility with the javacard.security.MessageDigest
035: * class.
036: * @param alg the desired message digest algorithm, e.g. ALG_MD5
037: * @param ext ignored
038: * @return a MessageDigest object instance of the requested algorithm
039: * @exception CryptoException with NO_SUCH_ALGORITHM reason code if the
040: * requested algorithm is not supported.
041: */
042: public static MessageDigest getInstance(byte alg, boolean ext)
043: throws CryptoException {
044: switch (alg) {
045: case ALG_MD5:
046: return new MD5();
047: case ALG_SHA:
048: return new SHA();
049: case ALG_MD2:
050: return new MD2();
051: default:
052: throw new CryptoException(CryptoException.NO_SUCH_ALGORITHM);
053: }
054: }
055:
056: /**
057: * Gets the message digest algorithm.
058: * @return algorithm implemented by this MessageDigest object
059: */
060: public abstract byte getAlgorithm();
061:
062: /**
063: * Gets the length (in bytes) of the hash.
064: * @return byte-length of the hash produced by this object
065: */
066: public abstract byte getLength();
067:
068: /**
069: * Generates a hash of all/last input data. Completes and returns the
070: * hash compuatation after performing final operations such as padding.
071: * The MessageDigest object is reset after this call.
072: * @param inBuf input buffer of data to be hashed
073: * @param inOff offset within inBuf where input data begins
074: * @param inLen length (in bytes) of data to be hashed
075: * @param outBuf output buffer where the hash should be placed
076: * @param outOff offset within outBuf where the resulting hash begins
077: * @return number of bytes of hash left in outBuf
078: */
079: public abstract short doFinal(byte[] inBuf, int inOff, int inLen,
080: byte[] outBuf, int outOff);
081:
082: /**
083: * Accumulates a hash of the input data. This method is useful when
084: * the input data to be hashed is not available in one byte array.
085: * @param inBuf input buffer of data to be hashed
086: * @param inOff offset within inBuf where input data begins
087: * @param inLen length (in bytes) of data to be hashed
088: * @see #doFinal(byte[], int, int, byte[], int)
089: */
090: public abstract void update(byte[] inBuf, int inOff, int inLen);
091:
092: /**
093: * Resets the MessageDigest to the initial state for further use.
094: */
095: public abstract void reset();
096:
097: /**
098: * Clones the MessageDigest object.
099: * @return a clone of this object
100: */
101: public abstract Object clone();
102: }
|