001: /*
002: * SSHTools - Java SSH2 API
003: *
004: * Copyright (C) 2002-2003 Lee David Painter and Contributors.
005: *
006: * Contributions made by:
007: *
008: * Brett Smith
009: * Richard Pernavas
010: * Erwin Bolwidt
011: *
012: * This program is free software; you can redistribute it and/or
013: * modify it under the terms of the GNU General Public License
014: * as published by the Free Software Foundation; either version 2
015: * of the License, or (at your option) any later version.
016: *
017: * This program is distributed in the hope that it will be useful,
018: * but WITHOUT ANY WARRANTY; without even the implied warranty of
019: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
020: * GNU General Public License for more details.
021: *
022: * You should have received a copy of the GNU General Public License
023: * along with this program; if not, write to the Free Software
024: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
025: */
026: package com.sshtools.j2ssh.util;
027:
028: import com.sshtools.j2ssh.io.*;
029:
030: import java.io.*;
031:
032: import java.math.*;
033:
034: import java.security.*;
035:
036: /**
037: *
038: *
039: * @author $author$
040: * @version $Revision: 1.13 $
041: */
042: public class Hash {
043: private MessageDigest hash;
044:
045: /**
046: * Creates a new Hash object.
047: *
048: * @param algorithm
049: *
050: * @throws NoSuchAlgorithmException
051: */
052: public Hash(String algorithm) throws NoSuchAlgorithmException {
053: hash = MessageDigest.getInstance(algorithm);
054: }
055:
056: /**
057: *
058: *
059: * @param bi
060: */
061: public void putBigInteger(BigInteger bi) {
062: byte[] data = bi.toByteArray();
063: putInt(data.length);
064: hash.update(data);
065: }
066:
067: /**
068: *
069: *
070: * @param b
071: */
072: public void putByte(byte b) {
073: hash.update(b);
074: }
075:
076: /**
077: *
078: *
079: * @param data
080: */
081: public void putBytes(byte[] data) {
082: hash.update(data);
083: }
084:
085: /**
086: *
087: *
088: * @param i
089: */
090: public void putInt(int i) {
091: ByteArrayWriter baw = new ByteArrayWriter();
092:
093: try {
094: baw.writeInt(i);
095: } catch (IOException ioe) {
096: }
097:
098: hash.update(baw.toByteArray());
099: }
100:
101: /**
102: *
103: *
104: * @param str
105: */
106: public void putString(String str) {
107: putInt(str.length());
108: hash.update(str.getBytes());
109: }
110:
111: /**
112: *
113: */
114: public void reset() {
115: hash.reset();
116: }
117:
118: /**
119: *
120: *
121: * @param data
122: * @param algorithm
123: *
124: * @return
125: *
126: * @throws NoSuchAlgorithmException
127: */
128: public static byte[] simple(byte[] data, String algorithm)
129: throws NoSuchAlgorithmException {
130: MessageDigest simp = MessageDigest.getInstance(algorithm);
131: simp.update(data);
132:
133: return simp.digest();
134: }
135:
136: /**
137: *
138: *
139: * @return
140: */
141: public byte[] doFinal() {
142: return hash.digest();
143: }
144: }
|