001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.crypto.digest;
023:
024: import java.io.ByteArrayOutputStream;
025: import java.security.MessageDigest;
026: import java.security.MessageDigestSpi;
027: import java.security.NoSuchAlgorithmException;
028: import java.security.ProviderException;
029:
030: /** The SHA_Interleave algorithm as described in section 3.1 of RFC2945. This
031: needs an SHA MessageDigest provider to function.
032:
033: @author Scott.Stark@jboss.org
034: @version $Revision: 57203 $
035: */
036: public class SHAInterleave extends MessageDigestSpi {
037: private static final int SHA_HASH_LEN = 20;
038:
039: private ByteArrayOutputStream evenBytes;
040: private ByteArrayOutputStream oddBytes;
041: private int count;
042: private boolean skipLeadingZeros;
043: private MessageDigest sha;
044:
045: /** Creates a new instance of SHAInterleave
046: @exception ProviderException thrown if MessageDigest.getInstance("SHA")
047: throws a NoSuchAlgorithmException.
048: */
049: public SHAInterleave() {
050: try {
051: sha = MessageDigest.getInstance("SHA");
052: } catch (NoSuchAlgorithmException e) {
053: throw new ProviderException(
054: "Failed to obtain SHA MessageDigest");
055: }
056: evenBytes = new ByteArrayOutputStream();
057: oddBytes = new ByteArrayOutputStream();
058: engineReset();
059: }
060:
061: protected int engineGetDigestLength() {
062: return 2 * SHA_HASH_LEN;
063: }
064:
065: /**
066: * Completes the digest computation by performing final
067: * operations such as padding. Once <code>engineDigest</code> has
068: * been called, the engine should be reset (see
069: * {@link #engineReset() engineReset}).
070: * Resetting is the responsibility of the
071: * engine implementor.
072: *
073: * @return the array of bytes for the resulting digest value.
074: */
075: protected byte[] engineDigest() {
076: byte[] E = evenBytes.toByteArray();
077: byte[] G = sha.digest(E);
078: // If the count is odd, drop the first byte
079: byte[] F = oddBytes.toByteArray();
080: int offset = 0;
081: if (count % 2 == 1)
082: offset = 1;
083: sha.reset();
084: sha.update(F, offset, F.length - offset);
085: byte[] H = sha.digest();
086: int length = G.length + H.length;
087: byte[] digest = new byte[length];
088: for (int i = 0; i < G.length; ++i)
089: digest[2 * i] = G[i];
090: for (int i = 0; i < H.length; ++i)
091: digest[2 * i + 1] = H[i];
092: engineReset();
093: return digest;
094: }
095:
096: /**
097: * Resets the digest for further use.
098: */
099: protected void engineReset() {
100: skipLeadingZeros = true;
101: count = 0;
102: evenBytes.reset();
103: oddBytes.reset();
104: sha.reset();
105: }
106:
107: /**
108: * Updates the digest using the specified byte.
109: *
110: * @param input the byte to use for the update.
111: */
112: protected void engineUpdate(byte input) {
113: if (skipLeadingZeros == true && input == 0)
114: return;
115: skipLeadingZeros = false;
116: if (count % 2 == 0)
117: evenBytes.write(input);
118: else
119: oddBytes.write(input);
120: count++;
121: }
122:
123: /**
124: * Updates the digest using the specified array of bytes,
125: * starting at the specified offset.
126: *
127: * @param input the array of bytes to use for the update.
128: * @param offset the offset to start from in the array of bytes.
129: * @param len the input of bytes to use, starting at
130: * <code>offset</code>.
131: */
132: protected void engineUpdate(byte[] input, int offset, int len) {
133: for (int i = offset; i < offset + len; i++)
134: engineUpdate(input[i]);
135: }
136:
137: }
|