Imported GNU Classpath 0.92

2006-08-14  Mark Wielaard  <mark@klomp.org>

       Imported GNU Classpath 0.92
       * HACKING: Add more importing hints. Update automake version
       requirement.

       * configure.ac (gconf-peer): New enable AC argument.
       Add --disable-gconf-peer and --enable-default-preferences-peer
       to classpath configure when gconf is disabled.
       * scripts/makemake.tcl: Set gnu/java/util/prefs/gconf and
       gnu/java/awt/dnd/peer/gtk to bc. Classify
       gnu/java/security/Configuration.java as generated source file.

       * gnu/java/lang/management/VMGarbageCollectorMXBeanImpl.java,
       gnu/java/lang/management/VMMemoryPoolMXBeanImpl.java,
       gnu/java/lang/management/VMClassLoadingMXBeanImpl.java,
       gnu/java/lang/management/VMRuntimeMXBeanImpl.java,
       gnu/java/lang/management/VMMemoryManagerMXBeanImpl.java,
       gnu/java/lang/management/VMThreadMXBeanImpl.java,
       gnu/java/lang/management/VMMemoryMXBeanImpl.java,
       gnu/java/lang/management/VMCompilationMXBeanImpl.java: New VM stub
       classes.
       * java/lang/management/VMManagementFactory.java: Likewise.
       * java/net/VMURLConnection.java: Likewise.
       * gnu/java/nio/VMChannel.java: Likewise.

       * java/lang/Thread.java (getState): Add stub implementation.
       * java/lang/Class.java (isEnum): Likewise.
       * java/lang/Class.h (isEnum): Likewise.

       * gnu/awt/xlib/XToolkit.java (getClasspathTextLayoutPeer): Removed.

       * javax/naming/spi/NamingManager.java: New override for StackWalker
       functionality.

       * configure, sources.am, Makefile.in, gcj/Makefile.in,
       include/Makefile.in, testsuite/Makefile.in: Regenerated.

From-SVN: r116139
This commit is contained in:
Mark Wielaard 2006-08-14 23:12:35 +00:00
parent abab460491
commit ac1ed908de
1294 changed files with 99479 additions and 35933 deletions

View file

@ -39,14 +39,11 @@ exception statement from your version. */
package gnu.java.security.hash;
/**
* <p>A base abstract class to facilitate hash implementations.</p>
* A base abstract class to facilitate hash implementations.
*/
public abstract class BaseHash implements IMessageDigest
public abstract class BaseHash
implements IMessageDigest
{
// Constants and variables
// -------------------------------------------------------------------------
/** The canonical name prefix of the hash. */
protected String name;
@ -62,12 +59,9 @@ public abstract class BaseHash implements IMessageDigest
/** Temporary input buffer. */
protected byte[] buffer;
// Constructor(s)
// -------------------------------------------------------------------------
/**
* <p>Trivial constructor for use by concrete subclasses.</p>
*
* Trivial constructor for use by concrete subclasses.
*
* @param name the canonical name prefix of this instance.
* @param hashSize the block size of the output in bytes.
* @param blockSize the block size of the internal transform.
@ -84,14 +78,6 @@ public abstract class BaseHash implements IMessageDigest
resetContext();
}
// Class methods
// -------------------------------------------------------------------------
// Instance methods
// -------------------------------------------------------------------------
// IMessageDigest interface implementation ---------------------------------
public String name()
{
return name;
@ -114,9 +100,7 @@ public abstract class BaseHash implements IMessageDigest
count++;
buffer[i] = b;
if (i == (blockSize - 1))
{
transform(buffer, 0);
}
transform(buffer, 0);
}
public void update(byte[] b)
@ -136,16 +120,13 @@ public abstract class BaseHash implements IMessageDigest
System.arraycopy(b, offset, buffer, n, partLen);
transform(buffer, 0);
for (i = partLen; i + blockSize - 1 < len; i += blockSize)
{
transform(b, offset + i);
}
transform(b, offset + i);
n = 0;
}
if (i < len)
{
System.arraycopy(b, offset + i, buffer, n, len - i);
}
System.arraycopy(b, offset + i, buffer, n, len - i);
}
public byte[] digest()
@ -163,31 +144,27 @@ public abstract class BaseHash implements IMessageDigest
{ // reset this instance for future re-use
count = 0L;
for (int i = 0; i < blockSize;)
{
buffer[i++] = 0;
}
buffer[i++] = 0;
resetContext();
}
// methods to be implemented by concrete subclasses ------------------------
public abstract Object clone();
public abstract boolean selfTest();
/**
* <p>Returns the byte array to use as padding before completing a hash
* operation.</p>
*
* Returns the byte array to use as padding before completing a hash
* operation.
*
* @return the bytes to pad the remaining bytes in the buffer before
* completing a hash operation.
* completing a hash operation.
*/
protected abstract byte[] padBuffer();
/**
* <p>Constructs the result from the contents of the current context.</p>
*
* Constructs the result from the contents of the current context.
*
* @return the output of the completed hash operation.
*/
protected abstract byte[] getResult();
@ -196,11 +173,11 @@ public abstract class BaseHash implements IMessageDigest
protected abstract void resetContext();
/**
* <p>The block digest transformation per se.</p>
*
* The block digest transformation per se.
*
* @param in the <i>blockSize</i> long block, as an array of bytes to digest.
* @param offset the index where the data to digest is located within the
* input buffer.
* input buffer.
*/
protected abstract void transform(byte[] in, int offset);
}

View file

@ -45,113 +45,73 @@ import java.util.HashSet;
import java.util.Set;
/**
* <p>A <i>Factory</i> to instantiate message digest algorithm instances.</p>
* A <i>Factory</i> to instantiate message digest algorithm instances.
*/
public class HashFactory
{
// Constants and variables
// -------------------------------------------------------------------------
// Constructor(s)
// -------------------------------------------------------------------------
/** Trivial constructor to enforce <i>Singleton</i> pattern. */
private HashFactory()
{
super();
}
// Class methods
// -------------------------------------------------------------------------
/**
* <p>Return an instance of a hash algorithm given its name.</p>
*
* Return an instance of a hash algorithm given its name.
*
* @param name the name of the hash algorithm.
* @return an instance of the hash algorithm, or null if none found.
* @exception InternalError if the implementation does not pass its self-
* test.
* test.
*/
public static IMessageDigest getInstance(String name)
{
if (name == null)
{
return null;
}
return null;
name = name.trim();
IMessageDigest result = null;
if (name.equalsIgnoreCase(Registry.WHIRLPOOL_HASH))
{
result = new Whirlpool();
}
result = new Whirlpool();
else if (name.equalsIgnoreCase(Registry.RIPEMD128_HASH)
|| name.equalsIgnoreCase(Registry.RIPEMD_128_HASH))
{
result = new RipeMD128();
}
result = new RipeMD128();
else if (name.equalsIgnoreCase(Registry.RIPEMD160_HASH)
|| name.equalsIgnoreCase(Registry.RIPEMD_160_HASH))
{
result = new RipeMD160();
}
result = new RipeMD160();
else if (name.equalsIgnoreCase(Registry.SHA160_HASH)
|| name.equalsIgnoreCase(Registry.SHA_1_HASH)
|| name.equalsIgnoreCase(Registry.SHA1_HASH)
|| name.equalsIgnoreCase(Registry.SHA_HASH))
{
result = new Sha160();
}
result = new Sha160();
else if (name.equalsIgnoreCase(Registry.SHA256_HASH))
{
result = new Sha256();
}
result = new Sha256();
else if (name.equalsIgnoreCase(Registry.SHA384_HASH))
{
result = new Sha384();
}
result = new Sha384();
else if (name.equalsIgnoreCase(Registry.SHA512_HASH))
{
result = new Sha512();
}
result = new Sha512();
else if (name.equalsIgnoreCase(Registry.TIGER_HASH))
{
result = new Tiger();
}
result = new Tiger();
else if (name.equalsIgnoreCase(Registry.HAVAL_HASH))
{
result = new Haval();
}
result = new Haval();
else if (name.equalsIgnoreCase(Registry.MD5_HASH))
{
result = new MD5();
}
result = new MD5();
else if (name.equalsIgnoreCase(Registry.MD4_HASH))
{
result = new MD4();
}
result = new MD4();
else if (name.equalsIgnoreCase(Registry.MD2_HASH))
{
result = new MD2();
}
result = new MD2();
else if (name.equalsIgnoreCase(Registry.HAVAL_HASH))
{
result = new Haval();
}
result = new Haval();
if (result != null && !result.selfTest())
{
throw new InternalError(result.name());
}
if (result != null && ! result.selfTest())
throw new InternalError(result.name());
return result;
}
/**
* <p>Returns a {@link Set} of names of hash algorithms supported by this
* <i>Factory</i>.</p>
*
* Returns a {@link Set} of names of hash algorithms supported by this
* <i>Factory</i>.
*
* @return a {@link Set} of hash names (Strings).
*/
public static final Set getNames()
@ -172,7 +132,4 @@ public class HashFactory
return Collections.unmodifiableSet(hs);
}
// Instance methods
// -------------------------------------------------------------------------
}

View file

