tikhomirov@31: /* tikhomirov@423: * Copyright (c) 2010-2012 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@102: * contact TMate Software at support@hg4j.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@423: import org.tmatesoft.hg.repo.HgInvalidStateException; 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@423: HgInvalidStateException t = new HgInvalidStateException("Need SHA-1 algorithm for nodeid calculation"); tikhomirov@423: t.initCause(ex); tikhomirov@423: throw t; 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@83: tikhomirov@83: public DigestHelper sha1(CharSequence... seq) { tikhomirov@83: MessageDigest alg = getSHA1(); tikhomirov@83: for (CharSequence s : seq) { tikhomirov@83: byte[] b = s.toString().getBytes(); tikhomirov@83: alg.update(b); tikhomirov@83: } tikhomirov@83: digest = alg.digest(); tikhomirov@83: return this; tikhomirov@83: } 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@266: tikhomirov@266: public static boolean ascii2bin(byte[] ascii, int offset, int len, byte[] binary) { tikhomirov@266: assert len % 2 == 0; tikhomirov@266: assert binary.length >= len >>> 1; tikhomirov@266: tikhomirov@266: boolean zeroBytes = true; tikhomirov@266: for (int i = 0, j = offset; i < len >>> 1; i++) { tikhomirov@266: int b = ascii[j++] & 0xCF; // -0x30 to get decimal digit out from their char, and to uppercase if a letter tikhomirov@266: int hiNibble = b > 64 ? b - 55 : b; tikhomirov@266: b = ascii[j++] & 0xCF; tikhomirov@266: int lowNibble = b > 64 ? b - 55 : b; tikhomirov@266: if (hiNibble >= 16 || lowNibble >= 16) { tikhomirov@266: throw new IllegalArgumentException(String.format("Characters '%c%c' (%1$d and %2$d) at index %d are not valid hex digits", ascii[j-2], ascii[j-1], j-2)); tikhomirov@266: } tikhomirov@266: b = (((hiNibble << 4) | lowNibble) & 0xFF); tikhomirov@266: binary[i] = (byte) b; tikhomirov@266: zeroBytes = zeroBytes && b == 0; tikhomirov@266: } tikhomirov@266: return zeroBytes; tikhomirov@266: } tikhomirov@9: }