Home | History | Annotate | Download | only in digest
      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.math.BigInteger;
      8 
      9 /**
     10  * HashForSSH2Types.
     11  *
     12  * @author Christian Plattner
     13  * @version 2.50, 03/15/10
     14  */
     15 public class HashForSSH2Types
     16 {
     17 	Digest md;
     18 
     19 	public HashForSSH2Types(Digest md)
     20 	{
     21 		this.md = md;
     22 	}
     23 
     24 	public HashForSSH2Types(String type)
     25 	{
     26 		if (type.equals("SHA1"))
     27 		{
     28 			md = new SHA1();
     29 		}
     30 		else if (type.equals("MD5"))
     31 		{
     32 			md = new MD5();
     33 		}
     34 		else
     35 			throw new IllegalArgumentException("Unknown algorithm " + type);
     36 	}
     37 
     38 	public void updateByte(byte b)
     39 	{
     40 		/* HACK - to test it with J2ME */
     41 		byte[] tmp = new byte[1];
     42 		tmp[0] = b;
     43 		md.update(tmp);
     44 	}
     45 
     46 	public void updateBytes(byte[] b)
     47 	{
     48 		md.update(b);
     49 	}
     50 
     51 	public void updateUINT32(int v)
     52 	{
     53 		md.update((byte) (v >> 24));
     54 		md.update((byte) (v >> 16));
     55 		md.update((byte) (v >> 8));
     56 		md.update((byte) (v));
     57 	}
     58 
     59 	public void updateByteString(byte[] b)
     60 	{
     61 		updateUINT32(b.length);
     62 		updateBytes(b);
     63 	}
     64 
     65 	public void updateBigInt(BigInteger b)
     66 	{
     67 		updateByteString(b.toByteArray());
     68 	}
     69 
     70 	public void reset()
     71 	{
     72 		md.reset();
     73 	}
     74 
     75 	public int getDigestLength()
     76 	{
     77 		return md.getDigestLength();
     78 	}
     79 
     80 	public byte[] getDigest()
     81 	{
     82 		byte[] tmp = new byte[md.getDigestLength()];
     83 		getDigest(tmp);
     84 		return tmp;
     85 	}
     86 
     87 	public void getDigest(byte[] out)
     88 	{
     89 		getDigest(out, 0);
     90 	}
     91 
     92 	public void getDigest(byte[] out, int off)
     93 	{
     94 		md.digest(out, off);
     95 	}
     96 }
     97