Refine search
private String sha1(final File dir, final File sprite, final File css) throws IOException { try (Stream<Path> stream = Files.walk(dir.toPath())) { Hasher sha1 = Hashing.sha1().newHasher(); stream.filter(p -> !Files.isDirectory(p)) .forEach(p -> Try.run(() -> sha1.putBytes(Files.readAllBytes(p)))); if (sprite.exists()) { sha1.putBytes(Files.readAllBytes(sprite.toPath())); } if (css.exists()) { sha1.putBytes(Files.readAllBytes(css.toPath())); } return BaseEncoding.base16().encode(sha1.hash().asBytes()).toLowerCase(); } }
public void testGetBytesInternal_noCloneOccurs() { byte[] bytes = new byte[] {(byte) 0xcd, (byte) 0xab, (byte) 0x00, (byte) 0x00}; HashCode hashCode = HashCode.fromBytes(bytes); assertEquals(0x0000abcd, hashCode.asInt()); assertEquals("cdab0000", hashCode.toString()); hashCode.getBytesInternal()[0] = (byte) 0x00; assertEquals(0x0000ab00, hashCode.asInt()); assertEquals("00ab0000", hashCode.toString()); }
/** * Returns a "Java hash code" for this {@code HashCode} instance; this is well-defined (so, for * example, you can safely put {@code HashCode} instances into a {@code HashSet}) but is otherwise * probably not what you want to use. */ @Override public final int hashCode() { // If we have at least 4 bytes (32 bits), just take the first 4 bytes. Since this is // already a (presumably) high-quality hash code, any four bytes of it will do. if (bits() >= 32) { return asInt(); } // If we have less than 4 bytes, use them all. byte[] bytes = getBytesInternal(); int val = (bytes[0] & 0xFF); for (int i = 1; i < bytes.length; i++) { val |= ((bytes[i] & 0xFF) << (i * 8)); } return val; }
public void testLongWriteBytesTo() { byte[] dest = new byte[8]; HashCode.fromLong(42).writeBytesTo(dest, 0, 8); assertTrue(Arrays.equals(HashCode.fromLong(42).asBytes(), dest)); }
public void testIntWriteBytesTo() { byte[] dest = new byte[4]; HashCode.fromInt(42).writeBytesTo(dest, 0, 4); assertTrue(Arrays.equals(HashCode.fromInt(42).asBytes(), dest)); }
public Void call() throws Exception { HttpWire wire = setUp(); InputStream in = wire.input(fromServer); ByteArrayOutputStream out = new ByteArrayOutputStream();// TODO copy(in, out); byte[] compare = md5().hashBytes(out.toByteArray()).asBytes(); Thread.sleep(100); assertEquals(base16().lowerCase().encode(compare), checkNotNull(sysHttpStreamMd5, sysHttpStreamMd5)); assertEquals(((BufferLogger) wire.getWireLog()).buff.toString().getBytes().length, 3331484); return null; } }
@Test(invocationCount = 100) public void testLessThan16Bytes() throws Exception { byte[] data = randomBytes(ThreadLocalRandom.current().nextInt(16)); HashCode expected = Hashing.murmur3_128().hashBytes(data); Slice actual = Murmur3Hash128.hash(Slices.wrappedBuffer(data)); assertEquals(actual.getBytes(), expected.asBytes()); }
private void checkLoginAsTheNewUser(String expectedUsername) { LoginResponse response = globalAdminClient.getSessionClient().loginUserInDomainWithHashOfPassword( expectedUsername, "", base16().lowerCase().encode(md5().hashString("password", UTF_8).asBytes())); assertNotNull(response); assertNotNull(response.getSessionKey()); assertNotNull(response.getJSessionId()); client.getSessionClient().logoutUser(response.getSessionKey()); } }
private static void setEncryptionHeaders( HttpHeaders headers, String headerPrefix, Map<Option, ?> options) { String key = Option.CUSTOMER_SUPPLIED_KEY.getString(options); if (key != null) { BaseEncoding base64 = BaseEncoding.base64(); HashFunction hashFunction = Hashing.sha256(); headers.set(headerPrefix + "algorithm", "AES256"); headers.set(headerPrefix + "key", key); headers.set( headerPrefix + "key-sha256", base64.encode(hashFunction.hashBytes(base64.decode(key)).asBytes())); } }
private void assertValidMd5(final InputStream input) throws IOException { assertEquals(base64().encode(new ByteSource() { @Override public InputStream openStream() { return input; } }.hash(md5()).asBytes()), md5); }
@Test(invocationCount = 100) public void testLessThan4Bytes() throws Exception { byte[] data = randomBytes(ThreadLocalRandom.current().nextInt(4)); int expected = Hashing.murmur3_32().hashBytes(data).asInt(); int actual = Murmur3Hash32.hash(Slices.wrappedBuffer(data)); assertEquals(actual, expected); }
@Test public void testZoneKeyData() { Hasher hasher = Hashing.murmur3_128().newHasher(); SortedSet<TimeZoneKey> timeZoneKeysSortedByKey = ImmutableSortedSet.copyOf(new Comparator<TimeZoneKey>() { @Override public int compare(TimeZoneKey left, TimeZoneKey right) { return Short.compare(left.getKey(), right.getKey()); } }, TimeZoneKey.getTimeZoneKeys()); for (TimeZoneKey timeZoneKey : timeZoneKeysSortedByKey) { hasher.putShort(timeZoneKey.getKey()); hasher.putString(timeZoneKey.getId(), StandardCharsets.UTF_8); } // Zone file should not (normally) be changed, so let's make this more difficult assertEquals(hasher.hash().asLong(), -4582158485614614451L, "zone-index.properties file contents changed!"); }
@Override public void run() { byte[] hash = Hashing.sha256().hashBytes(data).asBytes(); String hashStr = BaseEncoding.base16().encode(hash); archive.setHash(hash); String path = new StringBuilder() .append(hashStr.substring(0, 2)) .append('/') .append(hashStr.substring(2)) .toString(); try { try (InputStream in = minioClient.getObject(minioBucket, path)) { return; // already exists } catch (ErrorResponseException ex) { // doesn't exist } minioClient.putObject(minioBucket, path, new ByteArrayInputStream(data), data.length, "binary/octet-stream"); } catch (ErrorResponseException | InsufficientDataException | InternalException | InvalidArgumentException | InvalidBucketNameException | NoResponseException | IOException | InvalidKeyException | NoSuchAlgorithmException | XmlPullParserException ex) { logger.warn("unable to upload data to store", ex); } }
@Override public Blob create(BlobInfo blobInfo, byte[] content, BlobTargetOption... options) { content = firstNonNull(content, EMPTY_BYTE_ARRAY); BlobInfo updatedInfo = blobInfo .toBuilder() .setMd5(BaseEncoding.base64().encode(Hashing.md5().hashBytes(content).asBytes())) .setCrc32c( BaseEncoding.base64() .encode(Ints.toByteArray(Hashing.crc32c().hashBytes(content).asInt()))) .build(); return internalCreate(updatedInfo, content, options); }
@Test public void testMD5PayloadEnclosing() throws IOException { Payload payload = Payloads.newPayload("foo"); HttpMessage payloadEnclosing = HttpMessage.builder().payload(payload).build(); assertEquals(fn.apply(payloadEnclosing), md5().hashString("foo", UTF_8).asBytes()); }
private String sha1(final CharSequence source) { return BaseEncoding.base16() .encode(Hashing .sha1() .hashString(source, charset) .asBytes()) .substring(0, 8).toLowerCase(); }
@Test(dependsOnMethods = "uploadMultipartBlob", singleThreaded = true) public void downloadParallelBlob() throws IOException { final File downloadedFile = new File(bigFile + ".downloaded"); blobStore.downloadBlob(CONTAINER, bigFile.getName(), downloadedFile, executor); String eTag = Files.hash(downloadedFile, Hashing.md5()).toString(); assertEquals(eTag, etag); }
public void testPutAfterHash() { Hasher sha1 = Hashing.sha1().newHasher(); assertEquals( "2fd4e1c67a2d28fced849ee1bb76e7391b93eb12", sha1.putString("The quick brown fox jumps over the lazy dog", Charsets.UTF_8) .hash() .toString()); try { sha1.putInt(42); fail(); } catch (IllegalStateException expected) { } }