Mercurial > 510Connectbot
diff src/ch/ethz/ssh2/crypto/digest/HashForSSH2Types.java @ 273:91a31873c42a ganymed
start conversion from trilead to ganymed
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Fri, 18 Jul 2014 11:21:46 -0700 |
parents | |
children | 071eccdff8ea |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/ch/ethz/ssh2/crypto/digest/HashForSSH2Types.java Fri Jul 18 11:21:46 2014 -0700 @@ -0,0 +1,89 @@ +/* + * Copyright (c) 2006-2011 Christian Plattner. All rights reserved. + * Please refer to the LICENSE.txt for licensing details. + */ +package ch.ethz.ssh2.crypto.digest; + +import java.io.IOException; +import java.math.BigInteger; +import java.security.DigestException; + +/** + * HashForSSH2Types. + * + * @author Christian Plattner + * @version 2.50, 03/15/10 + */ +public class HashForSSH2Types { + Digest md; + + public HashForSSH2Types(Digest md) { + this.md = md; + } + + public HashForSSH2Types(String type) { + if(type.equals("SHA1")) { + md = new SHA1(); + } + else if(type.equals("SHA2")) { + md = new SHA256(); + } + else if(type.equals("MD5")) { + md = new MD5(); + } + else { + throw new IllegalArgumentException(String.format("Unknown algorithm %s", type)); + } + } + + public void updateByte(byte b) { + md.update(b); + } + + public void updateBytes(byte[] b) { + md.update(b); + } + + public void updateUINT32(int v) { + md.update((byte) (v >> 24)); + md.update((byte) (v >> 16)); + md.update((byte) (v >> 8)); + md.update((byte) (v)); + } + + public void updateByteString(byte[] b) { + updateUINT32(b.length); + updateBytes(b); + } + + public void updateBigInt(BigInteger b) { + updateByteString(b.toByteArray()); + } + + public void reset() { + md.reset(); + } + + public int getDigestLength() { + return md.getDigestLength(); + } + + public byte[] getDigest() throws IOException { + byte[] tmp = new byte[md.getDigestLength()]; + getDigest(tmp); + return tmp; + } + + public void getDigest(byte[] out) throws IOException { + try { + getDigest(out, 0); + } + catch(DigestException e) { + throw new IOException(e); + } + } + + public void getDigest(byte[] out, int off) throws DigestException { + md.digest(out, off); + } +}