001: /*
002: * @(#)MD.java 1.2 04/12/06
003: *
004: * Copyright (c) 2002-2004 Sun Microsystems, Inc. All Rights Reserved.
005: *
006: * See the file "LICENSE.txt" for information on usage and redistribution
007: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
008: */
009: package pnuts.security;
010:
011: import pnuts.lang.*;
012: import java.io.*;
013: import java.net.URL;
014: import java.security.*;
015:
016: /*
017: * Base class of Message Digest functions.
018: */
019: public class MD extends PnutsFunction {
020:
021: String algorithm;
022: String keysym;
023:
024: public MD(String name, String algorithm) {
025: super (name);
026: this .algorithm = algorithm;
027: this .keysym = ("pnuts.security.md." + algorithm).intern();
028: }
029:
030: public boolean defined(int nargs) {
031: return (nargs == 1 || nargs == 3);
032: }
033:
034: protected Object exec(Object args[], Context context) {
035: int nargs = args.length;
036: MessageDigest md;
037: try {
038: md = (MessageDigest) context.get(keysym);
039: if (md == null) {
040: md = MessageDigest.getInstance(algorithm);
041: context.set(keysym, md);
042: }
043: } catch (NoSuchAlgorithmException e) {
044: throw new PnutsException(e, context);
045: }
046: if (nargs == 1) {
047: try {
048: return MD.digest(md, args[0]);
049: } catch (IOException e) {
050: throw new PnutsException(e, context);
051: }
052: } else if (nargs == 3) {
053: byte[] b = (byte[]) args[0];
054: int offset = ((Integer) args[1]).intValue();
055: int size = ((Integer) args[2]).intValue();
056: return MD.digest(md, b, offset, size);
057: } else {
058: undefined(args, context);
059: return null;
060: }
061: }
062:
063: public static byte[] digest(MessageDigest md, Object input)
064: throws IOException {
065: md.reset();
066: if (input instanceof InputStream) {
067: InputStream in = (InputStream) input;
068: byte[] buf = new byte[512];
069: int n;
070: while ((n = in.read(buf, 0, 512)) != -1) {
071: md.update(buf, 0, n);
072: }
073: } else if (input instanceof File) {
074: InputStream in = new FileInputStream((File) input);
075: byte[] buf = new byte[512];
076: int n;
077: try {
078: while ((n = in.read(buf, 0, 512)) != -1) {
079: md.update(buf, 0, n);
080: }
081: } finally {
082: in.close();
083: }
084: } else if (input instanceof URL) {
085: InputStream in = ((URL) input).openStream();
086: byte[] buf = new byte[512];
087: int n;
088: try {
089: while ((n = in.read(buf, 0, 512)) != -1) {
090: md.update(buf, 0, n);
091: }
092: } finally {
093: in.close();
094: }
095: } else if (input instanceof String) {
096: String str = (String) input;
097: char[] chars = str.toCharArray();
098: int len = chars.length;
099: byte[] b = new byte[len * 2];
100: for (int i = 0; i < len; i++) {
101: b[i * 2] = (byte) ((chars[i] >> 8) & 0xff);
102: b[i * 2 + 1] = (byte) (chars[i] & 0xff);
103: }
104: md.update(b, 0, b.length);
105: } else if (input instanceof byte[]) {
106: byte[] b = (byte[]) input;
107: md.update(b, 0, b.length);
108: } else {
109: throw new IllegalArgumentException();
110: }
111: return md.digest();
112: }
113:
114: public static byte[] digest(MessageDigest md, byte[] input,
115: int offset, int size) {
116: md.reset();
117: md.update(input, offset, size);
118: return md.digest();
119: }
120: }
|