aboutsummaryrefslogtreecommitdiff
path: root/libjava/java/security/MessageDigest.java
blob: 04546cdde195b4b4245562d07c13c693c0ed611e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// MessageDigest.java

/* Copyright (C) 2000  Free Software Foundation

   This file is part of libgcj.

This software is copyrighted work licensed under the terms of the
Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
details.  */

package java.security;

/**
 * @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
{
  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;
}