@ -42,25 +42,21 @@ import gnu.java.security.Registry;
import gnu.java.security.util.Util;
/**
* <p>The <i>HAVAL</i> message-digest algorithm is a variable output length,
* with variable number of rounds. By default, this implementation allows
* <i>HAVAL</i> to be used as a drop-in replacement for <i>MD5</i>.</p>
*
* <p>References:</p>
*
* The <i>HAVAL</i> message-digest algorithm is a variable output length, with
* variable number of rounds. By default, this implementation allows <i>HAVAL</i>
* to be used as a drop-in replacement for <i>MD5</i>.
* <p>
* References:
* <ol>
* <li>HAVAL - A One-Way Hashing Algorithm with Variable Length of Output<br>
* Advances in Cryptology - AUSCRYPT'92, Lecture Notes in Computer Science,<br>
* Springer-Verlag, 1993; <br>
* Y. Zheng, J. Pieprzyk and J. Seberry.</li>
* <li>HAVAL - A One-Way Hashing Algorithm with Variable Length of Output<br>
* Advances in Cryptology - AUSCRYPT'92, Lecture Notes in Computer Science,<br>
* Springer-Verlag, 1993; <br>
* Y. Zheng, J. Pieprzyk and J. Seberry.</li>
* </ol>
*/
public class Haval extends BaseHash
public class Haval
extends BaseHash
{
// Constants and variables
// -------------------------------------------------------------------------
public static final int HAVAL_VERSION = 1;
public static final int HAVAL_128_BIT = 16;
@ -88,20 +84,18 @@ public class Haval extends BaseHash
/**
* Number of HAVAL rounds. Allowed values are integers in the range <code>3
* .. 5</code>. The default is <code>3</code>.
* .. 5</code>.
* The default is <code>3</code>.
*/
private int rounds = HAVAL_3_ROUND;
/** 128-bit interim result. */
private int h0, h1, h2, h3, h4, h5, h6, h7;
// Constructor(s)
// -------------------------------------------------------------------------
/**
* <p>Calls the constructor with two argument using {@link #HAVAL_128_BIT} as
* the value for the output size (i.e. <code>128</code> bits, and
* {@link #HAVAL_3_ROUND} for the value of number of rounds.</p>
* Calls the constructor with two argument using {@link #HAVAL_128_BIT} as the
* value for the output size (i.e. <code>128</code> bits, and
* {@link #HAVAL_3_ROUND} for the value of number of rounds.
*/
public Haval()
{
@ -109,9 +103,9 @@ public class Haval extends BaseHash
}
/**
* <p>Calls the constructor with two arguments using the designated output
* size, and {@link #HAVAL_3_ROUND} for the value of number of rounds.</p>
*
* Calls the constructor with two arguments using the designated output size,
* and {@link #HAVAL_3_ROUND} for the value of number of rounds.
*
* @param size the output size in bytes of this instance.
* @throws IllegalArgumentException if the designated output size is invalid.
* @see #HAVAL_128_BIT
@ -126,16 +120,16 @@ public class Haval extends BaseHash
}
/**
* <p>Constructs a <code>Haval</code> instance with the designated output
* size (in bytes). Valid output <code>size</code> values are <code>16</code>,
* <code>20</code>, <code>24</code>, <code>28</code> and <code>32</code>.
* Valid values for <code>rounds</code> are in the range <code>3..5</code>
* inclusive.</p>
*
* Constructs a <code>Haval</code> instance with the designated output size
* (in bytes). Valid output <code>size</code> values are <code>16</code>,
* <code>20</code>, <code>24</code>, <code>28</code> and
* <code>32</code>. Valid values for <code>rounds</code> are in the range
* <code>3..5</code> inclusive.
*
* @param size the output size in bytes of this instance.
* @param rounds the number of rounds to apply when transforming data.
* @throws IllegalArgumentException if the designated output size is invalid,
* or if the number of rounds is invalid.
* or if the number of rounds is invalid.
* @see #HAVAL_128_BIT
* @see #HAVAL_160_BIT
* @see #HAVAL_192_BIT
@ -149,24 +143,24 @@ public class Haval extends BaseHash
{
super(Registry.HAVAL_HASH, size, BLOCK_SIZE);
if (size != HAVAL_128_BIT && size != HAVAL_160_BIT && size != HAVAL_192_BIT
&& size != HAVAL_224_BIT && size != HAVAL_256_BIT)
{
throw new IllegalArgumentException("Invalid HAVAL output size");
}
if (size != HAVAL_128_BIT
&& size != HAVAL_160_BIT
&& size != HAVAL_192_BIT
&& size != HAVAL_224_BIT
&& size != HAVAL_256_BIT)
throw new IllegalArgumentException("Invalid HAVAL output size");
if (rounds != HAVAL_3_ROUND && rounds != HAVAL_4_ROUND
if (rounds != HAVAL_3_ROUND
&& rounds != HAVAL_4_ROUND
&& rounds != HAVAL_5_ROUND)
{
throw new IllegalArgumentException("Invalid HAVAL number of rounds");
}
throw new IllegalArgumentException("Invalid HAVAL number of rounds");
this.rounds = rounds;
}
/**
* <p>Private constructor for cloning purposes.</p>
*
* Private constructor for cloning purposes.
*
* @param md the instance to clone.
*/
private Haval(Haval md)
@ -185,93 +179,142 @@ public class Haval extends BaseHash
this.buffer = (byte[]) md.buffer.clone();
}
// Constructor(s)
// -------------------------------------------------------------------------
// Class methods
// -------------------------------------------------------------------------
// Instance methods
// -------------------------------------------------------------------------
// java.lang.Cloneable interface implementation ----------------------------
public Object clone()
{
return new Haval(this);
}
// Implementation of concrete methods in BaseHash --------------------------
protected synchronized void transform(byte[] in, int i)
{
int X0 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
| (in[i++] & 0xFF) << 24;
int X1 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
| (in[i++] & 0xFF) << 24;
int X2 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
| (in[i++] & 0xFF) << 24;
int X3 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
| (in[i++] & 0xFF) << 24;
int X4 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
| (in[i++] & 0xFF) << 24;
int X5 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
| (in[i++] & 0xFF) << 24;
int X6 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
| (in[i++] & 0xFF) << 24;
int X7 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
| (in[i++] & 0xFF) << 24;
int X8 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
| (in[i++] & 0xFF) << 24;
int X9 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
| (in[i++] & 0xFF) << 24;
int X10 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
| (in[i++] & 0xFF) << 24;
int X11 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
| (in[i++] & 0xFF) << 24;
int X12 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
| (in[i++] & 0xFF) << 24;
int X13 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
| (in[i++] & 0xFF) << 24;
int X14 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
| (in[i++] & 0xFF) << 24;
int X15 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
| (in[i++] & 0xFF) << 24;
int X16 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
| (in[i++] & 0xFF) << 24;
int X17 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
| (in[i++] & 0xFF) << 24;
int X18 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
| (in[i++] & 0xFF) << 24;
int X19 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
| (in[i++] & 0xFF) << 24;
int X20 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
| (in[i++] & 0xFF) << 24;
int X21 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
| (in[i++] & 0xFF) << 24;
int X22 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
| (in[i++] & 0xFF) << 24;
int X23 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
| (in[i++] & 0xFF) << 24;
int X24 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
| (in[i++] & 0xFF) << 24;
int X25 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
| (in[i++] & 0xFF) << 24;
int X26 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
| (in[i++] & 0xFF) << 24;
int X27 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
| (in[i++] & 0xFF) << 24;
int X28 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
| (in[i++] & 0xFF) << 24;
int X29 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
| (in[i++] & 0xFF) << 24;
int X30 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
| (in[i++] & 0xFF) << 24;
int X31 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
| (in[i++] & 0xFF) << 24;
int X0 = (in[i++] & 0xFF)
| (in[i++] & 0xFF) << 8
| (in[i++] & 0xFF) << 16
| (in[i++] & 0xFF) << 24;
int X1 = (in[i++] & 0xFF)
| (in[i++] & 0xFF) << 8
| (in[i++] & 0xFF) << 16
| (in[i++] & 0xFF) << 24;
int X2 = (in[i++] & 0xFF)
| (in[i++] & 0xFF) << 8
| (in[i++] & 0xFF) << 16
| (in[i++] & 0xFF) << 24;
int X3 = (in[i++] & 0xFF)
| (in[i++] & 0xFF) << 8
| (in[i++] & 0xFF) << 16
| (in[i++] & 0xFF) << 24;
int X4 = (in[i++] & 0xFF)
| (in[i++] & 0xFF) << 8
| (in[i++] & 0xFF) << 16
| (in[i++] & 0xFF) << 24;
int X5 = (in[i++] & 0xFF)
| (in[i++] & 0xFF) << 8
| (in[i++] & 0xFF) << 16
| (in[i++] & 0xFF) << 24;
int X6 = (in[i++] & 0xFF)
| (in[i++] & 0xFF) << 8
| (in[i++] & 0xFF) << 16
| (in[i++] & 0xFF) << 24;
int X7 = (in[i++] & 0xFF)
| (in[i++] & 0xFF) << 8
| (in[i++] & 0xFF) << 16
| (in[i++] & 0xFF) << 24;
int X8 = (in[i++] & 0xFF)
| (in[i++] & 0xFF) << 8
| (in[i++] & 0xFF) << 16
| (in[i++] & 0xFF) << 24;
int X9 = (in[i++] & 0xFF)
| (in[i++] & 0xFF) << 8
| (in[i++] & 0xFF) << 16
| (in[i++] & 0xFF) << 24;
int X10 = (in[i++] & 0xFF)
| (in[i++] & 0xFF) << 8
| (in[i++] & 0xFF) << 16
| (in[i++] & 0xFF) << 24;
int X11 = (in[i++] & 0xFF)
| (in[i++] & 0xFF) << 8
| (in[i++] & 0xFF) << 16
| (in[i++] & 0xFF) << 24;
int X12 = (in[i++] & 0xFF)
| (in[i++] & 0xFF) << 8
| (in[i++] & 0xFF) << 16
| (in[i++] & 0xFF) << 24;
int X13 = (in[i++] & 0xFF)
| (in[i++] & 0xFF) << 8
| (in[i++] & 0xFF) << 16
| (in[i++] & 0xFF) << 24;
int X14 = (in[i++] & 0xFF)
| (in[i++] & 0xFF) << 8
| (in[i++] & 0xFF) << 16
| (in[i++] & 0xFF) << 24;
int X15 = (in[i++] & 0xFF)
| (in[i++] & 0xFF) << 8
| (in[i++] & 0xFF) << 16
| (in[i++] & 0xFF) << 24;
int X16 = (in[i++] & 0xFF)
| (in[i++] & 0xFF) << 8
| (in[i++] & 0xFF) << 16
| (in[i++] & 0xFF) << 24;
int X17 = (in[i++] & 0xFF)
| (in[i++] & 0xFF) << 8
| (in[i++] & 0xFF) << 16
| (in[i++] & 0xFF) << 24;
int X18 = (in[i++] & 0xFF)
| (in[i++] & 0xFF) << 8
| (in[i++] & 0xFF) << 16
| (in[i++] & 0xFF) << 24;
int X19 = (in[i++] & 0xFF)
| (in[i++] & 0xFF) << 8
| (in[i++] & 0xFF) << 16
| (in[i++] & 0xFF) << 24;
int X20 = (in[i++] & 0xFF)
| (in[i++] & 0xFF) << 8
| (in[i++] & 0xFF) << 16
| (in[i++] & 0xFF) << 24;
int X21 = (in[i++] & 0xFF)
| (in[i++] & 0xFF) << 8
| (in[i++] & 0xFF) << 16
| (in[i++] & 0xFF) << 24;
int X22 = (in[i++] & 0xFF)
| (in[i++] & 0xFF) << 8
| (in[i++] & 0xFF) << 16
| (in[i++] & 0xFF) << 24;
int X23 = (in[i++] & 0xFF)
| (in[i++] & 0xFF) << 8
| (in[i++] & 0xFF) << 16
| (in[i++] & 0xFF) << 24;
int X24 = (in[i++] & 0xFF)
| (in[i++] & 0xFF) << 8
| (in[i++] & 0xFF) << 16
| (in[i++] & 0xFF) << 24;
int X25 = (in[i++] & 0xFF)
| (in[i++] & 0xFF) << 8
| (in[i++] & 0xFF) << 16
| (in[i++] & 0xFF) << 24;
int X26 = (in[i++] & 0xFF)
| (in[i++] & 0xFF) << 8
| (in[i++] & 0xFF) << 16
| (in[i++] & 0xFF) << 24;
int X27 = (in[i++] & 0xFF)
| (in[i++] & 0xFF) << 8
| (in[i++] & 0xFF) << 16
| (in[i++] & 0xFF) << 24;
int X28 = (in[i++] & 0xFF)
| (in[i++] & 0xFF) << 8
| (in[i++] & 0xFF) << 16
| (in[i++] & 0xFF) << 24;
int X29 = (in[i++] & 0xFF)
| (in[i++] & 0xFF) << 8
| (in[i++] & 0xFF) << 16
| (in[i++] & 0xFF) << 24;
int X30 = (in[i++] & 0xFF)
| (in[i++] & 0xFF) << 8
| (in[i++] & 0xFF) << 16
| (in[i++] & 0xFF) << 24;
int X31 = (in[i++] & 0xFF)
| (in[i++] & 0xFF) << 8
| (in[i++] & 0xFF) << 16
| (in[i++] & 0xFF) << 24;
int t0 = h0, t1 = h1, t2 = h2, t3 = h3, t4 = h4, t5 = h5, t6 = h6, t7 = h7;
// Pass 1
t7 = FF1(t7, t6, t5, t4, t3, t2, t1, t0, X0);
t6 = FF1(t6, t5, t4, t3, t2, t1, t0, t7, X1);
@ -458,7 +501,6 @@ public class Haval extends BaseHash
t0 = FF5(t0, t7, t6, t5, t4, t3, t2, t1, X15, 0x409F60C4);
}
}
h7 += t7;
h6 += t6;
h5 += t5;
@ -471,30 +513,30 @@ public class Haval extends BaseHash
protected byte[] padBuffer()
{
// pad out to 118 mod 128. other 10 bytes have special use.
int n = (int) (count % BLOCK_SIZE);
// pad out to 118 mod 128. other 10 bytes have special use.
int n = (int)(count % BLOCK_SIZE);
int padding = (n < 118) ? (118 - n) : (246 - n);
byte[] result = new byte[padding + 10];
result[0] = (byte) 0x01;
// save the version number (LSB 3), the number of rounds (3 bits in the
// middle), the fingerprint length (MSB 2 bits and next byte) and the
// number of bits in the unpadded message.
int bl = hashSize * 8;
result[padding++] = (byte) (((bl & 0x03) << 6) | ((rounds & 0x07) << 3) | (HAVAL_VERSION & 0x07));
result[padding++] = (byte) (bl >>> 2);
int sigByte = (bl & 0x03) << 6;
sigByte |= (rounds & 0x07) << 3;
sigByte |= HAVAL_VERSION & 0x07;
result[padding++] = (byte) sigByte;
result[padding++] = (byte)(bl >>> 2);
// save number of bits, casting the long to an array of 8 bytes
long bits = count << 3;
result[padding++] = (byte) bits;
result[padding++] = (byte) (bits >>> 8);
result[padding++] = (byte) (bits >>> 16);
result[padding++] = (byte) (bits >>> 24);
result[padding++] = (byte) (bits >>> 32);
result[padding++] = (byte) (bits >>> 40);
result[padding++] = (byte) (bits >>> 48);
result[padding] = (byte) (bits >>> 56);
result[padding++] = (byte)(bits >>> 8);
result[padding++] = (byte)(bits >>> 16);
result[padding++] = (byte)(bits >>> 24);
result[padding++] = (byte)(bits >>> 32);
result[padding++] = (byte)(bits >>> 40);
result[padding++] = (byte)(bits >>> 48);
result[padding ] = (byte)(bits >>> 56);
return result;
}
@ -505,49 +547,48 @@ public class Haval extends BaseHash
byte[] result = new byte[hashSize];
if (hashSize >= HAVAL_256_BIT)
{
result[31] = (byte) (h7 >>> 24);
result[30] = (byte) (h7 >>> 16);
result[29] = (byte) (h7 >>> 8);
result[31] = (byte)(h7 >>> 24);
result[30] = (byte)(h7 >>> 16);
result[29] = (byte)(h7 >>> 8);
result[28] = (byte) h7;
}
if (hashSize >= HAVAL_224_BIT)
{
result[27] = (byte) (h6 >>> 24);
result[26] = (byte) (h6 >>> 16);
result[25] = (byte) (h6 >>> 8);
result[27] = (byte)(h6 >>> 24);
result[26] = (byte)(h6 >>> 16);
result[25] = (byte)(h6 >>> 8);
result[24] = (byte) h6;
}
if (hashSize >= HAVAL_192_BIT)
{
result[23] = (byte) (h5 >>> 24);
result[22] = (byte) (h5 >>> 16);
result[21] = (byte) (h5 >>> 8);
result[23] = (byte)(h5 >>> 24);
result[22] = (byte)(h5 >>> 16);
result[21] = (byte)(h5 >>> 8);
result[20] = (byte) h5;
}
if (hashSize >= HAVAL_160_BIT)
{
result[19] = (byte) (h4 >>> 24);
result[18] = (byte) (h4 >>> 16);
result[17] = (byte) (h4 >>> 8);
result[19] = (byte)(h4 >>> 24);
result[18] = (byte)(h4 >>> 16);
result[17] = (byte)(h4 >>> 8);
result[16] = (byte) h4;
}
result[15] = (byte) (h3 >>> 24);
result[14] = (byte) (h3 >>> 16);
result[13] = (byte) (h3 >>> 8);
result[15] = (byte)(h3 >>> 24);
result[14] = (byte)(h3 >>> 16);
result[13] = (byte)(h3 >>> 8);
result[12] = (byte) h3;
result[11] = (byte) (h2 >>> 24);
result[10] = (byte) (h2 >>> 16);
result[9] = (byte) (h2 >>> 8);
result[8] = (byte) h2;
result[7] = (byte) (h1 >>> 24);
result[6] = (byte) (h1 >>> 16);
result[5] = (byte) (h1 >>> 8);
result[4] = (byte) h1;
result[3] = (byte) (h0 >>> 24);
result[2] = (byte) (h0 >>> 16);
result[1] = (byte) (h0 >>> 8);
result[0] = (byte) h0;
result[11] = (byte)(h2 >>> 24);
result[10] = (byte)(h2 >>> 16);
result[ 9] = (byte)(h2 >>> 8);
result[ 8] = (byte) h2;
result[ 7] = (byte)(h1 >>> 24);
result[ 6] = (byte)(h1 >>> 16);
result[ 5] = (byte)(h1 >>> 8);
result[ 4] = (byte) h1;
result[ 3] = (byte)(h0 >>> 24);
result[ 2] = (byte)(h0 >>> 16);
result[ 1] = (byte)(h0 >>> 8);
result[ 0] = (byte) h0;
return result;
}
@ -567,13 +608,12 @@ public class Haval extends BaseHash
{
if (valid == null)
{
valid = Boolean.valueOf(DIGEST0.equals(Util.toString(new Haval().digest())));
String d = Util.toString(new Haval().digest());
valid = Boolean.valueOf(DIGEST0.equals(d));
}
return valid.booleanValue();
}
// helper methods ----------------------------------------------------------
/** Tailors the last output. */
private void tailorDigestBits()
{
@ -581,17 +621,25 @@ public class Haval extends BaseHash
switch (hashSize)
{
case HAVAL_128_BIT:
t = (h7 & 0x000000FF) | (h6 & 0xFF000000) | (h5 & 0x00FF0000)
| (h4 & 0x0000FF00);
t = (h7 & 0x000000FF)
| (h6 & 0xFF000000)
| (h5 & 0x00FF0000)
| (h4 & 0x0000FF00);
h0 += t >>> 8 | t << 24;
t = (h7 & 0x0000FF00) | (h6 & 0x000000FF) | (h5 & 0xFF000000)
| (h4 & 0x00FF0000);
t = (h7 & 0x0000FF00)
| (h6 & 0x000000FF)
| (h5 & 0xFF000000)
| (h4 & 0x00FF0000);
h1 += t >>> 16 | t << 16;
t = (h7 & 0x00FF0000) | (h6 & 0x0000FF00) | (h5 & 0x000000FF)
| (h4 & 0xFF000000);
t = (h7 & 0x00FF0000)
| (h6 & 0x0000FF00)
| (h5 & 0x000000FF)
| (h4 & 0xFF000000);
h2 += t >>> 24 | t << 8;
t = (h7 & 0xFF000000) | (h6 & 0x00FF0000) | (h5 & 0x0000FF00)
| (h4 & 0x000000FF);
t = (h7 & 0xFF000000)
| (h6 & 0x00FF0000)
| (h5 & 0x0000FF00)
| (h4 & 0x000000FF);
h3 += t;
break;
case HAVAL_160_BIT:
@ -625,9 +673,9 @@ public class Haval extends BaseHash
h1 += ((h7 >>> 22) & 0x1F);
h2 += ((h7 >>> 18) & 0x0F);
h3 += ((h7 >>> 13) & 0x1F);
h4 += ((h7 >>> 9) & 0x0F);
h5 += ((h7 >>> 4) & 0x1F);
h6 += (h7 & 0x0F);
h4 += ((h7 >>> 9) & 0x0F);
h5 += ((h7 >>> 4) & 0x1F);
h6 += (h7 & 0x0F);
}
}
@ -748,8 +796,8 @@ public class Haval extends BaseHash
private int f4(int x6, int x5, int x4, int x3, int x2, int x1, int x0)
{
return x4 & (x5 & ~x2 ^ x3 & ~x6 ^ x1 ^ x6 ^ x0) ^ x3 & (x1 & x2 ^ x5 ^ x6)
^ x2 & x6 ^ x0;
return x4 & (x5 & ~x2 ^ x3 & ~x6 ^ x1 ^ x6 ^ x0) ^ x3
& (x1 & x2 ^ x5 ^ x6) ^ x2 & x6 ^ x0;
}
private int f5(int x6, int x5, int x4, int x3, int x2, int x1, int x0)

View file

