comparison src/com/trilead/ssh2/crypto/digest/HashForSSH2Types.java @ 0:0ce5cc452d02

initial version
author Carl Byington <carl@five-ten-sg.com>
date Thu, 22 May 2014 10:41:19 -0700
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:0ce5cc452d02
1
2 package com.trilead.ssh2.crypto.digest;
3
4 import java.math.BigInteger;
5 import java.security.DigestException;
6 import java.security.MessageDigest;
7 import java.security.NoSuchAlgorithmException;
8
9 /**
10 * HashForSSH2Types.
11 *
12 * @author Christian Plattner, plattner@trilead.com
13 * @version $Id: HashForSSH2Types.java,v 1.1 2007/10/15 12:49:57 cplattne Exp $
14 */
15 public class HashForSSH2Types {
16 MessageDigest md;
17
18 public HashForSSH2Types(String type) {
19 try {
20 md = MessageDigest.getInstance(type);
21 }
22 catch (NoSuchAlgorithmException e) {
23 throw new RuntimeException("Unsupported algorithm " + type);
24 }
25 }
26
27 public void updateByte(byte b) {
28 /* HACK - to test it with J2ME */
29 byte[] tmp = new byte[1];
30 tmp[0] = b;
31 md.update(tmp);
32 }
33
34 public void updateBytes(byte[] b) {
35 md.update(b);
36 }
37
38 public void updateUINT32(int v) {
39 md.update((byte)(v >> 24));
40 md.update((byte)(v >> 16));
41 md.update((byte)(v >> 8));
42 md.update((byte)(v));
43 }
44
45 public void updateByteString(byte[] b) {
46 updateUINT32(b.length);
47 updateBytes(b);
48 }
49
50 public void updateBigInt(BigInteger b) {
51 updateByteString(b.toByteArray());
52 }
53
54 public void reset() {
55 md.reset();
56 }
57
58 public int getDigestLength() {
59 return md.getDigestLength();
60 }
61
62 public byte[] getDigest() {
63 byte[] tmp = new byte[md.getDigestLength()];
64 getDigest(tmp);
65 return tmp;
66 }
67
68 public void getDigest(byte[] out) {
69 getDigest(out, 0);
70 }
71
72 public void getDigest(byte[] out, int off) {
73 try {
74 md.digest(out, off, out.length - off);
75 }
76 catch (DigestException e) {
77 // TODO is this right?!
78 throw new RuntimeException("Unable to digest", e);
79 }
80 }
81 }