/* MD2.java -- Copyright (C) 2001, 2002, 2006 Free Software Foundation, Inc. This file is a part of GNU Classpath. GNU Classpath is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. GNU Classpath is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with GNU Classpath; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA Linking this library statically or dynamically with other modules is making a combined work based on this library. Thus, the terms and conditions of the GNU General Public License cover the whole combination. As a special exception, the copyright holders of this library give you permission to link this library with independent modules to produce an executable, regardless of the license terms of these independent modules, and to copy and distribute the resulting executable under terms of your choice, provided that you also meet, for each linked independent module, the terms and conditions of the license of that module. An independent module is a module which is not derived from or based on this library. If you modify this library, you may extend this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version. */ package gnu.java.security.hash; import gnu.java.security.Registry; import gnu.java.security.util.Util; /** * An implementation of the MD2 message digest algorithm. *
* MD2 is not widely used. Unless it is needed for compatibility with * existing systems, it is not recommended for use in new applications. *
* References: *
BLOCK_LENGTH
bytes
* are also used to store the running digest.
*/
private byte[] work;
/** Creates a new MD2 digest ready for use. */
public MD2()
{
super(Registry.MD2_HASH, DIGEST_LENGTH, BLOCK_LENGTH);
}
/**
* Private constructor used for cloning.
*
* @param md2 the instance to clone.
*/
private MD2(MD2 md2)
{
this();
// superclass field
this.count = md2.count;
this.buffer = (byte[]) md2.buffer.clone();
// private field
this.checksum = (byte[]) md2.checksum.clone();
this.work = (byte[]) md2.work.clone();
}
public Object clone()
{
return new MD2(this);
}
protected byte[] getResult()
{
byte[] result = new byte[DIGEST_LENGTH];
// Encrypt checksum as last block.
encryptBlock(checksum, 0);
for (int i = 0; i < BLOCK_LENGTH; i++)
result[i] = work[i];
return result;
}
protected void resetContext()
{
checksum = new byte[BLOCK_LENGTH];
work = new byte[BLOCK_LENGTH * 3];
}
public boolean selfTest()
{
if (valid == null)
{
String d = Util.toString(new MD2().digest());
valid = Boolean.valueOf(DIGEST0.equals(d));
}
return valid.booleanValue();
}
/**
* Generates an array of padding bytes. The padding is defined as
* i
bytes of value i
, where i
is the
* number of bytes to fill the last block of the message to
* BLOCK_LENGTH
bytes (or BLOCK_LENGTH
bytes when
* the last block was completely full).
*
* @return the bytes to pad the remaining bytes in the buffer before
* completing a hash operation.
*/
protected byte[] padBuffer()
{
int length = BLOCK_LENGTH - (int) (count % BLOCK_LENGTH);
if (length == 0)
length = BLOCK_LENGTH;
byte[] pad = new byte[length];
for (int i = 0; i < length; i++)
pad[i] = (byte) length;
return pad;
}
/**
* Adds BLOCK_LENGTH
bytes to the running digest.
*
* @param in the byte array to take the BLOCK_LENGTH
bytes from.
* @param off the offset to start from in the given byte array.
*/
protected void transform(byte[] in, int off)
{
updateCheckSumAndEncryptBlock(in, off);
}
/**
* Adds a new block (BLOCK_LENGTH
bytes) to the running digest
* from the given byte array starting from the given offset.
*/
private void encryptBlock(byte[] in, int off)
{
for (int i = 0; i < BLOCK_LENGTH; i++)
{
byte b = in[off + i];
work[BLOCK_LENGTH + i] = b;
work[BLOCK_LENGTH * 2 + i] = (byte)(work[i] ^ b);
}
byte t = 0;
for (int i = 0; i < 18; i++)
{
for (int j = 0; j < 3 * BLOCK_LENGTH; j++)
{
t = (byte)(work[j] ^ PI[t & 0xFF]);
work[j] = t;
}
t = (byte)(t + i);
}
}
/**
* Optimized method that combines a checksum update and encrypt of a block.
*/
private void updateCheckSumAndEncryptBlock(byte[] in, int off)
{
byte l = checksum[BLOCK_LENGTH - 1];
for (int i = 0; i < BLOCK_LENGTH; i++)
{
byte b = in[off + i];
work[BLOCK_LENGTH + i] = b;
work[BLOCK_LENGTH * 2 + i] = (byte)(work[i] ^ b);
l = (byte)(checksum[i] ^ PI[(b ^ l) & 0xFF]);
checksum[i] = l;
}
byte t = 0;
for (int i = 0; i < 18; i++)
{
for (int j = 0; j < 3 * BLOCK_LENGTH; j++)
{
t = (byte)(work[j] ^ PI[t & 0xFF]);
work[j] = t;
}
t = (byte)(t + i);
}
}
}