@Override public byte[] fingerprint() { synchronized (publicKey) { if (_fingerprint == null) { _fingerprint = new byte[33]; _fingerprint[0] = (byte) FINGERPRINT_SHA256; System.arraycopy( updateDigestWithKeyComponents(new Sha256()).digest(), 0, _fingerprint, 1, 32); } return _fingerprint; } }
@Override protected byte[] _digest() { byte[] strLength = new Long(length).toString().getBytes(); sha.update(strLength); crc.update(strLength); byte[] s1 = sha.digest(); byte[] s2 = crc.update(s1).digest(); byte[] syntex = Arrays.copyOf(s1, 36); System.arraycopy(s2, 0, syntex, 32, 4); return syntex; }
assertArrayEquals(vector.getKey(), Hex.decode(vector.getValue()), new Sha256().digest(vector.getKey())); assertEquals(64, new Sha256().getChunkSize()); assertEquals(128, new Sha512().getChunkSize()); assertEquals(128, new Sha512_256().getChunkSize()); assertEquals(32, new Sha256().getLength()); assertEquals(64, new Sha512().getLength()); assertEquals(32, new Sha512_256().getLength());
/** * Create new HMAC hash. * * @param key * secret key * @param hashClass * class of the hash to use to sign the message. SHA256+ is recommended. */ public HMAC(byte[] key, Class<? extends Digest> hashClass) { this.hashClass = hashClass; this.hash = hashInstance(); this.blockSize = hash.getChunkSize(); byte[] keyBlock; if (key.length > blockSize) { byte[] kd = new Sha256().digest(key); keyBlock = Arrays.copyOf(kd, blockSize); } else keyBlock = Arrays.copyOf(key, blockSize); oKeyPad = xor(keyBlock, 0x5c); byte[] iKeyPad = xor(keyBlock, 0x36); hash.update(iKeyPad); }
@Override protected void _update(byte[] data, int offset, int size) { length += size; sha.update(data, offset, size); crc.update(data, offset, size); }
@Test public void testHashes() throws Exception { byte[] valid = Hex.decode("ba7816bf 8f01cfea 414140de 5dae2223 b00361a3 96177a9c b410ff61" + " f20015ad"); Digest d = new Sha256(); d.update('a'); d.update("bc".getBytes()); assertArrayEquals(valid, d.digest()); // And SHA512 too valid = Hex.decode("ddaf35a193617aba cc417349ae204131 12e6fa4e89a97ea2 0a9eeee64b55d39a " + "2192992a274fc1a8 36ba3c23a3feebbd 454d4423643ce80e " + "2a9ac94fa54ca49f"); assertArrayEquals(valid, new Sha512().digest("abc")); }