tikhomirov@31: /* tikhomirov@74: * Copyright (c) 2010-2011 TMate Software Ltd tikhomirov@74: * tikhomirov@74: * This program is free software; you can redistribute it and/or modify tikhomirov@74: * it under the terms of the GNU General Public License as published by tikhomirov@74: * the Free Software Foundation; version 2 of the License. tikhomirov@74: * tikhomirov@74: * This program is distributed in the hope that it will be useful, tikhomirov@74: * but WITHOUT ANY WARRANTY; without even the implied warranty of tikhomirov@74: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the tikhomirov@74: * GNU General Public License for more details. tikhomirov@74: * tikhomirov@74: * For information on how to redistribute this software under tikhomirov@74: * the terms of a license other than GNU General Public License tikhomirov@74: * contact TMate Software at support@svnkit.com tikhomirov@9: */ tikhomirov@74: package org.tmatesoft.hg.internal; 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@74: import org.tmatesoft.hg.core.Nodeid; tikhomirov@74: tikhomirov@74: 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@74: * tikhomirov@74: * @author Artem Tikhomirov tikhomirov@74: * @author TMate Software Ltd. 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@74: return sha1(nodeid1.toByteArray(), nodeid2.toByteArray(), 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: }