@ -39,64 +39,56 @@ exception statement from your version. */
package gnu.java.security.hash;
/**
* <p>The basic visible methods of any hash algorithm.</p>
*
* <p>A hash (or message digest) algorithm produces its output by iterating a
* basic compression function on blocks of data.</p>
* The basic visible methods of any hash algorithm.
* <p>
* A hash (or message digest) algorithm produces its output by iterating a basic
* compression function on blocks of data.
*/
public interface IMessageDigest extends Cloneable
public interface IMessageDigest
extends Cloneable
{
// Constants
// -------------------------------------------------------------------------
// Methods
// -------------------------------------------------------------------------
/**
* <p>Returns the canonical name of this algorithm.</p>
*
* Returns the canonical name of this algorithm.
*
* @return the canonical name of this instance.
*/
String name();
/**
* <p>Returns the output length in bytes of this message digest algorithm.</p>
*
* Returns the output length in bytes of this message digest algorithm.
*
* @return the output length in bytes of this message digest algorithm.
*/
int hashSize();
/**
* <p>Returns the algorithm's (inner) block size in bytes.</p>
*
* Returns the algorithm's (inner) block size in bytes.
*
* @return the algorithm's inner block size in bytes.
*/
int blockSize();
/**
* <p>Continues a message digest operation using the input byte.</p>
*
* Continues a message digest operation using the input byte.
*
* @param b the input byte to digest.
*/
void update(byte b);
/**
* <p>Continues a message digest operation, by filling the buffer, processing
* Continues a message digest operation, by filling the buffer, processing
* data in the algorithm's HASH_SIZE-bit block(s), updating the context and
* count, and buffering the remaining bytes in buffer for the next
* operation.</p>
*
* count, and buffering the remaining bytes in buffer for the next operation.
*
* @param in the input block.
*/
void update(byte[] in);
/**
* <p>Continues a message digest operation, by filling the buffer, processing
* Continues a message digest operation, by filling the buffer, processing
* data in the algorithm's HASH_SIZE-bit block(s), updating the context and
* count, and buffering the remaining bytes in buffer for the next
* operation.</p>
*
* count, and buffering the remaining bytes in buffer for the next operation.
*
* @param in the input block.
* @param offset start of meaningful bytes in input block.
* @param length number of bytes, in input block, to consider.
@ -104,31 +96,31 @@ public interface IMessageDigest extends Cloneable
void update(byte[] in, int offset, int length);
/**
* <p>Completes the message digest by performing final operations such as
* padding and resetting the instance.</p>
*
* Completes the message digest by performing final operations such as padding
* and resetting the instance.
*
* @return the array of bytes representing the hash value.
*/
byte[] digest();
/**
* <p>Resets the current context of this instance clearing any eventually cached
* intermediary values.</p>
* Resets the current context of this instance clearing any eventually cached
* intermediary values.
*/
void reset();
/**
* <p>A basic test. Ensures that the digest of a pre-determined message is equal
* to a known pre-computed value.</p>
*
* @return <tt>true</tt> if the implementation passes a basic self-test.
* Returns <tt>false</tt> otherwise.
* A basic test. Ensures that the digest of a pre-determined message is equal
* to a known pre-computed value.
*
* @return <code>true</code> if the implementation passes a basic self-test.
* Returns <code>false</code> otherwise.
*/
boolean selfTest();
/**
* <p>Returns a clone copy of this instance.</p>
*
* Returns a clone copy of this instance.
*
* @return a clone copy of this instance.
*/
Object clone();

View file

@ -42,13 +42,12 @@ import gnu.java.security.Registry;
import gnu.java.security.util.Util;
/**
* <p>An implementation of the MD2 message digest algorithm.</p>
*
* <p>MD2 is not widely used. Unless it is needed for compatibility with
* existing systems, it is not recommended for use in new applications.</p>
*
* <p>References:</p>
*
* An implementation of the MD2 message digest algorithm.
* <p>
* MD2 is not widely used. Unless it is needed for compatibility with
* existing systems, it is not recommended for use in new applications.
* <p>
* References:
* <ol>
* <li>The <a href="http://www.ietf.org/rfc/rfc1319.txt">MD2</a>
* Message-Digest Algorithm.<br>
@ -57,12 +56,9 @@ import gnu.java.security.util.Util;
* under section RFC 1319.</li>
* </ol>
*/
public class MD2 extends BaseHash
public class MD2
extends BaseHash
{
// Constants and variables
// -------------------------------------------------------------------------
/** An MD2 message digest is always 128-bits long, or 16 bytes. */
private static final int DIGEST_LENGTH = 16;
@ -70,34 +66,39 @@ public class MD2 extends BaseHash
private static final int BLOCK_LENGTH = 16;
/** 256 byte "random" permutation of the digits of pi. */
private static final byte[] PI = { 41, 46, 67, -55, -94, -40, 124, 1, 61, 54,
84, -95, -20, -16, 6, 19, 98, -89, 5, -13,
-64, -57, 115, -116, -104, -109, 43, -39,
-68, 76, -126, -54, 30, -101, 87, 60, -3,
-44, -32, 22, 103, 66, 111, 24, -118, 23,
-27, 18, -66, 78, -60, -42, -38, -98, -34,
73, -96, -5, -11, -114, -69, 47, -18, 122,
-87, 104, 121, -111, 21, -78, 7, 63, -108,
-62, 16, -119, 11, 34, 95, 33, -128, 127,
93, -102, 90, -112, 50, 39, 53, 62, -52,
-25, -65, -9, -105, 3, -1, 25, 48, -77, 72,
-91, -75, -47, -41, 94, -110, 42, -84, 86,
-86, -58, 79, -72, 56, -46, -106, -92, 125,
-74, 118, -4, 107, -30, -100, 116, 4, -15,
69, -99, 112, 89, 100, 113, -121, 32, -122,
91, -49, 101, -26, 45, -88, 2, 27, 96, 37,
-83, -82, -80, -71, -10, 28, 70, 97, 105,
52, 64, 126, 15, 85, 71, -93, 35, -35, 81,
-81, 58, -61, 92, -7, -50, -70, -59, -22,
38, 44, 83, 13, 110, -123, 40, -124, 9,
-45, -33, -51, -12, 65, -127, 77, 82, 106,
-36, 55, -56, 108, -63, -85, -6, 36, -31,
123, 8, 12, -67, -79, 74, 120, -120, -107,
-117, -29, 99, -24, 109, -23, -53, -43, -2,
59, 0, 29, 57, -14, -17, -73, 14, 102, 88,
-48, -28, -90, 119, 114, -8, -21, 117, 75,
10, 49, 68, 80, -76, -113, -19, 31, 26,
-37, -103, -115, 51, -97, 17, -125, 20 };
private static final byte[] PI = {
41, 46, 67, -55, -94, -40, 124, 1,
61, 54, 84, -95, -20, -16, 6, 19,
98, -89, 5, -13, -64, -57, 115, -116,
-104, -109, 43, -39, -68, 76, -126, -54,
30, -101, 87, 60, -3, -44, -32, 22,
103, 66, 111, 24, -118, 23, -27, 18,
-66, 78, -60, -42, -38, -98, -34, 73,
-96, -5, -11, -114, -69, 47, -18, 122,
-87, 104, 121, -111, 21, -78, 7, 63,
-108, -62, 16, -119, 11, 34, 95, 33,
-128, 127, 93, -102, 90, -112, 50, 39,
53, 62, -52, -25, -65, -9, -105, 3,
-1, 25, 48, -77, 72, -91, -75, -47,
-41, 94, -110, 42, -84, 86, -86, -58,
79, -72, 56, -46, -106, -92, 125, -74,
118, -4, 107, -30, -100, 116, 4, -15,
69, -99, 112, 89, 100, 113, -121, 32,
-122, 91, -49, 101, -26, 45, -88, 2,
27, 96, 37, -83, -82, -80, -71, -10,
28, 70, 97, 105, 52, 64, 126, 15,
85, 71, -93, 35, -35, 81, -81, 58,
-61, 92, -7, -50, -70, -59, -22, 38,
44, 83, 13, 110, -123, 40, -124, 9,
-45, -33, -51, -12, 65, -127, 77, 82,
106, -36, 55, -56, 108, -63, -85, -6,
36, -31, 123, 8, 12, -67, -79, 74,
120, -120, -107, -117, -29, 99, -24, 109,
-23, -53, -43, -2, 59, 0, 29, 57,
-14, -17, -73, 14, 102, 88, -48, -28,
-90, 119, 114, -8, -21, 117, 75, 10,
49, 68, 80, -76, -113, -19, 31, 26,
-37, -103, -115, 51, - 97, 17, -125, 20 };
/** The output of this message digest when no data has been input. */
private static final String DIGEST0 = "8350E5A3E24C153DF2275C9F80692773";
@ -114,9 +115,6 @@ public class MD2 extends BaseHash
*/
private byte[] work;
// Constructor(s)
// -------------------------------------------------------------------------
/** Creates a new MD2 digest ready for use. */
public MD2()
{
@ -124,7 +122,7 @@ public class MD2 extends BaseHash
}
/**
* <p>Private constructor used for cloning.</p>
* Private constructor used for cloning.
*
* @param md2 the instance to clone.
*/
@ -135,38 +133,23 @@ public class MD2 extends BaseHash
// 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();
}
// Class methods
// -------------------------------------------------------------------------
// Instance methods
// -------------------------------------------------------------------------
// java.lang.Cloneable interface implementation ----------------------------
public Object clone()
{
return new MD2(this);
}
// Implementation of abstract methods in BaseHash --------------------------
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];
}
result[i] = work[i];
return result;
}
@ -181,17 +164,18 @@ public class MD2 extends BaseHash
{
if (valid == null)
{
valid = Boolean.valueOf(DIGEST0.equals(Util.toString(new MD2().digest())));
String d = Util.toString(new MD2().digest());
valid = Boolean.valueOf(DIGEST0.equals(d));
}
return valid.booleanValue();
}
/**
* <p>Generates an array of padding bytes. The padding is defined as
* Generates an array of padding bytes. The padding is defined as
* <code>i</code> bytes of value <code>i</code>, where <code>i</code> is the
* number of bytes to fill the last block of the message to
* <code>BLOCK_LENGTH</code> bytes (or <code>BLOCK_LENGTH</code> bytes when
* the last block was completely full).</p>
* the last block was completely full).
*
* @return the bytes to pad the remaining bytes in the buffer before
* completing a hash operation.
@ -200,47 +184,26 @@ public class MD2 extends BaseHash
{
int length = BLOCK_LENGTH - (int) (count % BLOCK_LENGTH);
if (length == 0)
{
length = BLOCK_LENGTH;
}
length = BLOCK_LENGTH;
byte[] pad = new byte[length];
for (int i = 0; i < length; i++)
{
pad[i] = (byte) length;
}
pad[i] = (byte) length;
return pad;
}
/**
* <p>Adds <code>BLOCK_LENGTH</code> bytes to the running digest.</p>
* Adds <code>BLOCK_LENGTH</code> bytes to the running digest.
*
* @param in the byte array to take the <code>BLOCK_LENGTH</code> bytes from.
* @param off the offset to start from in the given byte array.
*/
protected void transform(byte[] in, int off)
{
// encryptBlock(in, off);
// updateCheckSum(in, off);
updateCheckSumAndEncryptBlock(in, off);
}
// Private instance methods ------------------------------------------------
/**
* Updates the checksum with the <code>BLOCK_LENGTH</code> bytes from the
* given array starting at <code>off</code>.
*/
/*
private void updateCheckSum(byte[] in, int off) {
byte l = checksum[BLOCK_LENGTH-1];
for (int i = 0; i < BLOCK_LENGTH; i++) {
byte b = in[off+i];
// l = (byte)((checksum[i] & 0xFF) ^ (PI[((b & 0xFF) ^ (l & 0xFF))] & 0xFF));
l = (byte)(checksum[i] ^ PI[(b ^ l) & 0xFF]);
checksum[i] = l;
}
}
*/
/**
* Adds a new block (<code>BLOCK_LENGTH</code> bytes) to the running digest
* from the given byte array starting from the given offset.
@ -251,20 +214,17 @@ public class MD2 extends BaseHash
{
byte b = in[off + i];
work[BLOCK_LENGTH + i] = b;
work[BLOCK_LENGTH * 2 + i] = (byte) (work[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] & 0xFF) ^ (PI[t & 0xFF] & 0xFF));
t = (byte) (work[j] ^ PI[t & 0xFF]);
t = (byte)(work[j] ^ PI[t & 0xFF]);
work[j] = t;
}
// t = (byte)((t + i) & 0xFF);
t = (byte) (t + i);
t = (byte)(t + i);
}
}
@ -278,24 +238,19 @@ public class MD2 extends BaseHash
{
byte b = in[off + i];
work[BLOCK_LENGTH + i] = b;
// work[BLOCK_LENGTH*2+i] = (byte)((work[i] & 0xFF) ^ (b & 0xFF));
work[BLOCK_LENGTH * 2 + i] = (byte) (work[i] ^ b);
// l = (byte)((checksum[i] & 0xFF) ^ (PI[((b & 0xFF) ^ (l & 0xFF))] & 0xFF));
l = (byte) (checksum[i] ^ PI[(b ^ l) & 0xFF]);
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] & 0xFF) ^ (PI[t & 0xFF] & 0xFF));
t = (byte) (work[j] ^ PI[t & 0xFF]);
t = (byte)(work[j] ^ PI[t & 0xFF]);
work[j] = t;
}
// t = (byte)((t + i) & 0xFF);
t = (byte) (t + i);
t = (byte)(t + i);
}
}
}

View file

@ -42,14 +42,13 @@ import gnu.java.security.Registry;
import gnu.java.security.util.Util;
/**
* <p>An implementation of Ron Rivest's MD4 message digest algorithm.</p>
*
* <p>MD4 was the precursor to the stronger {@link gnu.crypto.hash.MD5}
* An implementation of Ron Rivest's MD4 message digest algorithm.
* <p>
* MD4 was the precursor to the stronger {@link gnu.java.security.hash.MD5}
* algorithm, and while not considered cryptograpically secure itself, MD4 is
* in use in various applications. It is slightly faster than MD5.</p>
*
* <p>References:</p>
*
* in use in various applications. It is slightly faster than MD5.
* <p>
* References:
* <ol>
* <li>The <a href="http://www.ietf.org/rfc/rfc1320.txt">MD4</a>
* Message-Digest Algorithm.<br>
@ -58,12 +57,9 @@ import gnu.java.security.util.Util;
*
* @author Casey Marshall (rsdio@metastatic.org)
*/
public class MD4 extends BaseHash
public class MD4
extends BaseHash
{
// Constants and variables
// -------------------------------------------------------------------------
/** An MD4 message digest is always 128-bits long, or 16 bytes. */
private static final int DIGEST_LENGTH = 16;
@ -86,13 +82,9 @@ public class MD4 extends BaseHash
private int a, b, c, d;
// Constructor(s)
// -------------------------------------------------------------------------
/**
* <p>Public constructor. Initializes the chaining variables, sets the byte
* Public constructor. Initializes the chaining variables, sets the byte
* count to <code>0</code>, and creates a new block of <code>512</code> bits.
* </p>
*/
public MD4()
{
@ -100,7 +92,7 @@ public class MD4 extends BaseHash
}
/**
* <p>Trivial private constructor for cloning purposes.</p>
* Trivial private constructor for cloning purposes.
*
* @param that the instance to clone.
*/
@ -116,30 +108,18 @@ public class MD4 extends BaseHash
this.buffer = (byte[]) that.buffer.clone();
}
// Class methods
// -------------------------------------------------------------------------
// Instance methods
// -------------------------------------------------------------------------
// java.lang.Cloneable interface implementation ----------------------------
public Object clone()
{
return new MD4(this);
}
// Implementation of abstract methods in BashHash --------------------------
protected byte[] getResult()
{
byte[] digest = { (byte) a, (byte) (a >>> 8), (byte) (a >>> 16),
(byte) (a >>> 24), (byte) b, (byte) (b >>> 8),
(byte) (b >>> 16), (byte) (b >>> 24), (byte) c,
(byte) (c >>> 8), (byte) (c >>> 16), (byte) (c >>> 24),
(byte) d, (byte) (d >>> 8), (byte) (d >>> 16),
(byte) (d >>> 24) };
return digest;
return new byte[] {
(byte) a, (byte)(a >>> 8), (byte)(a >>> 16), (byte)(a >>> 24),
(byte) b, (byte)(b >>> 8), (byte)(b >>> 16), (byte)(b >>> 24),
(byte) c, (byte)(c >>> 8), (byte)(c >>> 16), (byte)(c >>> 24),
(byte) d, (byte)(d >>> 8), (byte)(d >>> 16), (byte)(d >>> 24) };
}
protected void resetContext()
@ -154,68 +134,97 @@ public class MD4 extends BaseHash
{
if (valid == null)
{
valid = Boolean.valueOf(DIGEST0.equals(Util.toString(new MD4().digest())));
String d = Util.toString(new MD4().digest());
valid = Boolean.valueOf(DIGEST0.equals(d));
}
return valid.booleanValue();
}
protected byte[] padBuffer()
{
int n = (int) (count % BLOCK_LENGTH);
int n = (int)(count % BLOCK_LENGTH);
int padding = (n < 56) ? (56 - n) : (120 - n);
byte[] pad = new byte[padding + 8];
pad[0] = (byte) 0x80;
long bits = count << 3;
pad[padding++] = (byte) bits;
pad[padding++] = (byte) (bits >>> 8);
pad[padding++] = (byte) (bits >>> 16);
pad[padding++] = (byte) (bits >>> 24);
pad[padding++] = (byte) (bits >>> 32);
pad[padding++] = (byte) (bits >>> 40);
pad[padding++] = (byte) (bits >>> 48);
pad[padding] = (byte) (bits >>> 56);
pad[padding++] = (byte)(bits >>> 8);
pad[padding++] = (byte)(bits >>> 16);
pad[padding++] = (byte)(bits >>> 24);
pad[padding++] = (byte)(bits >>> 32);
pad[padding++] = (byte)(bits >>> 40);
pad[padding++] = (byte)(bits >>> 48);
pad[padding ] = (byte)(bits >>> 56);
return pad;
}
protected void transform(byte[] in, int i)
{
int X0 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
| in[i++] << 24;
int X1 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
| in[i++] << 24;
int X2 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
| in[i++] << 24;
int X3 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
| in[i++] << 24;
int X4 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
| in[i++] << 24;
int X5 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
| in[i++] << 24;
int X6 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
| in[i++] << 24;
int X7 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
| in[i++] << 24;
int X8 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
| in[i++] << 24;
int X9 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
| in[i++] << 24;
int X10 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
| in[i++] << 24;
int X11 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
| in[i++] << 24;
int X12 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
| in[i++] << 24;
int X13 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
| in[i++] << 24;
int X14 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
| in[i++] << 24;
int X15 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
| in[i] << 24;
int X0 = (in[i++] & 0xFF)
| (in[i++] & 0xFF) << 8
| (in[i++] & 0xFF) << 16
| in[i++] << 24;
int X1 = (in[i++] & 0xFF)
| (in[i++] & 0xFF) << 8
| (in[i++] & 0xFF) << 16
| in[i++] << 24;
int X2 = (in[i++] & 0xFF)
| (in[i++] & 0xFF) << 8
| (in[i++] & 0xFF) << 16
| in[i++] << 24;
int X3 = (in[i++] & 0xFF)
| (in[i++] & 0xFF) << 8
| (in[i++] & 0xFF) << 16
| in[i++] << 24;
int X4 = (in[i++] & 0xFF)
| (in[i++] & 0xFF) << 8
| (in[i++] & 0xFF) << 16
| in[i++] << 24;
int X5 = (in[i++] & 0xFF)
| (in[i++] & 0xFF) << 8
| (in[i++] & 0xFF) << 16
| in[i++] << 24;
int X6 = (in[i++] & 0xFF)
| (in[i++] & 0xFF) << 8
| (in[i++] & 0xFF) << 16
| in[i++] << 24;
int X7 = (in[i++] & 0xFF)
| (in[i++] & 0xFF) << 8
| (in[i++] & 0xFF) << 16
| in[i++] << 24;
int X8 = (in[i++] & 0xFF)
| (in[i++] & 0xFF) << 8
| (in[i++] & 0xFF) << 16
| in[i++] << 24;
int X9 = (in[i++] & 0xFF)
| (in[i++] & 0xFF) << 8
| (in[i++] & 0xFF) << 16
| in[i++] << 24;
int X10 = (in[i++] & 0xFF)
| (in[i++] & 0xFF) << 8
| (in[i++] & 0xFF) << 16
| in[i++] << 24;
int X11 = (in[i++] & 0xFF)
| (in[i++] & 0xFF) << 8
| (in[i++] & 0xFF) << 16
| in[i++] << 24;
int X12 = (in[i++] & 0xFF)
| (in[i++] & 0xFF) << 8
| (in[i++] & 0xFF) << 16
| in[i++] << 24;
int X13 = (in[i++] & 0xFF)
| (in[i++] & 0xFF) << 8
| (in[i++] & 0xFF) << 16
| in[i++] << 24;
int X14 = (in[i++] & 0xFF)
| (in[i++] & 0xFF) << 8
| (in[i++] & 0xFF) << 16
| in[i++] << 24;
int X15 = (in[i++] & 0xFF)
| (in[i++] & 0xFF) << 8
| (in[i++] & 0xFF) << 16
| in[i] << 24;
int aa, bb, cc, dd;
aa = a;
bb = b;
cc = c;

View file

@ -42,26 +42,22 @@ import gnu.java.security.Registry;
import gnu.java.security.util.Util;
/**
* <p>The MD5 message-digest algorithm takes as input a message of arbitrary
* The MD5 message-digest algorithm takes as input a message of arbitrary
* length and produces as output a 128-bit "fingerprint" or "message digest" of
* the input. It is conjectured that it is computationally infeasible to
* produce two messages having the same message digest, or to produce any
* message having a given prespecified target message digest.</p>
*
* <p>References:</p>
*
* message having a given prespecified target message digest.
* <p>
* References:
* <ol>
* <li>The <a href="http://www.ietf.org/rfc/rfc1321.txt">MD5</a> Message-
* Digest Algorithm.<br>
* R. Rivest.</li>
* </ol>
*/
public class MD5 extends BaseHash
public class MD5
extends BaseHash
{
// Constants and variables
// -------------------------------------------------------------------------
private static final int BLOCK_SIZE = 64; // inner block size in bytes
private static final String DIGEST0 = "D41D8CD98F00B204E9800998ECF8427E";
@ -72,9 +68,6 @@ public class MD5 extends BaseHash
/** 128-bit interim result. */
private int h0, h1, h2, h3;
// Constructor(s)
// -------------------------------------------------------------------------
/** Trivial 0-arguments constructor. */
public MD5()
{
@ -82,7 +75,7 @@ public class MD5 extends BaseHash
}
/**
* <p>Private constructor for cloning purposes.</p>
* Private constructor for cloning purposes.
*
* @param md the instance to clone.
*/
@ -98,61 +91,81 @@ public class MD5 extends BaseHash
this.buffer = (byte[]) md.buffer.clone();
}
// Class methods
// -------------------------------------------------------------------------
// Instance methods
// -------------------------------------------------------------------------
// java.lang.Cloneable interface implementation ----------------------------
public Object clone()
{
return new MD5(this);
}
// Implementation of concrete methods in BaseHash --------------------------
protected synchronized void transform(byte[] in, int i)
{
int X0 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
| in[i++] << 24;
int X1 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
| in[i++] << 24;
int X2 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
| in[i++] << 24;
int X3 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
| in[i++] << 24;
int X4 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
| in[i++] << 24;
int X5 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
| in[i++] << 24;
int X6 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
| in[i++] << 24;
int X7 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
| in[i++] << 24;
int X8 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
| in[i++] << 24;
int X9 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
| in[i++] << 24;
int X10 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
| in[i++] << 24;
int X11 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
| in[i++] << 24;
int X12 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
| in[i++] << 24;
int X13 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
| in[i++] << 24;
int X14 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
| in[i++] << 24;
int X15 = (in[i++] & 0xFF) | (in[i++] & 0xFF) << 8 | (in[i++] & 0xFF) << 16
| in[i] << 24;
int X0 = (in[i++] & 0xFF)
| (in[i++] & 0xFF) << 8
| (in[i++] & 0xFF) << 16
| in[i++] << 24;
int X1 = (in[i++] & 0xFF)
| (in[i++] & 0xFF) << 8
| (in[i++] & 0xFF) << 16
| in[i++] << 24;
int X2 = (in[i++] & 0xFF)
| (in[i++] & 0xFF) << 8
| (in[i++] & 0xFF) << 16
| in[i++] << 24;
int X3 = (in[i++] & 0xFF)
| (in[i++] & 0xFF) << 8
| (in[i++] & 0xFF) << 16
| in[i++] << 24;
int X4 = (in[i++] & 0xFF)
| (in[i++] & 0xFF) << 8
| (in[i++] & 0xFF) << 16
| in[i++] << 24;
int X5 = (in[i++] & 0xFF)
| (in[i++] & 0xFF) << 8
| (in[i++] & 0xFF) << 16
| in[i++] << 24;
int X6 = (in[i++] & 0xFF)
| (in[i++] & 0xFF) << 8
| (in[i++] & 0xFF) << 16
| in[i++] << 24;
int X7 = (in[i++] & 0xFF)
| (in[i++] & 0xFF) << 8
| (in[i++] & 0xFF) << 16
| in[i++] << 24;
int X8 = (in[i++] & 0xFF)
| (in[i++] & 0xFF) << 8
| (in[i++] & 0xFF) << 16
| in[i++] << 24;
int X9 = (in[i++] & 0xFF)
| (in[i++] & 0xFF) << 8
| (in[i++] & 0xFF) << 16
| in[i++] << 24;
int X10 = (in[i++] & 0xFF)
| (in[i++] & 0xFF) << 8
| (in[i++] & 0xFF) << 16
| in[i++] << 24;
int X11 = (in[i++] & 0xFF)
| (in[i++] & 0xFF) << 8
| (in[i++] & 0xFF) << 16
| in[i++] << 24;
int X12 = (in[i++] & 0xFF)
| (in[i++] & 0xFF) << 8
| (in[i++] & 0xFF) << 16
| in[i++] << 24;
int X13 = (in[i++] & 0xFF)
| (in[i++] & 0xFF) << 8
| (in[i++] & 0xFF) << 16
| in[i++] << 24;
int X14 = (in[i++] & 0xFF)
| (in[i++] & 0xFF) << 8
| (in[i++] & 0xFF) << 16
| in[i++] << 24;
int X15 = (in[i++] & 0xFF)
| (in[i++] & 0xFF) << 8
| (in[i++] & 0xFF) << 16
| in[i] << 24;
int A = h0;
int B = h1;
int C = h2;
int D = h3;
// hex constants are from md5.c in FSF Gnu Privacy Guard 0.9.2
// round 1
A += ((B & C) | (~B & D)) + X0 + 0xD76AA478;
@ -310,39 +323,31 @@ public class MD5 extends BaseHash
protected byte[] padBuffer()
{
int n = (int) (count % BLOCK_SIZE);
int n = (int)(count % BLOCK_SIZE);
int padding = (n < 56) ? (56 - n) : (120 - n);
byte[] result = new byte[padding + 8];
// padding is always binary 1 followed by binary 0s
result[0] = (byte) 0x80;
// save number of bits, casting the long to an array of 8 bytes
long bits = count << 3;
result[padding++] = (byte) bits;
result[padding++] = (byte) (bits >>> 8);
result[padding++] = (byte) (bits >>> 16);
result[padding++] = (byte) (bits >>> 24);
result[padding++] = (byte) (bits >>> 32);
result[padding++] = (byte) (bits >>> 40);
result[padding++] = (byte) (bits >>> 48);
result[padding] = (byte) (bits >>> 56);
result[padding++] = (byte)(bits >>> 8);
result[padding++] = (byte)(bits >>> 16);
result[padding++] = (byte)(bits >>> 24);
result[padding++] = (byte)(bits >>> 32);
result[padding++] = (byte)(bits >>> 40);
result[padding++] = (byte)(bits >>> 48);
result[padding ] = (byte)(bits >>> 56);
return result;
}
protected byte[] getResult()
{
byte[] result = new byte[] { (byte) h0, (byte) (h0 >>> 8),
(byte) (h0 >>> 16), (byte) (h0 >>> 24),
(byte) h1, (byte) (h1 >>> 8),
(byte) (h1 >>> 16), (byte) (h1 >>> 24),
(byte) h2, (byte) (h2 >>> 8),
(byte) (h2 >>> 16), (byte) (h2 >>> 24),
(byte) h3, (byte) (h3 >>> 8),
(byte) (h3 >>> 16), (byte) (h3 >>> 24) };
return result;
return new byte[] {
(byte) h0, (byte)(h0 >>> 8), (byte)(h0 >>> 16), (byte)(h0 >>> 24),
(byte) h1, (byte)(h1 >>> 8), (byte)(h1 >>> 16), (byte)(h1 >>> 24),
(byte) h2, (byte)(h2 >>> 8), (byte)(h2 >>> 16), (byte)(h2 >>> 24),
(byte) h3, (byte)(h3 >>> 8), (byte)(h3 >>> 16), (byte)(h3 >>> 24) };
}
protected void resetContext()
@ -358,7 +363,8 @@ public class MD5 extends BaseHash
{
if (valid == null)
{
valid = Boolean.valueOf(DIGEST0.equals(Util.toString(new MD5().digest())));
String d = Util.toString(new MD5().digest());
valid = Boolean.valueOf(DIGEST0.equals(d));
}
return valid.booleanValue();
}

View file

@ -42,54 +42,48 @@ import gnu.java.security.Registry;
import gnu.java.security.util.Util;
/**
* <p>RIPEMD-128 is a 128-bit message digest.</p>
*
* <p>References:</p>
*
* RIPEMD-128 is a 128-bit message digest.
* <p>
* References:
* <ol>
* <li><a href="http://www.esat.kuleuven.ac.be/~bosselae/ripemd160.html">
* RIPEMD160</a>: A Strengthened Version of RIPEMD.<br>
* Hans Dobbertin, Antoon Bosselaers and Bart Preneel.</li>
* </ol>
*/
public class RipeMD128 extends BaseHash
public class RipeMD128
extends BaseHash
{
// Constants and variables
// -------------------------------------------------------------------------
private static final int BLOCK_SIZE = 64; // inner block size in bytes
private static final String DIGEST0 = "CDF26213A150DC3ECB610F18F6B38B46";
/** Constants for the transform method. */
// selection of message word
private static final int[] R = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
13, 14, 15, 7, 4, 13, 1, 10, 6, 15, 3, 12, 0,
9, 5, 2, 14, 11, 8, 3, 10, 14, 4, 9, 15, 8,
1, 2, 7, 0, 6, 13, 11, 5, 12, 1, 9, 11, 10,
0, 8, 12, 4, 13, 3, 7, 15, 14, 5, 6, 2 };
private static final int[] R = {
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
7, 4, 13, 1, 10, 6, 15, 3, 12, 0, 9, 5, 2, 14, 11, 8,
3, 10, 14, 4, 9, 15, 8, 1, 2, 7, 0, 6, 13, 11, 5, 12,
1, 9, 11, 10, 0, 8, 12, 4, 13, 3, 7, 15, 14, 5, 6, 2 };
private static final int[] Rp = { 5, 14, 7, 0, 9, 2, 11, 4, 13, 6, 15, 8, 1,
10, 3, 12, 6, 11, 3, 7, 0, 13, 5, 10, 14,
15, 8, 12, 4, 9, 1, 2, 15, 5, 1, 3, 7, 14,
6, 9, 11, 8, 12, 2, 10, 0, 4, 13, 8, 6, 4,
1, 3, 11, 15, 0, 5, 12, 2, 13, 9, 7, 10, 14 };
private static final int[] Rp = {
5, 14, 7, 0, 9, 2, 11, 4, 13, 6, 15, 8, 1, 10, 3, 12,
6, 11, 3, 7, 0, 13, 5, 10, 14, 15, 8, 12, 4, 9, 1, 2,
15, 5, 1, 3, 7, 14, 6, 9, 11, 8, 12, 2, 10, 0, 4, 13,
8, 6, 4, 1, 3, 11, 15, 0, 5, 12, 2, 13, 9, 7, 10, 14 };
// amount for rotate left (rol)
private static final int[] S = { 11, 14, 15, 12, 5, 8, 7, 9, 11, 13, 14, 15,
6, 7, 9, 8, 7, 6, 8, 13, 11, 9, 7, 15, 7, 12,
15, 9, 11, 7, 13, 12, 11, 13, 6, 7, 14, 9,
13, 15, 14, 8, 13, 6, 5, 12, 7, 5, 11, 12,
14, 15, 14, 15, 9, 8, 9, 14, 5, 6, 8, 6, 5,
12 };
private static final int[] S = {
11, 14, 15, 12, 5, 8, 7, 9, 11, 13, 14, 15, 6, 7, 9, 8,
7, 6, 8, 13, 11, 9, 7, 15, 7, 12, 15, 9, 11, 7, 13, 12,
11, 13, 6, 7, 14, 9, 13, 15, 14, 8, 13, 6, 5, 12, 7, 5,
11, 12, 14, 15, 14, 15, 9, 8, 9, 14, 5, 6, 8, 6, 5, 12 };
private static final int[] Sp = { 8, 9, 9, 11, 13, 15, 15, 5, 7, 7, 8, 11,
14, 14, 12, 6, 9, 13, 15, 7, 12, 8, 9, 11,
7, 7, 12, 7, 6, 15, 13, 11, 9, 7, 15, 11, 8,
6, 6, 14, 12, 13, 5, 14, 13, 13, 7, 5, 15,
5, 8, 11, 14, 14, 6, 14, 6, 9, 12, 9, 12, 5,
15, 8 };
private static final int[] Sp = {
8, 9, 9, 11, 13, 15, 15, 5, 7, 7, 8, 11, 14, 14, 12, 6,
9, 13, 15, 7, 12, 8, 9, 11, 7, 7, 12, 7, 6, 15, 13, 11,
9, 7, 15, 11, 8, 6, 6, 14, 12, 13, 5, 14, 13, 13, 7, 5,
15, 5, 8, 11, 14, 14, 6, 14, 6, 9, 12, 9, 12, 5, 15, 8 };
/** caches the result of the correctness test, once executed. */
private static Boolean valid;
@ -100,9 +94,6 @@ public class RipeMD128 extends BaseHash
/** 512 bits work buffer = 16 x 32-bit words */
private int[] X = new int[16];
// Constructor(s)
// -------------------------------------------------------------------------
/** Trivial 0-arguments constructor. */
public RipeMD128()
{
@ -110,7 +101,7 @@ public class RipeMD128 extends BaseHash
}
/**
* <p>Private constructor for cloning purposes.</p>
* Private constructor for cloning purposes.
*
* @param md the instance to clone.
*/
@ -126,40 +117,26 @@ public class RipeMD128 extends BaseHash
this.buffer = (byte[]) md.buffer.clone();
}
// Class methods
// -------------------------------------------------------------------------
// Instance methods
// -------------------------------------------------------------------------
// java.lang.Cloneable interface implementation ----------------------------
public Object clone()
{
return new RipeMD128(this);
}
// Implementation of concrete methods in BaseHash --------------------------
protected void transform(byte[] in, int offset)
{
int A, B, C, D, Ap, Bp, Cp, Dp, T, s, i;
// encode 64 bytes from input block into an array of 16 unsigned
// integers.
// encode 64 bytes from input block into an array of 16 unsigned integers.
for (i = 0; i < 16; i++)
{
X[i] = (in[offset++] & 0xFF) | (in[offset++] & 0xFF) << 8
| (in[offset++] & 0xFF) << 16 | in[offset++] << 24;
}
X[i] = (in[offset++] & 0xFF)
| (in[offset++] & 0xFF) << 8
| (in[offset++] & 0xFF) << 16
| in[offset++] << 24;
A = Ap = h0;
B = Bp = h1;
C = Cp = h2;
D = Dp = h3;
for (i = 0; i < 16; i++)
{ // rounds 0...15
for (i = 0; i < 16; i++) // rounds 0...15
{
s = S[i];
T = A + (B ^ C ^ D) + X[i];
A = D;
@ -174,9 +151,8 @@ public class RipeMD128 extends BaseHash
Cp = Bp;
Bp = T << s | T >>> (32 - s);
}
for (; i < 32; i++)
{ // rounds 16...31
for (; i < 32; i++) // rounds 16...31
{
s = S[i];
T = A + ((B & C) | (~B & D)) + X[R[i]] + 0x5A827999;
A = D;
@ -191,9 +167,8 @@ public class RipeMD128 extends BaseHash
Cp = Bp;
Bp = T << s | T >>> (32 - s);
}
for (; i < 48; i++)
{ // rounds 32...47
for (; i < 48; i++) // rounds 32...47
{
s = S[i];
T = A + ((B | ~C) ^ D) + X[R[i]] + 0x6ED9EBA1;
A = D;
@ -208,9 +183,8 @@ public class RipeMD128 extends BaseHash
Cp = Bp;
Bp = T << s | T >>> (32 - s);
}
for (; i < 64; i++)
{ // rounds 48...63
for (; i < 64; i++) // rounds 48...63
{
s = S[i];
T = A + ((B & D) | (C & ~D)) + X[R[i]] + 0x8F1BBCDC;
A = D;
@ -225,7 +199,6 @@ public class RipeMD128 extends BaseHash
Cp = Bp;
Bp = T << s | T >>> (32 - s);
}
T = h1 + C + Dp;
h1 = h2 + D + Ap;
h2 = h3 + A + Bp;
@ -235,39 +208,32 @@ public class RipeMD128 extends BaseHash
protected byte[] padBuffer()
{
int n = (int) (count % BLOCK_SIZE);
int n = (int)(count % BLOCK_SIZE);
int padding = (n < 56) ? (56 - n) : (120 - n);
byte[] result = new byte[padding + 8];
// padding is always binary 1 followed by binary 0s
result[0] = (byte) 0x80;
// save number of bits, casting the long to an array of 8 bytes
long bits = count << 3;
result[padding++] = (byte) bits;
result[padding++] = (byte) (bits >>> 8);
result[padding++] = (byte) (bits >>> 16);
result[padding++] = (byte) (bits >>> 24);
result[padding++] = (byte) (bits >>> 32);
result[padding++] = (byte) (bits >>> 40);
result[padding++] = (byte) (bits >>> 48);
result[padding] = (byte) (bits >>> 56);
result[padding++] = (byte)(bits >>> 8);
result[padding++] = (byte)(bits >>> 16);
result[padding++] = (byte)(bits >>> 24);
result[padding++] = (byte)(bits >>> 32);
result[padding++] = (byte)(bits >>> 40);
result[padding++] = (byte)(bits >>> 48);
result[padding ] = (byte)(bits >>> 56);
return result;
}
protected byte[] getResult()
{
byte[] result = new byte[] { (byte) h0, (byte) (h0 >>> 8),
(byte) (h0 >>> 16), (byte) (h0 >>> 24),
(byte) h1, (byte) (h1 >>> 8),
(byte) (h1 >>> 16), (byte) (h1 >>> 24),
(byte) h2, (byte) (h2 >>> 8),
(byte) (h2 >>> 16), (byte) (h2 >>> 24),
(byte) h3, (byte) (h3 >>> 8),
(byte) (h3 >>> 16), (byte) (h3 >>> 24) };
return result;
return new byte[] {
(byte) h0, (byte)(h0 >>> 8), (byte)(h0 >>> 16), (byte)(h0 >>> 24),
(byte) h1, (byte)(h1 >>> 8), (byte)(h1 >>> 16), (byte)(h1 >>> 24),
(byte) h2, (byte)(h2 >>> 8), (byte)(h2 >>> 16), (byte)(h2 >>> 24),
(byte) h3, (byte)(h3 >>> 8), (byte)(h3 >>> 16), (byte)(h3 >>> 24)
};
}
protected void resetContext()
@ -283,8 +249,8 @@ public class RipeMD128 extends BaseHash
{
if (valid == null)
{
valid = Boolean.valueOf
(DIGEST0.equals(Util.toString(new RipeMD128().digest())));
String d = Util.toString(new RipeMD128().digest());
valid = Boolean.valueOf(DIGEST0.equals(d));
}
return valid.booleanValue();
}

View file

@ -42,59 +42,51 @@ import gnu.java.security.Registry;
import gnu.java.security.util.Util;
/**
* <p>RIPEMD-160 is a 160-bit message digest.</p>
*
* <p>References:</p>
*
* RIPEMD-160 is a 160-bit message digest.
* <p>
* References:
* <ol>
* <li><a href="http://www.esat.kuleuven.ac.be/~bosselae/ripemd160.html">
* RIPEMD160</a>: A Strengthened Version of RIPEMD.<br>
* Hans Dobbertin, Antoon Bosselaers and Bart Preneel.</li>
* </ol>
*/
public class RipeMD160 extends BaseHash
public class RipeMD160
extends BaseHash
{
// Constants and variables
// -------------------------------------------------------------------------
private static final int BLOCK_SIZE = 64; // inner block size in bytes
private static final String DIGEST0 = "9C1185A5C5E9FC54612808977EE8F548B2258D31";
// selection of message word
private static final int[] R = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
13, 14, 15, 7, 4, 13, 1, 10, 6, 15, 3, 12, 0,
9, 5, 2, 14, 11, 8, 3, 10, 14, 4, 9, 15, 8,
1, 2, 7, 0, 6, 13, 11, 5, 12, 1, 9, 11, 10,
0, 8, 12, 4, 13, 3, 7, 15, 14, 5, 6, 2, 4, 0,
5, 9, 7, 12, 2, 10, 14, 1, 3, 8, 11, 6, 15,
13 };
private static final int[] R = {
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
7, 4, 13, 1, 10, 6, 15, 3, 12, 0, 9, 5, 2, 14, 11, 8,
3, 10, 14, 4, 9, 15, 8, 1, 2, 7, 0, 6, 13, 11, 5, 12,
1, 9, 11, 10, 0, 8, 12, 4, 13, 3, 7, 15, 14, 5, 6, 2,
4, 0, 5, 9, 7, 12, 2, 10, 14, 1, 3, 8, 11, 6, 15, 13 };
private static final int[] Rp = { 5, 14, 7, 0, 9, 2, 11, 4, 13, 6, 15, 8, 1,
10, 3, 12, 6, 11, 3, 7, 0, 13, 5, 10, 14,
15, 8, 12, 4, 9, 1, 2, 15, 5, 1, 3, 7, 14,
6, 9, 11, 8, 12, 2, 10, 0, 4, 13, 8, 6, 4,
1, 3, 11, 15, 0, 5, 12, 2, 13, 9, 7, 10, 14,
12, 15, 10, 4, 1, 5, 8, 7, 6, 2, 13, 14, 0,
3, 9, 11 };
private static final int[] Rp = {
5, 14, 7, 0, 9, 2, 11, 4, 13, 6, 15, 8, 1, 10, 3, 12,
6, 11, 3, 7, 0, 13, 5, 10, 14, 15, 8, 12, 4, 9, 1, 2,
15, 5, 1, 3, 7, 14, 6, 9, 11, 8, 12, 2, 10, 0, 4, 13,
8, 6, 4, 1, 3, 11, 15, 0, 5, 12, 2, 13, 9, 7, 10, 14,
12, 15, 10, 4, 1, 5, 8, 7, 6, 2, 13, 14, 0, 3, 9, 11 };
// amount for rotate left (rol)
private static final int[] S = { 11, 14, 15, 12, 5, 8, 7, 9, 11, 13, 14, 15,
6, 7, 9, 8, 7, 6, 8, 13, 11, 9, 7, 15, 7, 12,
15, 9, 11, 7, 13, 12, 11, 13, 6, 7, 14, 9,
13, 15, 14, 8, 13, 6, 5, 12, 7, 5, 11, 12,
14, 15, 14, 15, 9, 8, 9, 14, 5, 6, 8, 6, 5,
12, 9, 15, 5, 11, 6, 8, 13, 12, 5, 12, 13,
14, 11, 8, 5, 6 };
private static final int[] S = {
11, 14, 15, 12, 5, 8, 7, 9, 11, 13, 14, 15, 6, 7, 9, 8,
7, 6, 8, 13, 11, 9, 7, 15, 7, 12, 15, 9, 11, 7, 13, 12,
11, 13, 6, 7, 14, 9, 13, 15, 14, 8, 13, 6, 5, 12, 7, 5,
11, 12, 14, 15, 14, 15, 9, 8, 9, 14, 5, 6, 8, 6, 5, 12,
9, 15, 5, 11, 6, 8, 13, 12, 5, 12, 13, 14, 11, 8, 5, 6 };
private static final int[] Sp = { 8, 9, 9, 11, 13, 15, 15, 5, 7, 7, 8, 11,
14, 14, 12, 6, 9, 13, 15, 7, 12, 8, 9, 11,
7, 7, 12, 7, 6, 15, 13, 11, 9, 7, 15, 11, 8,
6, 6, 14, 12, 13, 5, 14, 13, 13, 7, 5, 15,
5, 8, 11, 14, 14, 6, 14, 6, 9, 12, 9, 12, 5,
15, 8, 8, 5, 12, 9, 12, 5, 14, 6, 8, 13, 6,
5, 15, 13, 11, 11 };
private static final int[] Sp = {
8, 9, 9, 11, 13, 15, 15, 5, 7, 7, 8, 11, 14, 14, 12, 6,
9, 13, 15, 7, 12, 8, 9, 11, 7, 7, 12, 7, 6, 15, 13, 11,
9, 7, 15, 11, 8, 6, 6, 14, 12, 13, 5, 14, 13, 13, 7, 5,
15, 5, 8, 11, 14, 14, 6, 14, 6, 9, 12, 9, 12, 5, 15, 8,
8, 5, 12, 9, 12, 5, 14, 6, 8, 13, 6, 5, 15, 13, 11, 11 };
/** caches the result of the correctness test, once executed. */
private static Boolean valid;
@ -105,9 +97,6 @@ public class RipeMD160 extends BaseHash
/** 512 bits work buffer = 16 x 32-bit words */
private int[] X = new int[16];
// Constructor(s)
// -------------------------------------------------------------------------
/** Trivial 0-arguments constructor. */
public RipeMD160()
{
@ -115,7 +104,7 @@ public class RipeMD160 extends BaseHash
}
/**
* <p>Private constructor for cloning purposes.</p>
* Private constructor for cloning purposes.
*
* @param md the instance to clone.
*/
@ -132,40 +121,27 @@ public class RipeMD160 extends BaseHash
this.buffer = (byte[]) md.buffer.clone();
}
// Class methods
// -------------------------------------------------------------------------
// Instance methods
// -------------------------------------------------------------------------
// java.lang.Cloneable interface implementation ----------------------------
public Object clone()
{
return (new RipeMD160(this));
}
// Implementation of concrete methods in BaseHash --------------------------
protected void transform(byte[] in, int offset)
{
int A, B, C, D, E, Ap, Bp, Cp, Dp, Ep, T, s, i;
// encode 64 bytes from input block into an array of 16 unsigned integers
for (i = 0; i < 16; i++)
{
X[i] = (in[offset++] & 0xFF) | (in[offset++] & 0xFF) << 8
| (in[offset++] & 0xFF) << 16 | in[offset++] << 24;
}
X[i] = (in[offset++] & 0xFF)
| (in[offset++] & 0xFF) << 8
| (in[offset++] & 0xFF) << 16
| in[offset++] << 24;
A = Ap = h0;
B = Bp = h1;
C = Cp = h2;
D = Dp = h3;
E = Ep = h4;
for (i = 0; i < 16; i++)
{ // rounds 0...15
for (i = 0; i < 16; i++) // rounds 0...15
{
s = S[i];
T = A + (B ^ C ^ D) + X[i];
A = E;
@ -182,9 +158,8 @@ public class RipeMD160 extends BaseHash
Cp = Bp;
Bp = (T << s | T >>> (32 - s)) + Ap;
}
for (; i < 32; i++)
{ // rounds 16...31
for (; i < 32; i++) // rounds 16...31
{
s = S[i];
T = A + ((B & C) | (~B & D)) + X[R[i]] + 0x5A827999;
A = E;
@ -201,9 +176,8 @@ public class RipeMD160 extends BaseHash
Cp = Bp;
Bp = (T << s | T >>> (32 - s)) + Ap;
}
for (; i < 48; i++)
{ // rounds 32...47
for (; i < 48; i++) // rounds 32...47
{
s = S[i];
T = A + ((B | ~C) ^ D) + X[R[i]] + 0x6ED9EBA1;
A = E;
@ -220,9 +194,8 @@ public class RipeMD160 extends BaseHash
Cp = Bp;
Bp = (T << s | T >>> (32 - s)) + Ap;
}
for (; i < 64; i++)
{ // rounds 48...63
for (; i < 64; i++) // rounds 48...63
{
s = S[i];
T = A + ((B & D) | (C & ~D)) + X[R[i]] + 0x8F1BBCDC;
A = E;
@ -239,9 +212,8 @@ public class RipeMD160 extends BaseHash
Cp = Bp;
Bp = (T << s | T >>> (32 - s)) + Ap;
}
for (; i < 80; i++)
{ // rounds 64...79
for (; i < 80; i++) // rounds 64...79
{
s = S[i];
T = A + (B ^ (C | ~D)) + X[R[i]] + 0xA953FD4E;
A = E;
@ -258,7 +230,6 @@ public class RipeMD160 extends BaseHash
Cp = Bp;
Bp = (T << s | T >>> (32 - s)) + Ap;
}
T = h1 + C + Dp;
h1 = h2 + D + Ep;
h2 = h3 + E + Ap;
@ -269,41 +240,33 @@ public class RipeMD160 extends BaseHash
protected byte[] padBuffer()
{
int n = (int) (count % BLOCK_SIZE);
int n = (int)(count % BLOCK_SIZE);
int padding = (n < 56) ? (56 - n) : (120 - n);
byte[] result = new byte[padding + 8];
// padding is always binary 1 followed by binary 0s
result[0] = (byte) 0x80;
// save number of bits, casting the long to an array of 8 bytes
long bits = count << 3;
result[padding++] = (byte) bits;
result[padding++] = (byte) (bits >>> 8);
result[padding++] = (byte) (bits >>> 16);
result[padding++] = (byte) (bits >>> 24);
result[padding++] = (byte) (bits >>> 32);
result[padding++] = (byte) (bits >>> 40);
result[padding++] = (byte) (bits >>> 48);
result[padding] = (byte) (bits >>> 56);
result[padding++] = (byte)(bits >>> 8);
result[padding++] = (byte)(bits >>> 16);
result[padding++] = (byte)(bits >>> 24);
result[padding++] = (byte)(bits >>> 32);
result[padding++] = (byte)(bits >>> 40);
result[padding++] = (byte)(bits >>> 48);
result[padding ] = (byte)(bits >>> 56);
return result;
}
protected byte[] getResult()
{
byte[] result = new byte[] { (byte) h0, (byte) (h0 >>> 8),
(byte) (h0 >>> 16), (byte) (h0 >>> 24),
(byte) h1, (byte) (h1 >>> 8),
(byte) (h1 >>> 16), (byte) (h1 >>> 24),
(byte) h2, (byte) (h2 >>> 8),
(byte) (h2 >>> 16), (byte) (h2 >>> 24),
(byte) h3, (byte) (h3 >>> 8),
(byte) (h3 >>> 16), (byte) (h3 >>> 24),
(byte) h4, (byte) (h4 >>> 8),
(byte) (h4 >>> 16), (byte) (h4 >>> 24) };
return result;
return new byte[] {
(byte) h0, (byte)(h0 >>> 8), (byte)(h0 >>> 16), (byte)(h0 >>> 24),
(byte) h1, (byte)(h1 >>> 8), (byte)(h1 >>> 16), (byte)(h1 >>> 24),
(byte) h2, (byte)(h2 >>> 8), (byte)(h2 >>> 16), (byte)(h2 >>> 24),
(byte) h3, (byte)(h3 >>> 8), (byte)(h3 >>> 16), (byte)(h3 >>> 24),
(byte) h4, (byte)(h4 >>> 8), (byte)(h4 >>> 16), (byte)(h4 >>> 24)
};
}
protected void resetContext()
@ -320,8 +283,8 @@ public class RipeMD160 extends BaseHash
{
if (valid == null)
{
valid = Boolean.valueOf
(DIGEST0.equals(Util.toString(new RipeMD160().digest())));
String d = Util.toString(new RipeMD160().digest());
valid = Boolean.valueOf(DIGEST0.equals(d));
}
return valid.booleanValue();
}

View file

@ -42,7 +42,7 @@ import gnu.java.security.Registry;
import gnu.java.security.util.Util;
/**
* <p>The Secure Hash Algorithm (SHA-1) is required for use with the Digital
* The Secure Hash Algorithm (SHA-1) is required for use with the Digital
* Signature Algorithm (DSA) as specified in the Digital Signature Standard
* (DSS) and whenever a secure hash algorithm is required for federal
* applications. For a message of length less than 2^64 bits, the SHA-1
@ -51,15 +51,14 @@ import gnu.java.security.util.Util;
* message. The SHA-1 is also used to compute a message digest for the received
* version of the message during the process of verifying the signature. Any
* change to the message in transit will, with very high probability, result in
* a different message digest, and the signature will fail to verify.</p>
*
* <p>The SHA-1 is designed to have the following properties: it is
* a different message digest, and the signature will fail to verify.
* <p>
* The SHA-1 is designed to have the following properties: it is
* computationally infeasible to find a message which corresponds to a given
* message digest, or to find two different messages which produce the same
* message digest.</p>
*
* <p>References:</p>
*
* message digest.
* <p>
* References:
* <ol>
* <li><a href="http://www.itl.nist.gov/fipspubs/fip180-1.htm">SECURE HASH
* STANDARD</a><br>
@ -67,12 +66,9 @@ import gnu.java.security.util.Util;
* </li>
* </ol>
*/
public class Sha160 extends BaseHash
public class Sha160
extends BaseHash
{
// Constants and variables
// -------------------------------------------------------------------------
private static final int BLOCK_SIZE = 64; // inner block size in bytes
private static final String DIGEST0 = "A9993E364706816ABA3E25717850C26C9CD0D89D";
@ -85,9 +81,6 @@ public class Sha160 extends BaseHash
/** 160-bit interim result. */
private int h0, h1, h2, h3, h4;
// Constructor(s)
// -------------------------------------------------------------------------
/** Trivial 0-arguments constructor. */
public Sha160()
{
@ -95,7 +88,7 @@ public class Sha160 extends BaseHash
}
/**
* <p>Private constructor for cloning purposes.</p>
* Private constructor for cloning purposes.
*
* @param md the instance to clone.
*/
@ -112,58 +105,20 @@ public class Sha160 extends BaseHash
this.buffer = (byte[]) md.buffer.clone();
}
// Class methods
// -------------------------------------------------------------------------
public static final int[] G(int hh0, int hh1, int hh2, int hh3, int hh4,
byte[] in, int offset)
{
// int[] w = new int[80];
// int i, T;
// for (i = 0; i < 16; i++) {
// w[i] = in[offset++] << 24 |
// (in[offset++] & 0xFF) << 16 |
// (in[offset++] & 0xFF) << 8 |
// (in[offset++] & 0xFF);
// }
// for (i = 16; i < 80; i++) {
// T = w[i-3] ^ w[i-8] ^ w[i-14] ^ w[i-16];
// w[i] = T << 1 | T >>> 31;
// }
// return sha(hh0, hh1, hh2, hh3, hh4, in, offset, w);
return sha(hh0, hh1, hh2, hh3, hh4, in, offset);
}
// Instance methods
// -------------------------------------------------------------------------
// java.lang.Cloneable interface implementation ----------------------------
public Object clone()
{
return new Sha160(this);
}
// Implementation of concrete methods in BaseHash --------------------------
protected void transform(byte[] in, int offset)
{
// int i, T;
// for (i = 0; i < 16; i++) {
// W[i] = in[offset++] << 24 |
// (in[offset++] & 0xFF) << 16 |
// (in[offset++] & 0xFF) << 8 |
// (in[offset++] & 0xFF);
// }
// for (i = 16; i < 80; i++) {
// T = W[i-3] ^ W[i-8] ^ W[i-14] ^ W[i-16];
// W[i] = T << 1 | T >>> 31;
// }
// int[] result = sha(h0, h1, h2, h3, h4, in, offset, W);
int[] result = sha(h0, h1, h2, h3, h4, in, offset);
h0 = result[0];
h1 = result[1];
h2 = result[2];
@ -173,41 +128,32 @@ public class Sha160 extends BaseHash
protected byte[] padBuffer()
{
int n = (int) (count % BLOCK_SIZE);
int n = (int)(count % BLOCK_SIZE);
int padding = (n < 56) ? (56 - n) : (120 - n);
byte[] result = new byte[padding + 8];
// padding is always binary 1 followed by binary 0s
result[0] = (byte) 0x80;
// save number of bits, casting the long to an array of 8 bytes
long bits = count << 3;
result[padding++] = (byte) (bits >>> 56);
result[padding++] = (byte) (bits >>> 48);
result[padding++] = (byte) (bits >>> 40);
result[padding++] = (byte) (bits >>> 32);
result[padding++] = (byte) (bits >>> 24);
result[padding++] = (byte) (bits >>> 16);
result[padding++] = (byte) (bits >>> 8);
result[padding] = (byte) bits;
result[padding++] = (byte)(bits >>> 56);
result[padding++] = (byte)(bits >>> 48);
result[padding++] = (byte)(bits >>> 40);
result[padding++] = (byte)(bits >>> 32);
result[padding++] = (byte)(bits >>> 24);
result[padding++] = (byte)(bits >>> 16);
result[padding++] = (byte)(bits >>> 8);
result[padding ] = (byte) bits;
return result;
}
protected byte[] getResult()
{
byte[] result = new byte[] { (byte) (h0 >>> 24), (byte) (h0 >>> 16),
(byte) (h0 >>> 8), (byte) h0,
(byte) (h1 >>> 24), (byte) (h1 >>> 16),
(byte) (h1 >>> 8), (byte) h1,
(byte) (h2 >>> 24), (byte) (h2 >>> 16),
(byte) (h2 >>> 8), (byte) h2,
(byte) (h3 >>> 24), (byte) (h3 >>> 16),
(byte) (h3 >>> 8), (byte) h3,
(byte) (h4 >>> 24), (byte) (h4 >>> 16),
(byte) (h4 >>> 8), (byte) h4 };
return result;
return new byte[] {
(byte)(h0 >>> 24), (byte)(h0 >>> 16), (byte)(h0 >>> 8), (byte) h0,
(byte)(h1 >>> 24), (byte)(h1 >>> 16), (byte)(h1 >>> 8), (byte) h1,
(byte)(h2 >>> 24), (byte)(h2 >>> 16), (byte)(h2 >>> 8), (byte) h2,
(byte)(h3 >>> 24), (byte)(h3 >>> 16), (byte)(h3 >>> 8), (byte) h3,
(byte)(h4 >>> 24), (byte)(h4 >>> 16), (byte)(h4 >>> 8), (byte) h4 };
}
protected void resetContext()
@ -234,11 +180,9 @@ public class Sha160 extends BaseHash
return valid.booleanValue();
}
// SHA specific methods ----------------------------------------------------
private static final synchronized int[]
// sha(int hh0, int hh1, int hh2, int hh3, int hh4, byte[] in, int offset, int[] w) {
sha(int hh0, int hh1, int hh2, int hh3, int hh4, byte[] in, int offset)
private static synchronized final int[] sha(int hh0, int hh1, int hh2,
int hh3, int hh4, byte[] in,
int offset)
{
int A = hh0;
int B = hh1;
@ -246,20 +190,17 @@ public class Sha160 extends BaseHash
int D = hh3;
int E = hh4;
int r, T;
for (r = 0; r < 16; r++)
{
w[r] = in[offset++] << 24 | (in[offset++] & 0xFF) << 16
| (in[offset++] & 0xFF) << 8 | (in[offset++] & 0xFF);
}
w[r] = in[offset++] << 24
| (in[offset++] & 0xFF) << 16
| (in[offset++] & 0xFF) << 8
| (in[offset++] & 0xFF);
for (r = 16; r < 80; r++)
{
T = w[r - 3] ^ w[r - 8] ^ w[r - 14] ^ w[r - 16];
w[r] = T << 1 | T >>> 31;
}
// rounds 0-19
for (r = 0; r < 20; r++)
for (r = 0; r < 20; r++) // rounds 0-19
{
T = (A << 5 | A >>> 27) + ((B & C) | (~B & D)) + E + w[r] + 0x5A827999;
E = D;
@ -268,9 +209,7 @@ public class Sha160 extends BaseHash
B = A;
A = T;
}
// rounds 20-39
for (r = 20; r < 40; r++)
for (r = 20; r < 40; r++) // rounds 20-39
{
T = (A << 5 | A >>> 27) + (B ^ C ^ D) + E + w[r] + 0x6ED9EBA1;
E = D;
@ -279,21 +218,16 @@ public class Sha160 extends BaseHash
B = A;
A = T;
}
// rounds 40-59
for (r = 40; r < 60; r++)
for (r = 40; r < 60; r++) // rounds 40-59
{
T = (A << 5 | A >>> 27) + (B & C | B & D | C & D) + E + w[r]
+ 0x8F1BBCDC;
T = (A << 5 | A >>> 27) + (B & C | B & D | C & D) + E + w[r] + 0x8F1BBCDC;
E = D;
D = C;
C = B << 30 | B >>> 2;
B = A;
A = T;
}
// rounds 60-79
for (r = 60; r < 80; r++)
for (r = 60; r < 80; r++) // rounds 60-79
{
T = (A << 5 | A >>> 27) + (B ^ C ^ D) + E + w[r] + 0xCA62C1D6;
E = D;
@ -302,7 +236,6 @@ public class Sha160 extends BaseHash
B = A;
A = T;
}
return new int[] { hh0 + A, hh1 + B, hh2 + C, hh3 + D, hh4 + E };
}
}

View file

@ -42,46 +42,41 @@ import gnu.java.security.Registry;
import gnu.java.security.util.Util;
/**
* <p>Implementation of SHA2-1 [SHA-256] per the IETF Draft Specification.</p>
*
* <p>References:</p>
* Implementation of SHA2-1 [SHA-256] per the IETF Draft Specification.
* <p>
* References:
* <ol>
* <li><a href="http://ftp.ipv4.heanet.ie/pub/ietf/internet-drafts/draft-ietf-ipsec-ciph-aes-cbc-03.txt">
* Descriptions of SHA-256, SHA-384, and SHA-512</a>,</li>
* <li>http://csrc.nist.gov/cryptval/shs/sha256-384-512.pdf</li>
* </ol>
*/
public class Sha256 extends BaseHash
public class Sha256
extends BaseHash
{
// Constants and variables
// -------------------------------------------------------------------------
private static final int[] k = { 0x428a2f98, 0x71374491, 0xb5c0fbcf,
0xe9b5dba5, 0x3956c25b, 0x59f111f1,
0x923f82a4, 0xab1c5ed5, 0xd807aa98,
0x12835b01, 0x243185be, 0x550c7dc3,
0x72be5d74, 0x80deb1fe, 0x9bdc06a7,
0xc19bf174, 0xe49b69c1, 0xefbe4786,
0x0fc19dc6, 0x240ca1cc, 0x2de92c6f,
0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
0x983e5152, 0xa831c66d, 0xb00327c8,
0xbf597fc7, 0xc6e00bf3, 0xd5a79147,
0x06ca6351, 0x14292967, 0x27b70a85,
0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
0x650a7354, 0x766a0abb, 0x81c2c92e,
0x92722c85, 0xa2bfe8a1, 0xa81a664b,
0xc24b8b70, 0xc76c51a3, 0xd192e819,
0xd6990624, 0xf40e3585, 0x106aa070,
0x19a4c116, 0x1e376c08, 0x2748774c,
0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a,
0x5b9cca4f, 0x682e6ff3, 0x748f82ee,
0x78a5636f, 0x84c87814, 0x8cc70208,
0x90befffa, 0xa4506ceb, 0xbef9a3f7,
0xc67178f2 };
private static final int[] k = {
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
};
private static final int BLOCK_SIZE = 64; // inner block size in bytes
private static final String DIGEST0 = "BA7816BF8F01CFEA414140DE5DAE2223B00361A396177A9CB410FF61F20015AD";
private static final String DIGEST0 =
"BA7816BF8F01CFEA414140DE5DAE2223B00361A396177A9CB410FF61F20015AD";
private static final int[] w = new int[64];
@ -91,9 +86,6 @@ public class Sha256 extends BaseHash
/** 256-bit interim result. */
private int h0, h1, h2, h3, h4, h5, h6, h7;
// Constructor(s)
// -------------------------------------------------------------------------
/** Trivial 0-arguments constructor. */
public Sha256()
{
@ -101,7 +93,7 @@ public class Sha256 extends BaseHash
}
/**
* <p>Private constructor for cloning purposes.</p>
* Private constructor for cloning purposes.
*
* @param md the instance to clone.
*/
@ -121,31 +113,20 @@ public class Sha256 extends BaseHash
this.buffer = (byte[]) md.buffer.clone();
}
// Class methods
// -------------------------------------------------------------------------
public static final int[] G(int hh0, int hh1, int hh2, int hh3, int hh4,
int hh5, int hh6, int hh7, byte[] in, int offset)
{
return sha(hh0, hh1, hh2, hh3, hh4, hh5, hh6, hh7, in, offset);
}
// Instance methods
// -------------------------------------------------------------------------
// java.lang.Cloneable interface implementation ----------------------------
public Object clone()
{
return new Sha256(this);
}
// Implementation of concrete methods in BaseHash --------------------------
protected void transform(byte[] in, int offset)
{
int[] result = sha(h0, h1, h2, h3, h4, h5, h6, h7, in, offset);
h0 = result[0];
h1 = result[1];
h2 = result[2];
@ -158,41 +139,35 @@ public class Sha256 extends BaseHash
protected byte[] padBuffer()
{
int n = (int) (count % BLOCK_SIZE);
int n = (int)(count % BLOCK_SIZE);
int padding = (n < 56) ? (56 - n) : (120 - n);
byte[] result = new byte[padding + 8];
// padding is always binary 1 followed by binary 0s
result[0] = (byte) 0x80;
// save number of bits, casting the long to an array of 8 bytes
long bits = count << 3;
result[padding++] = (byte) (bits >>> 56);
result[padding++] = (byte) (bits >>> 48);
result[padding++] = (byte) (bits >>> 40);
result[padding++] = (byte) (bits >>> 32);
result[padding++] = (byte) (bits >>> 24);
result[padding++] = (byte) (bits >>> 16);
result[padding++] = (byte) (bits >>> 8);
result[padding] = (byte) bits;
result[padding++] = (byte)(bits >>> 56);
result[padding++] = (byte)(bits >>> 48);
result[padding++] = (byte)(bits >>> 40);
result[padding++] = (byte)(bits >>> 32);
result[padding++] = (byte)(bits >>> 24);
result[padding++] = (byte)(bits >>> 16);
result[padding++] = (byte)(bits >>> 8);
result[padding ] = (byte) bits;
return result;
}
protected byte[] getResult()
{
return new byte[] { (byte) (h0 >>> 24), (byte) (h0 >>> 16),
(byte) (h0 >>> 8), (byte) h0, (byte) (h1 >>> 24),
(byte) (h1 >>> 16), (byte) (h1 >>> 8), (byte) h1,
(byte) (h2 >>> 24), (byte) (h2 >>> 16),
(byte) (h2 >>> 8), (byte) h2, (byte) (h3 >>> 24),
(byte) (h3 >>> 16), (byte) (h3 >>> 8), (byte) h3,
(byte) (h4 >>> 24), (byte) (h4 >>> 16),
(byte) (h4 >>> 8), (byte) h4, (byte) (h5 >>> 24),
(byte) (h5 >>> 16), (byte) (h5 >>> 8), (byte) h5,
(byte) (h6 >>> 24), (byte) (h6 >>> 16),
(byte) (h6 >>> 8), (byte) h6, (byte) (h7 >>> 24),
(byte) (h7 >>> 16), (byte) (h7 >>> 8), (byte) h7 };
return new byte[] {
(byte)(h0 >>> 24), (byte)(h0 >>> 16), (byte)(h0 >>> 8), (byte) h0,
(byte)(h1 >>> 24), (byte)(h1 >>> 16), (byte)(h1 >>> 8), (byte) h1,
(byte)(h2 >>> 24), (byte)(h2 >>> 16), (byte)(h2 >>> 8), (byte) h2,
(byte)(h3 >>> 24), (byte)(h3 >>> 16), (byte)(h3 >>> 8), (byte) h3,
(byte)(h4 >>> 24), (byte)(h4 >>> 16), (byte)(h4 >>> 8), (byte) h4,
(byte)(h5 >>> 24), (byte)(h5 >>> 16), (byte)(h5 >>> 8), (byte) h5,
(byte)(h6 >>> 24), (byte)(h6 >>> 16), (byte)(h6 >>> 8), (byte) h6,
(byte)(h7 >>> 24), (byte)(h7 >>> 16), (byte)(h7 >>> 8), (byte) h7 };
}
protected void resetContext()
@ -219,13 +194,10 @@ public class Sha256 extends BaseHash
String result = Util.toString(md.digest());
valid = Boolean.valueOf(DIGEST0.equals(result));
}
return valid.booleanValue();
}
// SHA specific methods ----------------------------------------------------
private static final synchronized int[] sha(int hh0, int hh1, int hh2,
private static synchronized final int[] sha(int hh0, int hh1, int hh2,
int hh3, int hh4, int hh5,
int hh6, int hh7, byte[] in,
int offset)
@ -239,29 +211,31 @@ public class Sha256 extends BaseHash
int G = hh6;
int H = hh7;
int r, T, T2;
for (r = 0; r < 16; r++)
{
w[r] = (in[offset++] << 24 | (in[offset++] & 0xFF) << 16
| (in[offset++] & 0xFF) << 8 | (in[offset++] & 0xFF));
}
w[r] = (in[offset++] << 24
| (in[offset++] & 0xFF) << 16
| (in[offset++] & 0xFF) << 8
| (in[offset++] & 0xFF));
for (r = 16; r < 64; r++)
{
T = w[r - 2];
T = w[r - 2];
T2 = w[r - 15];
w[r] = ((((T >>> 17) | (T << 15)) ^ ((T >>> 19) | (T << 13)) ^ (T >>> 10))
+ w[r - 7]
+ (((T2 >>> 7) | (T2 << 25)) ^ ((T2 >>> 18) | (T2 << 14)) ^ (T2 >>> 3))
+ w[r - 16]);
+ (((T2 >>> 7) | (T2 << 25))
^ ((T2 >>> 18) | (T2 << 14))
^ (T2 >>> 3)) + w[r - 16]);
}
for (r = 0; r < 64; r++)
{
T = (H
+ (((E >>> 6) | (E << 26)) ^ ((E >>> 11) | (E << 21)) ^ ((E >>> 25) | (E << 7)))
+ (((E >>> 6) | (E << 26))
^ ((E >>> 11) | (E << 21))
^ ((E >>> 25) | (E << 7)))
+ ((E & F) ^ (~E & G)) + k[r] + w[r]);
T2 = ((((A >>> 2) | (A << 30)) ^ ((A >>> 13) | (A << 19)) ^ ((A >>> 22) | (A << 10)))
+ ((A & B) ^ (A & C) ^ (B & C)));
T2 = ((((A >>> 2) | (A << 30))
^ ((A >>> 13) | (A << 19))
^ ((A >>> 22) | (A << 10))) + ((A & B) ^ (A & C) ^ (B & C)));
H = G;
G = F;
F = E;
@ -271,8 +245,8 @@ public class Sha256 extends BaseHash
B = A;
A = T + T2;
}
return new int[] { hh0 + A, hh1 + B, hh2 + C, hh3 + D, hh4 + E, hh5 + F,
hh6 + G, hh7 + H };
return new int[] {
hh0 + A, hh1 + B, hh2 + C, hh3 + D,
hh4 + E, hh5 + F, hh6 + G, hh7 + H };
}
}

View file

@ -42,66 +42,52 @@ import gnu.java.security.Registry;
import gnu.java.security.util.Util;
/**
* <p>Implementation of SHA2-2 [SHA-384] per the IETF Draft Specification.</p>
*
* <p>References:</p>
* Implementation of SHA2-2 [SHA-384] per the IETF Draft Specification.
* <p>
* References:
* <ol>
* <li><a href="http://ftp.ipv4.heanet.ie/pub/ietf/internet-drafts/draft-ietf-ipsec-ciph-aes-cbc-03.txt">
* Descriptions of SHA-256, SHA-384, and SHA-512</a>,</li>
* <li>http://csrc.nist.gov/cryptval/shs/sha256-384-512.pdf</li>
* </ol>
*/
public class Sha384 extends BaseHash
public class Sha384
extends BaseHash
{
// Constants and variables
// -------------------------------------------------------------------------
private static final long[] k = { 0x428a2f98d728ae22L, 0x7137449123ef65cdL,
0xb5c0fbcfec4d3b2fL, 0xe9b5dba58189dbbcL,
0x3956c25bf348b538L, 0x59f111f1b605d019L,
0x923f82a4af194f9bL, 0xab1c5ed5da6d8118L,
0xd807aa98a3030242L, 0x12835b0145706fbeL,
0x243185be4ee4b28cL, 0x550c7dc3d5ffb4e2L,
0x72be5d74f27b896fL, 0x80deb1fe3b1696b1L,
0x9bdc06a725c71235L, 0xc19bf174cf692694L,
0xe49b69c19ef14ad2L, 0xefbe4786384f25e3L,
0x0fc19dc68b8cd5b5L, 0x240ca1cc77ac9c65L,
0x2de92c6f592b0275L, 0x4a7484aa6ea6e483L,
0x5cb0a9dcbd41fbd4L, 0x76f988da831153b5L,
0x983e5152ee66dfabL, 0xa831c66d2db43210L,
0xb00327c898fb213fL, 0xbf597fc7beef0ee4L,
0xc6e00bf33da88fc2L, 0xd5a79147930aa725L,
0x06ca6351e003826fL, 0x142929670a0e6e70L,
0x27b70a8546d22ffcL, 0x2e1b21385c26c926L,
0x4d2c6dfc5ac42aedL, 0x53380d139d95b3dfL,
0x650a73548baf63deL, 0x766a0abb3c77b2a8L,
0x81c2c92e47edaee6L, 0x92722c851482353bL,
0xa2bfe8a14cf10364L, 0xa81a664bbc423001L,
0xc24b8b70d0f89791L, 0xc76c51a30654be30L,
0xd192e819d6ef5218L, 0xd69906245565a910L,
0xf40e35855771202aL, 0x106aa07032bbd1b8L,
0x19a4c116b8d2d0c8L, 0x1e376c085141ab53L,
0x2748774cdf8eeb99L, 0x34b0bcb5e19b48a8L,
0x391c0cb3c5c95a63L, 0x4ed8aa4ae3418acbL,
0x5b9cca4f7763e373L, 0x682e6ff3d6b2b8a3L,
0x748f82ee5defb2fcL, 0x78a5636f43172f60L,
0x84c87814a1f0ab72L, 0x8cc702081a6439ecL,
0x90befffa23631e28L, 0xa4506cebde82bde9L,
0xbef9a3f7b2c67915L, 0xc67178f2e372532bL,
0xca273eceea26619cL, 0xd186b8c721c0c207L,
0xeada7dd6cde0eb1eL, 0xf57d4f7fee6ed178L,
0x06f067aa72176fbaL, 0x0a637dc5a2c898a6L,
0x113f9804bef90daeL, 0x1b710b35131c471bL,
0x28db77f523047d84L, 0x32caab7b40c72493L,
0x3c9ebe0a15c9bebcL, 0x431d67c49c100d4cL,
0x4cc5d4becb3e42b6L, 0x597f299cfc657e2aL,
0x5fcb6fab3ad6faecL, 0x6c44198c4a475817L };
private static final long[] k = {
0x428a2f98d728ae22L, 0x7137449123ef65cdL, 0xb5c0fbcfec4d3b2fL,
0xe9b5dba58189dbbcL, 0x3956c25bf348b538L, 0x59f111f1b605d019L,
0x923f82a4af194f9bL, 0xab1c5ed5da6d8118L, 0xd807aa98a3030242L,
0x12835b0145706fbeL, 0x243185be4ee4b28cL, 0x550c7dc3d5ffb4e2L,
0x72be5d74f27b896fL, 0x80deb1fe3b1696b1L, 0x9bdc06a725c71235L,
0xc19bf174cf692694L, 0xe49b69c19ef14ad2L, 0xefbe4786384f25e3L,
0x0fc19dc68b8cd5b5L, 0x240ca1cc77ac9c65L, 0x2de92c6f592b0275L,
0x4a7484aa6ea6e483L, 0x5cb0a9dcbd41fbd4L, 0x76f988da831153b5L,
0x983e5152ee66dfabL, 0xa831c66d2db43210L, 0xb00327c898fb213fL,
0xbf597fc7beef0ee4L, 0xc6e00bf33da88fc2L, 0xd5a79147930aa725L,
0x06ca6351e003826fL, 0x142929670a0e6e70L, 0x27b70a8546d22ffcL,
0x2e1b21385c26c926L, 0x4d2c6dfc5ac42aedL, 0x53380d139d95b3dfL,
0x650a73548baf63deL, 0x766a0abb3c77b2a8L, 0x81c2c92e47edaee6L,
0x92722c851482353bL, 0xa2bfe8a14cf10364L, 0xa81a664bbc423001L,
0xc24b8b70d0f89791L, 0xc76c51a30654be30L, 0xd192e819d6ef5218L,
0xd69906245565a910L, 0xf40e35855771202aL, 0x106aa07032bbd1b8L,
0x19a4c116b8d2d0c8L, 0x1e376c085141ab53L, 0x2748774cdf8eeb99L,
0x34b0bcb5e19b48a8L, 0x391c0cb3c5c95a63L, 0x4ed8aa4ae3418acbL,
0x5b9cca4f7763e373L, 0x682e6ff3d6b2b8a3L, 0x748f82ee5defb2fcL,
0x78a5636f43172f60L, 0x84c87814a1f0ab72L, 0x8cc702081a6439ecL,
0x90befffa23631e28L, 0xa4506cebde82bde9L, 0xbef9a3f7b2c67915L,
0xc67178f2e372532bL, 0xca273eceea26619cL, 0xd186b8c721c0c207L,
0xeada7dd6cde0eb1eL, 0xf57d4f7fee6ed178L, 0x06f067aa72176fbaL,
0x0a637dc5a2c898a6L, 0x113f9804bef90daeL, 0x1b710b35131c471bL,
0x28db77f523047d84L, 0x32caab7b40c72493L, 0x3c9ebe0a15c9bebcL,
0x431d67c49c100d4cL, 0x4cc5d4becb3e42b6L, 0x597f299cfc657e2aL,
0x5fcb6fab3ad6faecL, 0x6c44198c4a475817L };
private static final int BLOCK_SIZE = 128; // inner block size in bytes
private static final String DIGEST0 = "CB00753F45A35E8BB5A03D699AC65007272C32AB0EDED1631A8B605A43FF5BED"
+ "8086072BA1E7CC2358BAECA134C825A7";
private static final String DIGEST0 =
"CB00753F45A35E8BB5A03D699AC65007272C32AB0EDED1631A8B605A43FF5BED"
+ "8086072BA1E7CC2358BAECA134C825A7";
private static final long[] w = new long[80];
@ -111,9 +97,6 @@ public class Sha384 extends BaseHash
/** 512-bit interim result. */
private long h0, h1, h2, h3, h4, h5, h6, h7;
// Constructor(s)
// -------------------------------------------------------------------------
/** Trivial 0-arguments constructor. */
public Sha384()
{
@ -121,7 +104,7 @@ public class Sha384 extends BaseHash
}
/**
* <p>Private constructor for cloning purposes.</p>
* Private constructor for cloning purposes.
*
* @param md the instance to clone.
*/
@ -141,9 +124,6 @@ public class Sha384 extends BaseHash
this.buffer = (byte[]) md.buffer.clone();
}
// Class methods
// -------------------------------------------------------------------------
public static final long[] G(long hh0, long hh1, long hh2, long hh3,
long hh4, long hh5, long hh6, long hh7,
byte[] in, int offset)
@ -151,22 +131,14 @@ public class Sha384 extends BaseHash
return sha(hh0, hh1, hh2, hh3, hh4, hh5, hh6, hh7, in, offset);
}
// Instance methods
// -------------------------------------------------------------------------
// java.lang.Cloneable interface implementation ----------------------------
public Object clone()
{
return new Sha384(this);
}
// Implementation of concrete methods in BaseHash --------------------------
protected void transform(byte[] in, int offset)
{
long[] result = sha(h0, h1, h2, h3, h4, h5, h6, h7, in, offset);
h0 = result[0];
h1 = result[1];
h2 = result[2];
@ -179,57 +151,41 @@ public class Sha384 extends BaseHash
protected byte[] padBuffer()
{
int n = (int) (count % BLOCK_SIZE);
int n = (int)(count % BLOCK_SIZE);
int padding = (n < 112) ? (112 - n) : (240 - n);
byte[] result = new byte[padding + 16];
// padding is always binary 1 followed by binary 0s
result[0] = (byte) 0x80;
// save number of bits, casting the long to an array of 8 bytes
// TODO: FIX Only ~35 bits of 128 bit counter usable this way
long bits = count << 3;
padding += 8;
result[padding++] = (byte) (bits >>> 56);
result[padding++] = (byte) (bits >>> 48);
result[padding++] = (byte) (bits >>> 40);
result[padding++] = (byte) (bits >>> 32);
result[padding++] = (byte) (bits >>> 24);
result[padding++] = (byte) (bits >>> 16);
result[padding++] = (byte) (bits >>> 8);
result[padding] = (byte) bits;
result[padding++] = (byte)(bits >>> 56);
result[padding++] = (byte)(bits >>> 48);
result[padding++] = (byte)(bits >>> 40);
result[padding++] = (byte)(bits >>> 32);
result[padding++] = (byte)(bits >>> 24);
result[padding++] = (byte)(bits >>> 16);
result[padding++] = (byte)(bits >>> 8);
result[padding ] = (byte) bits;
return result;
}
protected byte[] getResult()
{
return new byte[] { (byte) (h0 >>> 56), (byte) (h0 >>> 48),
(byte) (h0 >>> 40), (byte) (h0 >>> 32),
(byte) (h0 >>> 24), (byte) (h0 >>> 16),
(byte) (h0 >>> 8), (byte) h0, (byte) (h1 >>> 56),
(byte) (h1 >>> 48), (byte) (h1 >>> 40),
(byte) (h1 >>> 32), (byte) (h1 >>> 24),
(byte) (h1 >>> 16), (byte) (h1 >>> 8), (byte) h1,
(byte) (h2 >>> 56), (byte) (h2 >>> 48),
(byte) (h2 >>> 40), (byte) (h2 >>> 32),
(byte) (h2 >>> 24), (byte) (h2 >>> 16),
(byte) (h2 >>> 8), (byte) h2, (byte) (h3 >>> 56),
(byte) (h3 >>> 48), (byte) (h3 >>> 40),
(byte) (h3 >>> 32), (byte) (h3 >>> 24),
(byte) (h3 >>> 16), (byte) (h3 >>> 8), (byte) h3,
(byte) (h4 >>> 56), (byte) (h4 >>> 48),
(byte) (h4 >>> 40), (byte) (h4 >>> 32),
(byte) (h4 >>> 24), (byte) (h4 >>> 16),
(byte) (h4 >>> 8), (byte) h4, (byte) (h5 >>> 56),
(byte) (h5 >>> 48), (byte) (h5 >>> 40),
(byte) (h5 >>> 32), (byte) (h5 >>> 24),
(byte) (h5 >>> 16), (byte) (h5 >>> 8), (byte) h5
// (byte)(h6 >>> 56), (byte)(h6 >>> 48), (byte)(h6 >>> 40), (byte)(h6 >>> 32),
// (byte)(h6 >>> 24), (byte)(h6 >>> 16), (byte)(h6 >>> 8), (byte) h6,
// (byte)(h7 >>> 56), (byte)(h7 >>> 48), (byte)(h7 >>> 40), (byte)(h7 >>> 32),
// (byte)(h7 >>> 24), (byte)(h7 >>> 16), (byte)(h7 >>> 8), (byte) h7
};
return new byte[] {
(byte)(h0 >>> 56), (byte)(h0 >>> 48), (byte)(h0 >>> 40), (byte)(h0 >>> 32),
(byte)(h0 >>> 24), (byte)(h0 >>> 16), (byte)(h0 >>> 8), (byte) h0,
(byte)(h1 >>> 56), (byte)(h1 >>> 48), (byte)(h1 >>> 40), (byte)(h1 >>> 32),
(byte)(h1 >>> 24), (byte)(h1 >>> 16), (byte)(h1 >>> 8), (byte) h1,
(byte)(h2 >>> 56), (byte)(h2 >>> 48), (byte)(h2 >>> 40), (byte)(h2 >>> 32),
(byte)(h2 >>> 24), (byte)(h2 >>> 16), (byte)(h2 >>> 8), (byte) h2,
(byte)(h3 >>> 56), (byte)(h3 >>> 48), (byte)(h3 >>> 40), (byte)(h3 >>> 32),
(byte)(h3 >>> 24), (byte)(h3 >>> 16), (byte)(h3 >>> 8), (byte) h3,
(byte)(h4 >>> 56), (byte)(h4 >>> 48), (byte)(h4 >>> 40), (byte)(h4 >>> 32),
(byte)(h4 >>> 24), (byte)(h4 >>> 16), (byte)(h4 >>> 8), (byte) h4,
(byte)(h5 >>> 56), (byte)(h5 >>> 48), (byte)(h5 >>> 40), (byte)(h5 >>> 32),
(byte)(h5 >>> 24), (byte)(h5 >>> 16), (byte)(h5 >>> 8), (byte) h5 };
}
protected void resetContext()
@ -259,9 +215,7 @@ public class Sha384 extends BaseHash
return valid.booleanValue();
}
// SHA specific methods ----------------------------------------------------
private static final synchronized long[] sha(long hh0, long hh1, long hh2,
private static synchronized final long[] sha(long hh0, long hh1, long hh2,
long hh3, long hh4, long hh5,
long hh6, long hh7, byte[] in,
int offset)
@ -276,35 +230,38 @@ public class Sha384 extends BaseHash
long H = hh7;
long T, T2;
int r;
for (r = 0; r < 16; r++)
{
w[r] = (long) in[offset++] << 56 | ((long) in[offset++] & 0xFF) << 48
| ((long) in[offset++] & 0xFF) << 40
| ((long) in[offset++] & 0xFF) << 32
| ((long) in[offset++] & 0xFF) << 24
| ((long) in[offset++] & 0xFF) << 16
| ((long) in[offset++] & 0xFF) << 8
| ((long) in[offset++] & 0xFF);
}
w[r] = (long) in[offset++] << 56
| ((long) in[offset++] & 0xFF) << 48
| ((long) in[offset++] & 0xFF) << 40
| ((long) in[offset++] & 0xFF) << 32
| ((long) in[offset++] & 0xFF) << 24
| ((long) in[offset++] & 0xFF) << 16
| ((long) in[offset++] & 0xFF) << 8
| ((long) in[offset++] & 0xFF);
for (r = 16; r < 80; r++)
{
T = w[r - 2];
T2 = w[r - 15];
w[r] = (((T >>> 19) | (T << 45)) ^ ((T >>> 61) | (T << 3)) ^ (T >>> 6))
+ w[r - 7]
+ (((T2 >>> 1) | (T2 << 63)) ^ ((T2 >>> 8) | (T2 << 56)) ^ (T2 >>> 7))
+ (((T2 >>> 1) | (T2 << 63))
^ ((T2 >>> 8) | (T2 << 56))
^ (T2 >>> 7))
+ w[r - 16];
}
for (r = 0; r < 80; r++)
{
T = H
+ (((E >>> 14) | (E << 50)) ^ ((E >>> 18) | (E << 46)) ^ ((E >>> 41) | (E << 23)))
+ (((E >>> 14) | (E << 50))
^ ((E >>> 18) | (E << 46))
^ ((E >>> 41) | (E << 23)))
+ ((E & F) ^ ((~E) & G)) + k[r] + w[r];
// T IS INCORRECT SOMEHOW
T2 = (((A >>> 28) | (A << 36)) ^ ((A >>> 34) | (A << 30)) ^ ((A >>> 39) | (A << 25)))
T2 = (((A >>> 28) | (A << 36))
^ ((A >>> 34) | (A << 30))
^ ((A >>> 39) | (A << 25)))
+ ((A & B) ^ (A & C) ^ (B & C));
H = G;
G = F;
@ -315,8 +272,8 @@ public class Sha384 extends BaseHash
B = A;
A = T + T2;
}
return new long[] { hh0 + A, hh1 + B, hh2 + C, hh3 + D, hh4 + E, hh5 + F,
hh6 + G, hh7 + H };
return new long[] {
hh0 + A, hh1 + B, hh2 + C, hh3 + D,
hh4 + E, hh5 + F, hh6 + G, hh7 + H };
}
}

View file

@ -42,66 +42,52 @@ import gnu.java.security.Registry;
import gnu.java.security.util.Util;
/**
* <p>Implementation of SHA2-3 [SHA-512] per the IETF Draft Specification.</p>
*
* <p>References:</p>
* Implementation of SHA2-3 [SHA-512] per the IETF Draft Specification.
* <p>
* References:
* <ol>
* <li><a href="http://ftp.ipv4.heanet.ie/pub/ietf/internet-drafts/draft-ietf-ipsec-ciph-aes-cbc-03.txt">
* Descriptions of SHA-256, SHA-384, and SHA-512</a>,</li>
* <li>http://csrc.nist.gov/cryptval/shs/sha256-384-512.pdf</li>
* </ol>
*/
public class Sha512 extends BaseHash
public class Sha512
extends BaseHash
{
// Constants and variables
// -------------------------------------------------------------------------
private static final long[] k = { 0x428a2f98d728ae22L, 0x7137449123ef65cdL,
0xb5c0fbcfec4d3b2fL, 0xe9b5dba58189dbbcL,
0x3956c25bf348b538L, 0x59f111f1b605d019L,
0x923f82a4af194f9bL, 0xab1c5ed5da6d8118L,
0xd807aa98a3030242L, 0x12835b0145706fbeL,
0x243185be4ee4b28cL, 0x550c7dc3d5ffb4e2L,
0x72be5d74f27b896fL, 0x80deb1fe3b1696b1L,
0x9bdc06a725c71235L, 0xc19bf174cf692694L,
0xe49b69c19ef14ad2L, 0xefbe4786384f25e3L,
0x0fc19dc68b8cd5b5L, 0x240ca1cc77ac9c65L,
0x2de92c6f592b0275L, 0x4a7484aa6ea6e483L,
0x5cb0a9dcbd41fbd4L, 0x76f988da831153b5L,
0x983e5152ee66dfabL, 0xa831c66d2db43210L,
0xb00327c898fb213fL, 0xbf597fc7beef0ee4L,
0xc6e00bf33da88fc2L, 0xd5a79147930aa725L,
0x06ca6351e003826fL, 0x142929670a0e6e70L,
0x27b70a8546d22ffcL, 0x2e1b21385c26c926L,
0x4d2c6dfc5ac42aedL, 0x53380d139d95b3dfL,
0x650a73548baf63deL, 0x766a0abb3c77b2a8L,
0x81c2c92e47edaee6L, 0x92722c851482353bL,
0xa2bfe8a14cf10364L, 0xa81a664bbc423001L,
0xc24b8b70d0f89791L, 0xc76c51a30654be30L,
0xd192e819d6ef5218L, 0xd69906245565a910L,
0xf40e35855771202aL, 0x106aa07032bbd1b8L,
0x19a4c116b8d2d0c8L, 0x1e376c085141ab53L,
0x2748774cdf8eeb99L, 0x34b0bcb5e19b48a8L,
0x391c0cb3c5c95a63L, 0x4ed8aa4ae3418acbL,
0x5b9cca4f7763e373L, 0x682e6ff3d6b2b8a3L,
0x748f82ee5defb2fcL, 0x78a5636f43172f60L,
0x84c87814a1f0ab72L, 0x8cc702081a6439ecL,
0x90befffa23631e28L, 0xa4506cebde82bde9L,
0xbef9a3f7b2c67915L, 0xc67178f2e372532bL,
0xca273eceea26619cL, 0xd186b8c721c0c207L,
0xeada7dd6cde0eb1eL, 0xf57d4f7fee6ed178L,
0x06f067aa72176fbaL, 0x0a637dc5a2c898a6L,
0x113f9804bef90daeL, 0x1b710b35131c471bL,
0x28db77f523047d84L, 0x32caab7b40c72493L,
0x3c9ebe0a15c9bebcL, 0x431d67c49c100d4cL,
0x4cc5d4becb3e42b6L, 0x597f299cfc657e2aL,
0x5fcb6fab3ad6faecL, 0x6c44198c4a475817L };
private static final long[] k = {
0x428a2f98d728ae22L, 0x7137449123ef65cdL, 0xb5c0fbcfec4d3b2fL,
0xe9b5dba58189dbbcL, 0x3956c25bf348b538L, 0x59f111f1b605d019L,
0x923f82a4af194f9bL, 0xab1c5ed5da6d8118L, 0xd807aa98a3030242L,
0x12835b0145706fbeL, 0x243185be4ee4b28cL, 0x550c7dc3d5ffb4e2L,
0x72be5d74f27b896fL, 0x80deb1fe3b1696b1L, 0x9bdc06a725c71235L,
0xc19bf174cf692694L, 0xe49b69c19ef14ad2L, 0xefbe4786384f25e3L,
0x0fc19dc68b8cd5b5L, 0x240ca1cc77ac9c65L, 0x2de92c6f592b0275L,
0x4a7484aa6ea6e483L, 0x5cb0a9dcbd41fbd4L, 0x76f988da831153b5L,
0x983e5152ee66dfabL, 0xa831c66d2db43210L, 0xb00327c898fb213fL,
0xbf597fc7beef0ee4L, 0xc6e00bf33da88fc2L, 0xd5a79147930aa725L,
0x06ca6351e003826fL, 0x142929670a0e6e70L, 0x27b70a8546d22ffcL,
0x2e1b21385c26c926L, 0x4d2c6dfc5ac42aedL, 0x53380d139d95b3dfL,
0x650a73548baf63deL, 0x766a0abb3c77b2a8L, 0x81c2c92e47edaee6L,
0x92722c851482353bL, 0xa2bfe8a14cf10364L, 0xa81a664bbc423001L,
0xc24b8b70d0f89791L, 0xc76c51a30654be30L, 0xd192e819d6ef5218L,
0xd69906245565a910L, 0xf40e35855771202aL, 0x106aa07032bbd1b8L,
0x19a4c116b8d2d0c8L, 0x1e376c085141ab53L, 0x2748774cdf8eeb99L,
0x34b0bcb5e19b48a8L, 0x391c0cb3c5c95a63L, 0x4ed8aa4ae3418acbL,
0x5b9cca4f7763e373L, 0x682e6ff3d6b2b8a3L, 0x748f82ee5defb2fcL,
0x78a5636f43172f60L, 0x84c87814a1f0ab72L, 0x8cc702081a6439ecL,
0x90befffa23631e28L, 0xa4506cebde82bde9L, 0xbef9a3f7b2c67915L,
0xc67178f2e372532bL, 0xca273eceea26619cL, 0xd186b8c721c0c207L,
0xeada7dd6cde0eb1eL, 0xf57d4f7fee6ed178L, 0x06f067aa72176fbaL,
0x0a637dc5a2c898a6L, 0x113f9804bef90daeL, 0x1b710b35131c471bL,
0x28db77f523047d84L, 0x32caab7b40c72493L, 0x3c9ebe0a15c9bebcL,
0x431d67c49c100d4cL, 0x4cc5d4becb3e42b6L, 0x597f299cfc657e2aL,
0x5fcb6fab3ad6faecL, 0x6c44198c4a475817L };
private static final int BLOCK_SIZE = 128; // inner block size in bytes
private static final String DIGEST0 = "DDAF35A193617ABACC417349AE20413112E6FA4E89A97EA20A9EEEE64B55D39A"
+ "2192992A274FC1A836BA3C23A3FEEBBD454D4423643CE80E2A9AC94FA54CA49F";
private static final String DIGEST0 =
"DDAF35A193617ABACC417349AE20413112E6FA4E89A97EA20A9EEEE64B55D39A"
+ "2192992A274FC1A836BA3C23A3FEEBBD454D4423643CE80E2A9AC94FA54CA49F";
private static final long[] w = new long[80];
@ -111,9 +97,6 @@ public class Sha512 extends BaseHash
/** 512-bit interim result. */
private long h0, h1, h2, h3, h4, h5, h6, h7;
// Constructor(s)
// -------------------------------------------------------------------------
/** Trivial 0-arguments constructor. */
public Sha512()
{
@ -121,7 +104,7 @@ public class Sha512 extends BaseHash
}
/**
* <p>Private constructor for cloning purposes.</p>
* Private constructor for cloning purposes.
*
* @param md the instance to clone.
*/
@ -141,9 +124,6 @@ public class Sha512 extends BaseHash
this.buffer = (byte[]) md.buffer.clone();
}
// Class methods
// -------------------------------------------------------------------------
public static final long[] G(long hh0, long hh1, long hh2, long hh3,
long hh4, long hh5, long hh6, long hh7,
byte[] in, int offset)
@ -151,22 +131,14 @@ public class Sha512 extends BaseHash
return sha(hh0, hh1, hh2, hh3, hh4, hh5, hh6, hh7, in, offset);
}
// Instance methods
// -------------------------------------------------------------------------
// java.lang.Cloneable interface implementation ----------------------------
public Object clone()
{
return new Sha512(this);
}
// Implementation of concrete methods in BaseHash --------------------------
protected void transform(byte[] in, int offset)
{
long[] result = sha(h0, h1, h2, h3, h4, h5, h6, h7, in, offset);
h0 = result[0];
h1 = result[1];
h2 = result[2];
@ -179,59 +151,45 @@ public class Sha512 extends BaseHash
protected byte[] padBuffer()
{
int n = (int) (count % BLOCK_SIZE);
int n = (int)(count % BLOCK_SIZE);
int padding = (n < 112) ? (112 - n) : (240 - n);
byte[] result = new byte[padding + 16];
// padding is always binary 1 followed by binary 0s
result[0] = (byte) 0x80;
// save number of bits, casting the long to an array of 8 bytes
// TODO: FIX Only ~35 bits of 128 bit counter usable this way
long bits = count << 3;
padding += 8;
result[padding++] = (byte) (bits >>> 56);
result[padding++] = (byte) (bits >>> 48);
result[padding++] = (byte) (bits >>> 40);
result[padding++] = (byte) (bits >>> 32);
result[padding++] = (byte) (bits >>> 24);
result[padding++] = (byte) (bits >>> 16);
result[padding++] = (byte) (bits >>> 8);
result[padding] = (byte) bits;
result[padding++] = (byte)(bits >>> 56);
result[padding++] = (byte)(bits >>> 48);
result[padding++] = (byte)(bits >>> 40);
result[padding++] = (byte)(bits >>> 32);
result[padding++] = (byte)(bits >>> 24);
result[padding++] = (byte)(bits >>> 16);
result[padding++] = (byte)(bits >>> 8);
result[padding ] = (byte) bits;
return result;
}
protected byte[] getResult()
{
return new byte[] { (byte) (h0 >>> 56), (byte) (h0 >>> 48),
(byte) (h0 >>> 40), (byte) (h0 >>> 32),
(byte) (h0 >>> 24), (byte) (h0 >>> 16),
(byte) (h0 >>> 8), (byte) h0, (byte) (h1 >>> 56),
(byte) (h1 >>> 48), (byte) (h1 >>> 40),
(byte) (h1 >>> 32), (byte) (h1 >>> 24),
(byte) (h1 >>> 16), (byte) (h1 >>> 8), (byte) h1,
(byte) (h2 >>> 56), (byte) (h2 >>> 48),
(byte) (h2 >>> 40), (byte) (h2 >>> 32),
(byte) (h2 >>> 24), (byte) (h2 >>> 16),
(byte) (h2 >>> 8), (byte) h2, (byte) (h3 >>> 56),
(byte) (h3 >>> 48), (byte) (h3 >>> 40),
(byte) (h3 >>> 32), (byte) (h3 >>> 24),
(byte) (h3 >>> 16), (byte) (h3 >>> 8), (byte) h3,
(byte) (h4 >>> 56), (byte) (h4 >>> 48),
(byte) (h4 >>> 40), (byte) (h4 >>> 32),
(byte) (h4 >>> 24), (byte) (h4 >>> 16),
(byte) (h4 >>> 8), (byte) h4, (byte) (h5 >>> 56),
(byte) (h5 >>> 48), (byte) (h5 >>> 40),
(byte) (h5 >>> 32), (byte) (h5 >>> 24),
(byte) (h5 >>> 16), (byte) (h5 >>> 8), (byte) h5,
(byte) (h6 >>> 56), (byte) (h6 >>> 48),
(byte) (h6 >>> 40), (byte) (h6 >>> 32),
(byte) (h6 >>> 24), (byte) (h6 >>> 16),
(byte) (h6 >>> 8), (byte) h6, (byte) (h7 >>> 56),
(byte) (h7 >>> 48), (byte) (h7 >>> 40),
(byte) (h7 >>> 32), (byte) (h7 >>> 24),
(byte) (h7 >>> 16), (byte) (h7 >>> 8), (byte) h7 };
return new byte[] {
(byte)(h0 >>> 56), (byte)(h0 >>> 48), (byte)(h0 >>> 40), (byte)(h0 >>> 32),
(byte)(h0 >>> 24), (byte)(h0 >>> 16), (byte)(h0 >>> 8), (byte) h0,
(byte)(h1 >>> 56), (byte)(h1 >>> 48), (byte)(h1 >>> 40), (byte)(h1 >>> 32),
(byte)(h1 >>> 24), (byte)(h1 >>> 16), (byte)(h1 >>> 8), (byte) h1,
(byte)(h2 >>> 56), (byte)(h2 >>> 48), (byte)(h2 >>> 40), (byte)(h2 >>> 32),
(byte)(h2 >>> 24), (byte)(h2 >>> 16), (byte)(h2 >>> 8), (byte) h2,
(byte)(h3 >>> 56), (byte)(h3 >>> 48), (byte)(h3 >>> 40), (byte)(h3 >>> 32),
(byte)(h3 >>> 24), (byte)(h3 >>> 16), (byte)(h3 >>> 8), (byte) h3,
(byte)(h4 >>> 56), (byte)(h4 >>> 48), (byte)(h4 >>> 40), (byte)(h4 >>> 32),
(byte)(h4 >>> 24), (byte)(h4 >>> 16), (byte)(h4 >>> 8), (byte) h4,
(byte)(h5 >>> 56), (byte)(h5 >>> 48), (byte)(h5 >>> 40), (byte)(h5 >>> 32),
(byte)(h5 >>> 24), (byte)(h5 >>> 16), (byte)(h5 >>> 8), (byte) h5,
(byte)(h6 >>> 56), (byte)(h6 >>> 48), (byte)(h6 >>> 40), (byte)(h6 >>> 32),
(byte)(h6 >>> 24), (byte)(h6 >>> 16), (byte)(h6 >>> 8), (byte) h6,
(byte)(h7 >>> 56), (byte)(h7 >>> 48), (byte)(h7 >>> 40), (byte)(h7 >>> 32),
(byte)(h7 >>> 24), (byte)(h7 >>> 16), (byte)(h7 >>> 8), (byte) h7 };
}
protected void resetContext()
@ -261,9 +219,7 @@ public class Sha512 extends BaseHash
return valid.booleanValue();
}
// SHA specific methods ----------------------------------------------------
private static final synchronized long[] sha(long hh0, long hh1, long hh2,
private static synchronized final long[] sha(long hh0, long hh1, long hh2,
long hh3, long hh4, long hh5,
long hh6, long hh7, byte[] in,
int offset)
@ -278,33 +234,36 @@ public class Sha512 extends BaseHash
long H = hh7;
long T, T2;
int r;
for (r = 0; r < 16; r++)
{
w[r] = (long) in[offset++] << 56 | ((long) in[offset++] & 0xFF) << 48
| ((long) in[offset++] & 0xFF) << 40
| ((long) in[offset++] & 0xFF) << 32
| ((long) in[offset++] & 0xFF) << 24
| ((long) in[offset++] & 0xFF) << 16
| ((long) in[offset++] & 0xFF) << 8
| ((long) in[offset++] & 0xFF);
}
w[r] = (long) in[offset++] << 56
| ((long) in[offset++] & 0xFF) << 48
| ((long) in[offset++] & 0xFF) << 40
| ((long) in[offset++] & 0xFF) << 32
| ((long) in[offset++] & 0xFF) << 24
| ((long) in[offset++] & 0xFF) << 16
| ((long) in[offset++] & 0xFF) << 8
| ((long) in[offset++] & 0xFF);
for (r = 16; r < 80; r++)
{
T = w[r - 2];
T2 = w[r - 15];
w[r] = (((T >>> 19) | (T << 45)) ^ ((T >>> 61) | (T << 3)) ^ (T >>> 6))
+ w[r - 7]
+ (((T2 >>> 1) | (T2 << 63)) ^ ((T2 >>> 8) | (T2 << 56)) ^ (T2 >>> 7))
+ (((T2 >>> 1) | (T2 << 63))
^ ((T2 >>> 8) | (T2 << 56))
^ (T2 >>> 7))
+ w[r - 16];
}
for (r = 0; r < 80; r++)
{
T = H
+ (((E >>> 14) | (E << 50)) ^ ((E >>> 18) | (E << 46)) ^ ((E >>> 41) | (E << 23)))
+ (((E >>> 14) | (E << 50))
^ ((E >>> 18) | (E << 46))
^ ((E >>> 41) | (E << 23)))
+ ((E & F) ^ ((~E) & G)) + k[r] + w[r];
T2 = (((A >>> 28) | (A << 36)) ^ ((A >>> 34) | (A << 30)) ^ ((A >>> 39) | (A << 25)))
T2 = (((A >>> 28) | (A << 36))
^ ((A >>> 34) | (A << 30))
^ ((A >>> 39) | (A << 25)))
+ ((A & B) ^ (A & C) ^ (B & C));
H = G;
G = F;
@ -315,8 +274,8 @@ public class Sha512 extends BaseHash
B = A;
A = T + T2;
}
return new long[] { hh0 + A, hh1 + B, hh2 + C, hh3 + D, hh4 + E, hh5 + F,
hh6 + G, hh7 + H };
return new long[] {
hh0 + A, hh1 + B, hh2 + C, hh3 + D,
hh4 + E, hh5 + F, hh6 + G, hh7 + H };
}
}

File diff suppressed because it is too large Load diff

View file

@ -38,9 +38,12 @@ exception statement from your version. */
package gnu.java.security.hash;
import gnu.java.security.Configuration;
import gnu.java.security.Registry;
import gnu.java.security.util.Util;
import java.util.logging.Logger;
/**
* Whirlpool, a new 512-bit hashing function operating on messages less than
* 2 ** 256 bits in length. The function structure is designed according to the
@ -59,18 +62,10 @@ import gnu.java.security.util.Util;
* <a href="mailto:vincent.rijmen@iaik.tugraz.at">Vincent Rijmen</a>.</li>
* </ol>
*/
public final class Whirlpool extends BaseHash
public final class Whirlpool
extends BaseHash
{
// Debugging methods and variables
// -------------------------------------------------------------------------
private static final boolean DEBUG = false;
private static final int debuglevel = 3;
// Constants and variables
// -------------------------------------------------------------------------
private static final Logger log = Logger.getLogger(Whirlpool.class.getName());
private static final int BLOCK_SIZE = 64; // inner block size in bytes
/** The digest of the 0-bit long message. */
@ -83,22 +78,22 @@ public final class Whirlpool extends BaseHash
/** Whirlpool S-box; p. 19. */
private static final String S_box = // p. 19 [WHIRLPOOL]
"\u1823\uc6E8\u87B8\u014F\u36A6\ud2F5\u796F\u9152" +
"\u60Bc\u9B8E\uA30c\u7B35\u1dE0\ud7c2\u2E4B\uFE57" +
"\u1577\u37E5\u9FF0\u4AdA\u58c9\u290A\uB1A0\u6B85" +
"\uBd5d\u10F4\ucB3E\u0567\uE427\u418B\uA77d\u95d8" +
"\uFBEE\u7c66\udd17\u479E\ucA2d\uBF07\uAd5A\u8333" +
"\u6302\uAA71\uc819\u49d9\uF2E3\u5B88\u9A26\u32B0" +
"\uE90F\ud580\uBEcd\u3448\uFF7A\u905F\u2068\u1AAE" +
"\uB454\u9322\u64F1\u7312\u4008\uc3Ec\udBA1\u8d3d" +
"\u9700\ucF2B\u7682\ud61B\uB5AF\u6A50\u45F3\u30EF" +
"\u3F55\uA2EA\u65BA\u2Fc0\udE1c\uFd4d\u9275\u068A" +
"\uB2E6\u0E1F\u62d4\uA896\uF9c5\u2559\u8472\u394c" +
"\u5E78\u388c\ud1A5\uE261\uB321\u9c1E\u43c7\uFc04" +
"\u5199\u6d0d\uFAdF\u7E24\u3BAB\ucE11\u8F4E\uB7EB" +
"\u3c81\u94F7\uB913\u2cd3\uE76E\uc403\u5644\u7FA9" +
"\u2ABB\uc153\udc0B\u9d6c\u3174\uF646\uAc89\u14E1" +
"\u163A\u6909\u70B6\ud0Ed\ucc42\u98A4\u285c\uF886";
"\u1823\uc6E8\u87B8\u014F\u36A6\ud2F5\u796F\u9152"
+ "\u60Bc\u9B8E\uA30c\u7B35\u1dE0\ud7c2\u2E4B\uFE57"
+ "\u1577\u37E5\u9FF0\u4AdA\u58c9\u290A\uB1A0\u6B85"
+ "\uBd5d\u10F4\ucB3E\u0567\uE427\u418B\uA77d\u95d8"
+ "\uFBEE\u7c66\udd17\u479E\ucA2d\uBF07\uAd5A\u8333"
+ "\u6302\uAA71\uc819\u49d9\uF2E3\u5B88\u9A26\u32B0"
+ "\uE90F\ud580\uBEcd\u3448\uFF7A\u905F\u2068\u1AAE"
+ "\uB454\u9322\u64F1\u7312\u4008\uc3Ec\udBA1\u8d3d"
+ "\u9700\ucF2B\u7682\ud61B\uB5AF\u6A50\u45F3\u30EF"
+ "\u3F55\uA2EA\u65BA\u2Fc0\udE1c\uFd4d\u9275\u068A"
+ "\uB2E6\u0E1F\u62d4\uA896\uF9c5\u2559\u8472\u394c"
+ "\u5E78\u388c\ud1A5\uE261\uB321\u9c1E\u43c7\uFc04"
+ "\u5199\u6d0d\uFAdF\u7E24\u3BAB\ucE11\u8F4E\uB7EB"
+ "\u3c81\u94F7\uB913\u2cd3\uE76E\uc403\u5644\u7FA9"
+ "\u2ABB\uc153\udc0B\u9d6c\u3174\uF646\uAc89\u14E1"
+ "\u163A\u6909\u70B6\ud0Ed\ucc42\u98A4\u285c\uF886";
/** The 64-bit lookup tables; section 7.1 p. 13. */
private static final long[] T0 = new long[256];
@ -130,12 +125,9 @@ public final class Whirlpool extends BaseHash
/** work area for holding block cipher's intermediate values. */
private long w0, w1, w2, w3, w4, w5, w6, w7;
// Static code - to intialise lookup tables --------------------------------
static
{
long time = System.currentTimeMillis();
int ROOT = 0x11D; // para. 2.1 [WHIRLPOOL]
int i, r, j;
long s1, s2, s4, s5, s8, s9, t;
@ -171,7 +163,6 @@ public final class Whirlpool extends BaseHash
T6[i] = t >>> 48 | t << 16;
T7[i] = t >>> 56 | t << 8;
}
for (r = 0, i = 0; r < R; )
rc[r++] = (T0[i++] & 0xFF00000000000000L)
^ (T1[i++] & 0x00FF000000000000L)
@ -181,103 +172,91 @@ public final class Whirlpool extends BaseHash
^ (T5[i++] & 0x0000000000FF0000L)
^ (T6[i++] & 0x000000000000FF00L)
^ (T7[i++] & 0x00000000000000FFL);
time = System.currentTimeMillis() - time;
if (DEBUG && debuglevel > 8)
if (Configuration.DEBUG)
{
System.out.println("==========");
System.out.println();
System.out.println("Static data");
System.out.println();
System.out.println();
System.out.println("T0[]:");
log.fine("Static data");
log.fine("T0[]:");
StringBuilder sb;
for (i = 0; i < 64; i++)
{
sb = new StringBuilder();
for (j = 0; j < 4; j++)
System.out.print("0x" + Util.toString(T0[i * 4 + j]) + ", ");
sb.append("0x").append(Util.toString(T0[i * 4 + j])).append(", ");
System.out.println();
log.fine(sb.toString());
}
System.out.println();
System.out.println("T1[]:");
log.fine("T1[]:");
for (i = 0; i < 64; i++)
{
sb = new StringBuilder();
for (j = 0; j < 4; j++)
System.out.print("0x" + Util.toString(T1[i * 4 + j]) + ", ");
sb.append("0x").append(Util.toString(T1[i * 4 + j])).append(", ");
System.out.println();
log.fine(sb.toString());
}
System.out.println();
System.out.println("T2[]:");
log.fine("T2[]:");
for (i = 0; i < 64; i++)
{
sb = new StringBuilder();
for (j = 0; j < 4; j++)
System.out.print("0x" + Util.toString(T2[i * 4 + j]) + ", ");
sb.append("0x").append(Util.toString(T2[i * 4 + j])).append(", ");
System.out.println();
log.fine(sb.toString());
}
System.out.println();
System.out.println("T3[]:");
log.fine("T3[]:");
for (i = 0; i < 64; i++)
{
sb = new StringBuilder();
for (j = 0; j < 4; j++)
System.out.print("0x" + Util.toString(T3[i * 4 + j]) + ", ");
sb.append("0x").append(Util.toString(T3[i * 4 + j])).append(", ");
System.out.println();
log.fine(sb.toString());
}
System.out.println();
System.out.println("T4[]:");
log.fine("\nT4[]:");
for (i = 0; i < 64; i++)
{
sb = new StringBuilder();
for (j = 0; j < 4; j++)
System.out.print("0x" + Util.toString(T4[i * 4 + j]) + ", ");
sb.append("0x").append(Util.toString(T4[i * 4 + j])).append(", ");
System.out.println();
log.fine(sb.toString());
}
System.out.println();
System.out.println("T5[]:");
log.fine("T5[]:");
for (i = 0; i < 64; i++)
{
sb = new StringBuilder();
for (j = 0; j < 4; j++)
System.out.print("0x" + Util.toString(T5[i * 4 + j]) + ", ");
sb.append("0x").append(Util.toString(T5[i * 4 + j])).append(", ");
System.out.println();
log.fine(sb.toString());
}
System.out.println();
System.out.println("T6[]:");
log.fine("T6[]:");
for (i = 0; i < 64; i++)
{
sb = new StringBuilder();
for (j = 0; j < 4; j++)
System.out.print("0x" + Util.toString(T5[i * 4 + j]) + ", ");
sb.append("0x").append(Util.toString(T5[i * 4 + j])).append(", ");
System.out.println();
log.fine(sb.toString());
}
System.out.println();
System.out.println("T7[]:");
log.fine("T7[]:");
for (i = 0; i < 64; i++)
{
sb = new StringBuilder();
for (j = 0; j < 4; j++)
System.out.print("0x" + Util.toString(T5[i * 4 + j]) + ", ");
sb.append("0x").append(Util.toString(T5[i * 4 + j])).append(", ");
System.out.println();
log.fine(sb.toString());
}
System.out.println();
System.out.println("rc[]:");
log.fine("rc[]:");
for (i = 0; i < R; i++)
System.out.println("0x" + Util.toString(rc[i]));
log.fine("0x" + Util.toString(rc[i]));
System.out.println();
System.out.println();
System.out.println("Total initialization time: " + time + " ms.");
System.out.println();
log.fine("Total initialization time: " + time + " ms.");
}
}
// Constructor(s)
// -------------------------------------------------------------------------
/** Trivial 0-arguments constructor. */
public Whirlpool()
{
@ -285,7 +264,7 @@ public final class Whirlpool extends BaseHash
}
/**
* <p>Private constructor for cloning purposes.</p>
* Private constructor for cloning purposes.
*
* @param md the instance to clone.
*/
@ -305,21 +284,11 @@ public final class Whirlpool extends BaseHash
this.buffer = (byte[]) md.buffer.clone();
}
// Class methods
// -------------------------------------------------------------------------
// Instance methods
// -------------------------------------------------------------------------
// java.lang.Cloneable interface implementation ----------------------------
public Object clone()
{
return (new Whirlpool(this));
}
// Implementation of concrete methods in BaseHash --------------------------
protected void transform(byte[] in, int offset)
{
// apply mu to the input
@ -387,7 +356,6 @@ public final class Whirlpool extends BaseHash
| (in[offset++] & 0xFFL) << 16
| (in[offset++] & 0xFFL) << 8
| (in[offset++] & 0xFFL);
// transform K into the key schedule Kr; 0 <= r <= R
k00 = H0;
k01 = H1;
@ -397,7 +365,6 @@ public final class Whirlpool extends BaseHash
k05 = H5;
k06 = H6;
k07 = H7;
nn0 = n0 ^ k00;
nn1 = n1 ^ k01;
nn2 = n2 ^ k02;
@ -406,10 +373,8 @@ public final class Whirlpool extends BaseHash
nn5 = n5 ^ k05;
nn6 = n6 ^ k06;
nn7 = n7 ^ k07;
// intermediate cipher output
w0 = w1 = w2 = w3 = w4 = w5 = w6 = w7 = 0L;
for (int r = 0; r < R; r++)
{
// 1. compute intermediate round key schedule by applying ro[rc]
@ -478,7 +443,6 @@ public final class Whirlpool extends BaseHash
^ T5[(int)((k02 >> 16) & 0xFFL)]
^ T6[(int)((k01 >> 8) & 0xFFL)]
^ T7[(int)( k00 & 0xFFL)];
k00 = Kr0;
k01 = Kr1;
k02 = Kr2;
@ -487,7 +451,6 @@ public final class Whirlpool extends BaseHash
k05 = Kr5;
k06 = Kr6;
k07 = Kr7;
// 2. incrementally compute the cipher output
w0 = T0[(int)((nn0 >> 56) & 0xFFL)]
^ T1[(int)((nn7 >> 48) & 0xFFL)]
@ -553,7 +516,6 @@ public final class Whirlpool extends BaseHash
^ T5[(int)((nn2 >> 16) & 0xFFL)]
^ T6[(int)((nn1 >> 8) & 0xFFL)]
^ T7[(int)( nn0 & 0xFFL)] ^ Kr7;
nn0 = w0;
nn1 = w1;
nn2 = w2;
@ -563,7 +525,6 @@ public final class Whirlpool extends BaseHash
nn6 = w6;
nn7 = w7;
}
// apply the Miyaguchi-Preneel hash scheme
H0 ^= w0 ^ n0;
H1 ^= w1 ^ n1;
@ -588,12 +549,9 @@ public final class Whirlpool extends BaseHash
// count + 33 + padding = 0 (mod BLOCK_SIZE)
int n = (int)((count + 33) % BLOCK_SIZE);
int padding = n == 0 ? 33 : BLOCK_SIZE - n + 33;
byte[] result = new byte[padding];
// padding is always binary 1 followed by binary 0s
result[0] = (byte) 0x80;
// save (right justified) the number of bits hashed
long bits = count * 8;
int i = padding - 8;
@ -605,14 +563,13 @@ public final class Whirlpool extends BaseHash
result[i++] = (byte)(bits >>> 16);
result[i++] = (byte)(bits >>> 8);
result[i ] = (byte) bits;
return result;
}
protected byte[] getResult()
{
// apply inverse mu to the context
byte[] result = new byte[] {
return new byte[] {
(byte)(H0 >>> 56), (byte)(H0 >>> 48), (byte)(H0 >>> 40), (byte)(H0 >>> 32),
(byte)(H0 >>> 24), (byte)(H0 >>> 16), (byte)(H0 >>> 8), (byte) H0,
(byte)(H1 >>> 56), (byte)(H1 >>> 48), (byte)(H1 >>> 40), (byte)(H1 >>> 32),
@ -628,10 +585,8 @@ public final class Whirlpool extends BaseHash
(byte)(H6 >>> 56), (byte)(H6 >>> 48), (byte)(H6 >>> 40), (byte)(H6 >>> 32),
(byte)(H6 >>> 24), (byte)(H6 >>> 16), (byte)(H6 >>> 8), (byte) H6,
(byte)(H7 >>> 56), (byte)(H7 >>> 48), (byte)(H7 >>> 40), (byte)(H7 >>> 32),
(byte)(H7 >>> 24), (byte)(H7 >>> 16), (byte)(H7 >>> 8), (byte) H7
};
(byte)(H7 >>> 24), (byte)(H7 >>> 16), (byte)(H7 >>> 8), (byte) H7 };
return result;
}
protected void resetContext()
@ -642,8 +597,10 @@ public final class Whirlpool extends BaseHash
public boolean selfTest()
{
if (valid == null)
valid = Boolean.valueOf(DIGEST0.equals(Util.toString(new Whirlpool().digest())));
{
String d = Util.toString(new Whirlpool().digest());
valid = Boolean.valueOf(DIGEST0.equals(d));
}
return valid.booleanValue();
}
}