com.google.common.hash.HashingOutputStream Java Examples

The following examples show how to use com.google.common.hash.HashingOutputStream. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HttpCacheClient.java    From bazel with Apache License 2.0 6 votes vote down vote up
@Override
public ListenableFuture<Void> downloadBlob(Digest digest, OutputStream out) {
  final HashingOutputStream hashOut =
      verifyDownloads ? digestUtil.newHashingOutputStream(out) : null;
  return Futures.transformAsync(
      get(digest, hashOut != null ? hashOut : out, /* casDownload= */ true),
      (v) -> {
        try {
          if (hashOut != null) {
            Utils.verifyBlobContents(
                digest.getHash(), DigestUtil.hashCodeToString(hashOut.hash()));
          }
          out.flush();
          return Futures.immediateFuture(null);
        } catch (IOException e) {
          return Futures.immediateFailedFuture(e);
        }
      },
      MoreExecutors.directExecutor());
}
 
Example #2
Source File: DiskCacheClient.java    From bazel with Apache License 2.0 6 votes vote down vote up
@Override
public ListenableFuture<Void> downloadBlob(Digest digest, OutputStream out) {
  @Nullable
  HashingOutputStream hashOut = verifyDownloads ? digestUtil.newHashingOutputStream(out) : null;
  return Futures.transformAsync(
      download(digest, hashOut != null ? hashOut : out, /* isActionCache= */ false),
      (v) -> {
        try {
          if (hashOut != null) {
            Utils.verifyBlobContents(
                digest.getHash(), DigestUtil.hashCodeToString(hashOut.hash()));
          }
          out.flush();
          return Futures.immediateFuture(null);
        } catch (IOException e) {
          return Futures.immediateFailedFuture(e);
        }
      },
      MoreExecutors.directExecutor());
}
 
Example #3
Source File: ThriftArtifactCacheProtocol.java    From buck with Apache License 2.0 6 votes vote down vote up
private static String computeHash(ByteSource source, HashFunction hashFunction)
    throws IOException {
  try (InputStream inputStream = source.openStream();
      HashingOutputStream outputStream =
          new HashingOutputStream(
              hashFunction,
              new OutputStream() {
                @Override
                public void write(int b) {
                  // Do nothing.
                }
              })) {
    ByteStreams.copy(inputStream, outputStream);
    return outputStream.hash().toString();
  }
}
 
Example #4
Source File: ThriftArtifactCacheProtocol.java    From buck with Apache License 2.0 6 votes vote down vote up
public ReadPayloadInfo readPayload(OutputStream outStream) throws IOException {
  assertTrue(
      nextPayloadToBeRead < thriftData.getPayloadsSize(),
      "Trying to download payload index=[%s] but the thriftData only contains [%s] payloads.",
      nextPayloadToBeRead,
      thriftData.getPayloadsSize());

  long payloadSizeBytes =
      assertNotNull(thriftData.getPayloads(), "Payloads[] cannot be null.")
          .get(nextPayloadToBeRead)
          .getSizeBytes();
  try (HashingOutputStream wrappedOutputStream =
      new HashingOutputStream(MD5_HASH_FUNCTION, outStream)) {
    copyExactly(responseStream, wrappedOutputStream, payloadSizeBytes);
    ++nextPayloadToBeRead;
    return new ReadPayloadInfo(payloadSizeBytes, wrappedOutputStream.hash().toString());
  }
}
 
Example #5
Source File: MavenIOUtils.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
public static HashedPayload createStreamPayload(
    final Path path, final String contentType,
    final Writer writer) throws IOException
{
  Map<HashAlgorithm, HashingOutputStream> hashingStreams = writeToPath(path, writer);
  Map<HashAlgorithm, HashCode> hashCodes = generateHashCodes(hashingStreams);
  return new HashedPayload(aStreamPayload(path, contentType), hashCodes);
}
 
Example #6
Source File: MavenIOUtils.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
private static Map<HashAlgorithm, HashCode> generateHashCodes(
    final Map<HashAlgorithm, HashingOutputStream> hashingStreams)
{
  Map<HashAlgorithm, HashCode> hashCodes = new HashMap<>();
  for (Entry<HashAlgorithm, HashingOutputStream> entry : hashingStreams.entrySet()) {
    hashCodes.put(entry.getKey(), entry.getValue().hash());
  }
  return hashCodes;
}
 
