comparison app/src/main/java/ch/ethz/ssh2/crypto/digest/HashForSSH2Types.java @ 438:d29cce60f393

migrate from Eclipse to Android Studio
author Carl Byington <carl@five-ten-sg.com>
date Thu, 03 Dec 2015 11:23:55 -0800
parents src/ch/ethz/ssh2/crypto/digest/HashForSSH2Types.java@071eccdff8ea
children
comparison
equal deleted inserted replaced
437:208b31032318 438:d29cce60f393
1 /*
2 * Copyright (c) 2006-2011 Christian Plattner. All rights reserved.
3 * Please refer to the LICENSE.txt for licensing details.
4 */
5 package ch.ethz.ssh2.crypto.digest;
6
7 import java.io.IOException;
8 import java.math.BigInteger;
9 import java.security.DigestException;
10
11 /**
12 * HashForSSH2Types.
13 *
14 * @author Christian Plattner
15 * @version 2.50, 03/15/10
16 */
17 public class HashForSSH2Types {
18 Digest md;
19
20 public HashForSSH2Types(Digest md) {
21 this.md = md;
22 }
23
24 public HashForSSH2Types(String type) {
25 if (type.equals("SHA1")) {
26 md = new SHA1();
27 }
28 else if (type.equals("SHA2")) {
29 md = new SHA256();
30 }
31 else if (type.equals("MD5")) {
32 md = new MD5();
33 }
34 else {
35 throw new IllegalArgumentException(String.format("Unknown algorithm %s", type));
36 }
37 }
38
39 public void updateByte(byte b) {
40 md.update(b);
41 }
42
43 public void updateBytes(byte[] b) {
44 md.update(b);
45 }
46
47 public void updateUINT32(int v) {
48 md.update((byte)(v >> 24));
49 md.update((byte)(v >> 16));
50 md.update((byte)(v >> 8));
51 md.update((byte)(v));
52 }
53
54 public void updateByteString(byte[] b) {
55 updateUINT32(b.length);
56 updateBytes(b);
57 }
58
59 public void updateBigInt(BigInteger b) {
60 updateByteString(b.toByteArray());
61 }
62
63 public void reset() {
64 md.reset();
65 }
66
67 public int getDigestLength() {
68 return md.getDigestLength();
69 }
70
71 public byte[] getDigest() throws IOException {
72 byte[] tmp = new byte[md.getDigestLength()];
73 getDigest(tmp);
74 return tmp;
75 }
76
77 public void getDigest(byte[] out) throws IOException {
78 try {
79 getDigest(out, 0);
80 }
81 catch (DigestException e) {
82 throw new IOException(e);
83 }
84 }
85
86 public void getDigest(byte[] out, int off) throws DigestException {
87 md.digest(out, off);
88 }
89 }