1 | /******************************************************************************
|
---|
2 | *
|
---|
3 | * Copyright (c) 1998-2000 by Mindbright Technology AB, Stockholm, Sweden.
|
---|
4 | * www.mindbright.se, [email protected]
|
---|
5 | *
|
---|
6 | * This program is free software; you can redistribute it and/or modify
|
---|
7 | * it under the terms of the GNU General Public License as published by
|
---|
8 | * the Free Software Foundation; either version 2 of the License, or
|
---|
9 | * (at your option) any later version.
|
---|
10 | *
|
---|
11 | * This program is distributed in the hope that it will be useful,
|
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
14 | * GNU General Public License for more details.
|
---|
15 | *
|
---|
16 | *****************************************************************************
|
---|
17 | * $Author: mats $
|
---|
18 | * $Date: 2000/05/16 10:30:42 $
|
---|
19 | * $Name: rel1-2-1 $
|
---|
20 | *****************************************************************************/
|
---|
21 | package mindbright.security;
|
---|
22 |
|
---|
23 | public abstract class MessageDigest {
|
---|
24 | public static boolean useNative = false;
|
---|
25 |
|
---|
26 | public static MessageDigest getInstance(String algorithm)
|
---|
27 | throws InstantiationException, IllegalAccessException, ClassNotFoundException
|
---|
28 | {
|
---|
29 | if(useNative) {
|
---|
30 | try {
|
---|
31 | Class c1, c2;
|
---|
32 | c1 = Class.forName("java.security.MessageDigest");
|
---|
33 | c2 = Class.forName("mindbright.security.NativeHashState");
|
---|
34 | MessageDigest md = (MessageDigest)c2.newInstance();
|
---|
35 | md.init(algorithm);
|
---|
36 | return md;
|
---|
37 | } catch (Throwable t) {
|
---|
38 | // !!! Oh well, we're not too worried, the pure java
|
---|
39 | // versions are pretty quick, we don't need no
|
---|
40 | // steenking native code anyway... :-)
|
---|
41 | }
|
---|
42 | }
|
---|
43 | Class c;
|
---|
44 | c = Class.forName("mindbright.security." + algorithm);
|
---|
45 | return (MessageDigest)c.newInstance();
|
---|
46 | }
|
---|
47 |
|
---|
48 | protected void init(String algorithm) throws Exception {
|
---|
49 | }
|
---|
50 |
|
---|
51 | public abstract String getName();
|
---|
52 | public abstract void reset();
|
---|
53 | public abstract void update(byte[] buf, int offset, int length);
|
---|
54 | public abstract byte[] digest();
|
---|
55 | public abstract int blockSize();
|
---|
56 | public abstract int hashSize();
|
---|
57 |
|
---|
58 | public final void update(byte[] buf) {
|
---|
59 | update(buf, 0, buf.length);
|
---|
60 | }
|
---|
61 |
|
---|
62 | public int digestInto(byte[] dest, int destOff) {
|
---|
63 | byte[] dig = digest();
|
---|
64 | System.arraycopy(dig, 0, dest, destOff, dig.length);
|
---|
65 | return dig.length;
|
---|
66 | }
|
---|
67 |
|
---|
68 | public Object clone() throws CloneNotSupportedException {
|
---|
69 | if(this instanceof Cloneable) {
|
---|
70 | return super.clone();
|
---|
71 | } else {
|
---|
72 | throw new CloneNotSupportedException();
|
---|
73 | }
|
---|
74 | }
|
---|
75 | }
|
---|