kitaev@213: /* kitaev@213: * Copyright (c) 2010-2011 TMate Software Ltd kitaev@213: * kitaev@213: * This program is free software; you can redistribute it and/or modify kitaev@213: * it under the terms of the GNU General Public License as published by kitaev@213: * the Free Software Foundation; version 2 of the License. kitaev@213: * kitaev@213: * This program is distributed in the hope that it will be useful, kitaev@213: * but WITHOUT ANY WARRANTY; without even the implied warranty of kitaev@213: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the kitaev@213: * GNU General Public License for more details. kitaev@213: * kitaev@213: * For information on how to redistribute this software under kitaev@213: * the terms of a license other than GNU General Public License kitaev@213: * contact TMate Software at support@hg4j.com kitaev@213: */ kitaev@213: package org.tmatesoft.hg.internal; kitaev@213: kitaev@213: import java.io.IOException; kitaev@213: import java.io.InputStream; kitaev@213: import java.security.MessageDigest; kitaev@213: import java.security.NoSuchAlgorithmException; kitaev@213: kitaev@213: import org.tmatesoft.hg.core.Nodeid; kitaev@213: kitaev@213: kitaev@213: /** kitaev@213: *
kitaev@213:  * DigestHelper dh;
kitaev@213:  * dh.sha1(...).asHexString();
kitaev@213:  *  or 
kitaev@213:  * dh = dh.sha1(...);
kitaev@213:  * nodeid.equalsTo(dh.asBinary());
kitaev@213:  * 
kitaev@213: * kitaev@213: * @author Artem Tikhomirov kitaev@213: * @author TMate Software Ltd. kitaev@213: */ kitaev@213: public class DigestHelper { kitaev@213: private MessageDigest sha1; kitaev@213: private byte[] digest; kitaev@213: kitaev@213: public DigestHelper() { kitaev@213: } kitaev@213: kitaev@213: private MessageDigest getSHA1() { kitaev@213: if (sha1 == null) { kitaev@213: try { kitaev@213: sha1 = MessageDigest.getInstance("SHA-1"); kitaev@213: } catch (NoSuchAlgorithmException ex) { kitaev@213: // could hardly happen, JDK from Sun always has sha1. kitaev@213: ex.printStackTrace(); // FIXME log error kitaev@213: } kitaev@213: } kitaev@213: return sha1; kitaev@213: } kitaev@213: kitaev@213: kitaev@213: public DigestHelper sha1(Nodeid nodeid1, Nodeid nodeid2, byte[] data) { kitaev@213: return sha1(nodeid1.toByteArray(), nodeid2.toByteArray(), data); kitaev@213: } kitaev@213: kitaev@213: // sha1_digest(min(p1,p2) ++ max(p1,p2) ++ final_text) kitaev@213: public DigestHelper sha1(byte[] nodeidParent1, byte[] nodeidParent2, byte[] data) { kitaev@213: MessageDigest alg = getSHA1(); kitaev@213: if ((nodeidParent1[0] & 0x00FF) < (nodeidParent2[0] & 0x00FF)) { kitaev@213: alg.update(nodeidParent1); kitaev@213: alg.update(nodeidParent2); kitaev@213: } else { kitaev@213: alg.update(nodeidParent2); kitaev@213: alg.update(nodeidParent1); kitaev@213: } kitaev@213: digest = alg.digest(data); kitaev@213: assert digest.length == 20; kitaev@213: return this; kitaev@213: } kitaev@213: kitaev@213: public String asHexString() { kitaev@213: if (digest == null) { kitaev@213: throw new IllegalStateException("Shall init with sha1() call first"); kitaev@213: } kitaev@213: return toHexString(digest, 0, digest.length); kitaev@213: } kitaev@213: kitaev@213: // by reference, be careful not to modify (or #clone() if needed) kitaev@213: public byte[] asBinary() { kitaev@213: if (digest == null) { kitaev@213: throw new IllegalStateException("Shall init with sha1() call first"); kitaev@213: } kitaev@213: return digest; kitaev@213: } kitaev@213: kitaev@213: // XXX perhaps, digest functions should throw an exception, as it's caller responsibility to deal with eof, etc kitaev@213: public DigestHelper sha1(InputStream is /*ByteBuffer*/) throws IOException { kitaev@213: MessageDigest alg = getSHA1(); kitaev@213: byte[] buf = new byte[1024]; kitaev@213: int c; kitaev@213: while ((c = is.read(buf)) != -1) { kitaev@213: alg.update(buf, 0, c); kitaev@213: } kitaev@213: digest = alg.digest(); kitaev@213: return this; kitaev@213: } kitaev@213: kitaev@213: public DigestHelper sha1(CharSequence... seq) { kitaev@213: MessageDigest alg = getSHA1(); kitaev@213: for (CharSequence s : seq) { kitaev@213: byte[] b = s.toString().getBytes(); kitaev@213: alg.update(b); kitaev@213: } kitaev@213: digest = alg.digest(); kitaev@213: return this; kitaev@213: } kitaev@213: kitaev@213: public static String toHexString(byte[] data, final int offset, final int count) { kitaev@213: char[] result = new char[count << 1]; kitaev@213: final String hexDigits = "0123456789abcdef"; kitaev@213: final int end = offset+count; kitaev@213: for (int i = offset, j = 0; i < end; i++) { kitaev@213: result[j++] = hexDigits.charAt((data[i] >>> 4) & 0x0F); kitaev@213: result[j++] = hexDigits.charAt(data[i] & 0x0F); kitaev@213: } kitaev@213: return new String(result); kitaev@213: } kitaev@213: }