aboutsummaryrefslogtreecommitdiff
path: root/libjava/java/security/MessageDigest.java
diff options
context:
space:
mode:
Diffstat (limited to 'libjava/java/security/MessageDigest.java')
-rw-r--r--libjava/java/security/MessageDigest.java180
1 files changed, 130 insertions, 50 deletions
diff --git a/libjava/java/security/MessageDigest.java b/libjava/java/security/MessageDigest.java
index 67e4911..04546cd 100644
--- a/libjava/java/security/MessageDigest.java
+++ b/libjava/java/security/MessageDigest.java
@@ -1,6 +1,6 @@
// MessageDigest.java
-/* Copyright (C) 1999 Free Software Foundation
+/* Copyright (C) 2000 Free Software Foundation
This file is part of libgcj.
@@ -10,55 +10,135 @@ details. */
package java.security;
-// FIXME: This is just a stub for a proper implementation.
+/**
+ * @author Tom Tromey <tromey@cygnus.com>
+ * @date February 11, 2000.
+ */
+
+/**
+ * Written using on-line Java Platform 1.1 API Specification.
+ * Status: Believed complete and correct to 1.1 spec.
+ * It is known not to comply with the 1.2 spec.
+ */
+
public abstract class MessageDigest
{
- private static final byte[] dummy = { 0 };
-
- public static MessageDigest getInstance(String algorithm)
- throws NoSuchAlgorithmException
- {
- Object obj;
-
- try {
- obj = Class.forName(algorithm).newInstance();
- } catch (Exception e) {
- throw new NoSuchAlgorithmException("algorithm "
- + algorithm
- + " not available.");
- }
-
- return (MessageDigest) obj;
- }
-
- public void update(byte input)
- {
- // FIXME
- }
-
- public void update(byte[] input, int offset, int len)
- {
- // FIXME
- }
-
- public void update(byte[] input)
- {
- // FIXME
- }
-
- public byte[] digest()
- {
- return dummy;
- }
-
- public byte[] digest(byte[] input)
- {
- update(input);
- return digest();
- }
-
- public void reset()
- {
- // FIXME
- }
+ protected MessageDigest (String algorithm)
+ {
+ name = algorithm;
+ }
+
+ public static MessageDigest getInstance (String algorithm)
+ throws NoSuchAlgorithmException
+ {
+ String name = "MessageDigest." + algorithm;
+ Provider[] provs = Security.getProviders ();
+ for (int i = 0; i < provs.length; ++i)
+ {
+ String val = provs[i].getProperty (name);
+ if (val != null)
+ {
+ try
+ {
+ return (MessageDigest) Class.forName(val).newInstance ();
+ }
+ catch (Throwable _)
+ {
+ // We just ignore failures.
+ }
+ }
+ }
+
+ throw new NoSuchAlgorithmException (algorithm);
+ }
+
+ public static MessageDigest getInstance (String algorithm, String provider)
+ throws NoSuchAlgorithmException, NoSuchProviderException
+ {
+ String name = "MessageDigest." + algorithm;
+ Provider p = Security.getProvider (provider);
+ if (p == null)
+ throw new NoSuchProviderException (provider);
+ String val = p.getProperty (name);
+ if (val != null)
+ {
+ try
+ {
+ return (MessageDigest) Class.forName(val).newInstance ();
+ }
+ catch (Throwable _)
+ {
+ // Nothing.
+ }
+ }
+
+ throw new NoSuchAlgorithmException (algorithm);
+ }
+
+ public void update (byte input)
+ {
+ engineUpdate (input);
+ }
+
+ public void update (byte[] input, int offset, int len)
+ {
+ engineUpdate (input, offset, len);
+ }
+
+ public void update (byte[] input)
+ {
+ engineUpdate (input, 0, input.length);
+ }
+
+ public byte[] digest ()
+ {
+ return engineDigest ();
+ }
+
+ public byte[] digest (byte[] input)
+ {
+ update (input);
+ return engineDigest ();
+ }
+
+ public String toString ()
+ {
+ // There is no spec for this.
+ return "[MessageDigest: " + name + "]";
+ }
+
+ public static boolean isEqual (byte[] digesta, byte[] digestb)
+ {
+ if (digesta == digestb)
+ return true;
+ if (digesta.length != digestb.length)
+ return false;
+ for (int i = digesta.length - 1; i >= 0; --i)
+ if (digesta[i] != digestb[i])
+ return false;
+ return true;
+ }
+
+ public void reset ()
+ {
+ engineReset ();
+ }
+
+ public final String getAlgorithm ()
+ {
+ return name;
+ }
+
+ protected abstract void engineUpdate (byte input);
+ protected abstract void engineUpdate (byte input[], int offset, int len);
+ protected abstract byte[] engineDigest ();
+ protected abstract void engineReset ();
+
+ public Object clone() throws CloneNotSupportedException
+ {
+ return super.clone ();
+ }
+
+ // Algorithm name.
+ private String name;
}