Example #7
Source File: Binary.java    From activitystreams with Apache License 2.0 5 votes vote down vote up
/**
 * Set the input data the given Compression. Will automatically
 * set calculate the md5 sum and length properties
 * @param in InputStream
 * @return Builder
 * @throws IOException
 */
public Builder data(
  InputStream in, 
  Compression<?,?> compression) 
    throws IOException {
  StringWriter writer = new StringWriter();
  OutputStream out = 
    BaseEncoding.base64Url().encodingStream(writer);
  if (compression != null)
    out = compression.compressor(out);
  HashingOutputStream hout = 
    new HashingOutputStream(
      Hashing.md5(), out);
  byte[] buf = new byte[1024];
  int r = -1;
  long size = 0;
  while((r = in.read(buf)) > -1) {
    hout.write(buf,0,r);
    size += r;
  }
  set("length", size);
  if (compression != null) {
    set("compression", compression.label());
    compression.finish(out);
  }
  hout.close();
  set("md5", hout.hash().toString());
  return set("data",writer.toString());
}
 
Example #8
Source File: GrpcCacheClient.java    From bazel with Apache License 2.0 5 votes vote down vote up
@Override
public ListenableFuture<Void> downloadBlob(Digest digest, OutputStream out) {
  if (digest.getSizeBytes() == 0) {
    return Futures.immediateFuture(null);
  }

  @Nullable Supplier<HashCode> hashSupplier = null;
  if (options.remoteVerifyDownloads) {
    HashingOutputStream hashOut = digestUtil.newHashingOutputStream(out);
    hashSupplier = hashOut::hash;
    out = hashOut;
  }

  return downloadBlob(digest, out, hashSupplier);
}
 
Example #9
Source File: DigestUtil.java    From bazel-buildfarm with Apache License 2.0 4 votes vote down vote up
public HashingOutputStream newHashingOutputStream(OutputStream out) {
  return new HashingOutputStream(hashFn.getHash(), out);
}
 
Example #10
Source File: ClientLoader.java    From runelite with BSD 2-Clause "Simplified" License 4 votes vote down vote up
private void applyPatch() throws IOException
{
	byte[] vanillaHash = new byte[64];
	byte[] appliedPatchHash = new byte[64];

	try (InputStream is = ClientLoader.class.getResourceAsStream("/client.serial"))
	{
		if (is == null)
		{
			SwingUtilities.invokeLater(() ->
				new FatalErrorDialog("The client-patch is missing from the classpath. If you are building " +
					"the client you need to re-run maven")
					.addBuildingGuide()
					.open());
			throw new NullPointerException();
		}

		DataInputStream dis = new DataInputStream(is);
		dis.readFully(vanillaHash);
		dis.readFully(appliedPatchHash);
	}

	byte[] vanillaCacheHash = Files.asByteSource(VANILLA_CACHE).hash(Hashing.sha512()).asBytes();
	if (!Arrays.equals(vanillaHash, vanillaCacheHash))
	{
		log.info("Client is outdated!");
		updateCheckMode = VANILLA;
		return;
	}

	if (PATCHED_CACHE.exists())
	{
		byte[] diskBytes = Files.asByteSource(PATCHED_CACHE).hash(Hashing.sha512()).asBytes();
		if (!Arrays.equals(diskBytes, appliedPatchHash))
		{
			log.warn("Cached patch hash mismatches, regenerating patch");
		}
		else
		{
			log.info("Using cached patched client");
			return;
		}
	}

	try (HashingOutputStream hos = new HashingOutputStream(Hashing.sha512(), new FileOutputStream(PATCHED_CACHE));
		InputStream patch = ClientLoader.class.getResourceAsStream("/client.patch"))
	{
		new FileByFileV1DeltaApplier().applyDelta(VANILLA_CACHE, patch, hos);

		if (!Arrays.equals(hos.hash().asBytes(), appliedPatchHash))
		{
			log.error("Patched client hash mismatch");
			updateCheckMode = VANILLA;
			return;
		}
	}
	catch (IOException e)
	{
		log.error("Unable to apply patch despite hash matching", e);
		updateCheckMode = VANILLA;
		return;
	}
}
 
Example #11
Source File: DigestUtil.java    From bazel with Apache License 2.0 4 votes vote down vote up
public HashingOutputStream newHashingOutputStream(OutputStream out) {
  return new HashingOutputStream(hashFn.getHashFunction(), out);
}