tikhomirov@31: /* tikhomirov@31: * Copyright (c) 2010, 2011 Artem Tikhomirov tikhomirov@9: */ tikhomirov@9: package com.tmate.hgkit.ll; tikhomirov@9: tikhomirov@9: import java.io.IOException; tikhomirov@9: import java.io.InputStream; tikhomirov@9: import java.security.MessageDigest; tikhomirov@9: import java.security.NoSuchAlgorithmException; tikhomirov@9: tikhomirov@9: /** tikhomirov@42: *
tikhomirov@42: * DigestHelper dh; tikhomirov@42: * dh.sha1(...).asHexString(); tikhomirov@42: * or tikhomirov@42: * dh = dh.sha1(...); tikhomirov@42: * nodeid.equalsTo(dh.asBinary()); tikhomirov@42: *tikhomirov@9: * @author artem tikhomirov@9: */ tikhomirov@9: public class DigestHelper { tikhomirov@9: private MessageDigest sha1; tikhomirov@42: private byte[] digest; tikhomirov@9: tikhomirov@9: public DigestHelper() { tikhomirov@9: } tikhomirov@9: tikhomirov@9: private MessageDigest getSHA1() { tikhomirov@9: if (sha1 == null) { tikhomirov@9: try { tikhomirov@9: sha1 = MessageDigest.getInstance("SHA-1"); tikhomirov@9: } catch (NoSuchAlgorithmException ex) { tikhomirov@9: // could hardly happen, JDK from Sun always has sha1. tikhomirov@9: ex.printStackTrace(); // FIXME log error tikhomirov@9: } tikhomirov@9: } tikhomirov@9: return sha1; tikhomirov@9: } tikhomirov@9: tikhomirov@41: tikhomirov@42: public DigestHelper sha1(Nodeid nodeid1, Nodeid nodeid2, byte[] data) { tikhomirov@41: return sha1(nodeid1.cloneData(), nodeid2.cloneData(), data); tikhomirov@41: } tikhomirov@41: tikhomirov@41: // sha1_digest(min(p1,p2) ++ max(p1,p2) ++ final_text) tikhomirov@42: public DigestHelper sha1(byte[] nodeidParent1, byte[] nodeidParent2, byte[] data) { tikhomirov@9: MessageDigest alg = getSHA1(); tikhomirov@41: if ((nodeidParent1[0] & 0x00FF) < (nodeidParent2[0] & 0x00FF)) { tikhomirov@41: alg.update(nodeidParent1); tikhomirov@41: alg.update(nodeidParent2); tikhomirov@41: } else { tikhomirov@41: alg.update(nodeidParent2); tikhomirov@41: alg.update(nodeidParent1); tikhomirov@41: } tikhomirov@42: digest = alg.digest(data); tikhomirov@9: assert digest.length == 20; tikhomirov@42: return this; tikhomirov@42: } tikhomirov@42: tikhomirov@42: public String asHexString() { tikhomirov@42: if (digest == null) { tikhomirov@42: throw new IllegalStateException("Shall init with sha1() call first"); tikhomirov@42: } tikhomirov@42: return toHexString(digest, 0, digest.length); tikhomirov@42: } tikhomirov@42: tikhomirov@42: // by reference, be careful not to modify (or #clone() if needed) tikhomirov@42: public byte[] asBinary() { tikhomirov@42: if (digest == null) { tikhomirov@42: throw new IllegalStateException("Shall init with sha1() call first"); tikhomirov@42: } tikhomirov@42: return digest; tikhomirov@9: } tikhomirov@9: tikhomirov@41: // XXX perhaps, digest functions should throw an exception, as it's caller responsibility to deal with eof, etc tikhomirov@42: public DigestHelper sha1(InputStream is /*ByteBuffer*/) throws IOException { tikhomirov@9: MessageDigest alg = getSHA1(); tikhomirov@9: byte[] buf = new byte[1024]; tikhomirov@9: int c; tikhomirov@9: while ((c = is.read(buf)) != -1) { tikhomirov@9: alg.update(buf, 0, c); tikhomirov@9: } tikhomirov@42: digest = alg.digest(); tikhomirov@42: return this; tikhomirov@9: } tikhomirov@9: tikhomirov@31: public static String toHexString(byte[] data, final int offset, final int count) { tikhomirov@9: char[] result = new char[count << 1]; tikhomirov@9: final String hexDigits = "0123456789abcdef"; tikhomirov@9: final int end = offset+count; tikhomirov@9: for (int i = offset, j = 0; i < end; i++) { tikhomirov@9: result[j++] = hexDigits.charAt((data[i] >>> 4) & 0x0F); tikhomirov@9: result[j++] = hexDigits.charAt(data[i] & 0x0F); tikhomirov@9: } tikhomirov@9: return new String(result); tikhomirov@9: } tikhomirov@9: }