1 19 20 package com.maverick.crypto.digests; 21 22 import com.maverick.util.ByteArrayWriter; 23 24 import java.math.BigInteger ; 25 26 27 33 public class Hash { 34 private Digest digest; 35 36 40 public Hash(String type) { 41 this.digest = DigestFactory.createDigest(type); 42 } 43 44 public Hash(Digest digest) { 45 this.digest = digest; 46 } 47 48 51 public void putBigInteger(BigInteger bi) { 52 byte[] data = bi.toByteArray(); 53 54 putInt(data.length); 55 putBytes(data); 56 } 57 58 61 public void putByte(byte b) { 62 digest.update(b); 63 } 64 65 68 public void putBytes(byte[] data) { 69 digest.update(data, 0, data.length); 70 } 71 72 75 public void putBytes(byte[] data, int offset, int len) { 76 digest.update(data, offset, len); 77 } 78 79 82 public void putInt(int i) { 83 putBytes(ByteArrayWriter.encodeInt(i)); 84 } 85 86 89 public void putString(String str) { 90 putInt(str.length()); 91 putBytes(str.getBytes()); 92 } 93 94 97 public void reset() { 98 digest.reset(); 99 } 100 101 104 public byte[] doFinal() { 105 byte[] hash = new byte[digest.getDigestSize()]; 106 digest.doFinal(hash, 0); 107 return hash; 108 } 109 } 110 | Popular Tags |