Java Code Examples for com.google.common.hash.HashingInputStream

The following examples show how to use com.google.common.hash.HashingInputStream. These examples are extracted from open source projects. 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 Project: onos   Source File: DefaultPiPipeconf.java    License: Apache License 2.0 6 votes vote down vote up
private long generateFingerprint(Collection<URL> extensions) {
    Collection<Integer> hashes = new ArrayList<>();
    for (URL extUrl : extensions) {
        try {
            HashingInputStream hin = new HashingInputStream(
                    Hashing.crc32(), extUrl.openStream());
            //noinspection StatementWithEmptyBody
            while (hin.read() != -1) {
                // Do nothing. Reading all input stream to update hash.
            }
            hashes.add(hin.hash().asInt());
        } catch (IOException e) {
            throw new IllegalArgumentException(e);
        }
    }
    //  FIXME: how to include behaviours in the hash?
    int low = Arrays.hashCode(hashes.toArray());
    int high = pipelineModel.hashCode();
    return ByteBuffer.allocate(8).putInt(high).putInt(low).getLong(0);
}
 
Example 2
Source Project: ServerListPlus   Source File: SnakeYAML.java    License: GNU General Public License v3.0 6 votes vote down vote up
public static Path load(Path pluginFolder) throws IOException {
    Path libFolder = pluginFolder.resolve("lib");
    Path path = libFolder.resolve(SNAKE_YAML_JAR);

    if (Files.notExists(path)) {
        Files.createDirectories(libFolder);

        URL url = new URL(SNAKE_YAML);

        String hash;

        try (HashingInputStream his = new HashingInputStream(Hashing.sha1(), url.openStream());
             ReadableByteChannel source = Channels.newChannel(his);
             FileChannel out = FileChannel.open(path, StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE)) {
            out.transferFrom(source, 0, Long.MAX_VALUE);
            hash = his.hash().toString();
        }

        if (!hash.equals(EXPECTED_HASH)) {
            Files.delete(path);
            throw new IOException("Hash mismatch in " + SNAKE_YAML_JAR + ": expected " + EXPECTED_HASH);
        }
    }

    return path;
}
 
Example 3
Source Project: nexus-public   Source File: Hashes.java    License: Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Computes the hash of the given stream using the given algorithm.
 */
public static HashCode hash(final HashAlgorithm algorithm, final InputStream inputStream) throws IOException {
  checkNotNull(algorithm);
  checkNotNull(inputStream);

  try (HashingInputStream hashingStream = new HashingInputStream(algorithm.function(), inputStream)) {
    ByteStreams.copy(hashingStream, ByteStreams.nullOutputStream());
    return hashingStream.hash();
  }
}
 
Example 4
Source Project: james-project   Source File: ObjectStorageBlobStore.java    License: Apache License 2.0 5 votes vote down vote up
private Mono<BlobId> saveBigStream(BucketName bucketName, InputStream data) {
    ObjectStorageBucketName resolvedBucketName = bucketNameResolver.resolve(bucketName);

    return Mono.fromCallable(blobIdFactory::randomId)
        .flatMap(tmpId -> {
            HashingInputStream hashingInputStream = new HashingInputStream(Hashing.sha256(), data);
            Payload payload = payloadCodec.write(hashingInputStream);
            Blob blob = blobStore.blobBuilder(tmpId.asString())
                .payload(payload.getPayload())
                .build();

            Supplier<BlobId> blobIdSupplier = () -> blobIdFactory.from(hashingInputStream.hash().toString());
            return blobPutter.putAndComputeId(resolvedBucketName, blob, blobIdSupplier);
        });
}
 
Example 5
Source Project: james-project   Source File: CassandraBlobStore.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Mono<BlobId> save(BucketName bucketName, InputStream data, StoragePolicy storagePolicy) {
    Preconditions.checkNotNull(bucketName);
    Preconditions.checkNotNull(data);
    HashingInputStream hashingInputStream = new HashingInputStream(Hashing.sha256(), data);
    return Mono.using(
        () -> new FileBackedOutputStream(FILE_THRESHOLD),
        fileBackedOutputStream -> saveAndGenerateBlobId(bucketName, hashingInputStream, fileBackedOutputStream),
        Throwing.consumer(FileBackedOutputStream::reset).sneakyThrow(),
        LAZY_RESOURCE_CLEANUP);
}
 
Example 6
Source Project: james-project   Source File: CassandraBlobStore.java    License: Apache License 2.0 5 votes vote down vote up
private Mono<BlobId> saveAndGenerateBlobId(BucketName bucketName, HashingInputStream hashingInputStream, FileBackedOutputStream fileBackedOutputStream) {
    return Mono.fromCallable(() -> {
        IOUtils.copy(hashingInputStream, fileBackedOutputStream);
        return Tuples.of(blobIdFactory.from(hashingInputStream.hash().toString()), fileBackedOutputStream.asByteSource());
    })
        .flatMap(tuple -> dumbBlobStore.save(bucketName, tuple.getT1(), tuple.getT2()).thenReturn(tuple.getT1()));
}
 
Example 7
Source Project: buck   Source File: CustomJarOutputStreamTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void manifestContainsEntryHashesOfHashedEntries() throws IOException {
  String entryName = "A";
  InputStream contents = new ByteArrayInputStream("contents".getBytes(StandardCharsets.UTF_8));
  try (HashingInputStream hashingContents =
      new HashingInputStream(Hashing.murmur3_128(), contents)) {
    writer.writeEntry(entryName, hashingContents);
    writer.close();

    String expectedHash = hashingContents.hash().toString();
    assertEntryHash(entryName, expectedHash);
  }
}
 
Example 8
Source Project: buck   Source File: CustomJarOutputStreamTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void manifestContainsEntryHashesOfEmptyHashedEntries() throws IOException {
  String entryName = "A";
  InputStream contents = new ByteArrayInputStream(new byte[0]);
  try (HashingInputStream hashingContents =
      new HashingInputStream(Hashing.murmur3_128(), contents)) {
    writer.putNextEntry(new CustomZipEntry(entryName));
    writer.closeEntry();
    writer.close();

    String expectedHash = hashingContents.hash().toString();
    assertEntryHash(entryName, expectedHash);
  }
}
 
Example 9
/** Set this location of extracted desugar jar that is used for processing. */
private static void initDesugarJar(@Nullable FileCache cache) throws IOException {
    if (isDesugarJarInitialized()) {
        return;
    }

    URL url = DesugarProcessBuilder.class.getClassLoader().getResource(DESUGAR_JAR);
    Preconditions.checkNotNull(url);

    Path extractedDesugar = null;
    if (cache != null) {
        try {
            String fileHash;
            try (HashingInputStream stream =
                         new HashingInputStream(Hashing.sha256(), url.openStream())) {
                fileHash = stream.hash().toString();
            }
            FileCache.Inputs inputs =
                    new FileCache.Inputs.Builder(FileCache.Command.EXTRACT_DESUGAR_JAR)
                            .putString("pluginVersion", Version.ANDROID_GRADLE_PLUGIN_VERSION)
                            .putString("jarUrl", url.toString())
                            .putString("fileHash", fileHash)
                            .build();

            File cachedFile =
                    cache.createFileInCacheIfAbsent(
                            inputs, file -> copyDesugarJar(url, file.toPath()))
                            .getCachedFile();
            Preconditions.checkNotNull(cachedFile);
            extractedDesugar = cachedFile.toPath();
        } catch (IOException | ExecutionException e) {
            logger.error(e, "Unable to cache Desugar jar. Extracting to temp dir.");
        }
    }

    synchronized (desugarJar) {
        if (isDesugarJarInitialized()) {
            return;
        }

        if (extractedDesugar == null) {
            extractedDesugar = PathUtils.createTmpToRemoveOnShutdown(DESUGAR_JAR);
            copyDesugarJar(url, extractedDesugar);
        }
        desugarJar.set(extractedDesugar);
    }
}
 
Example 10
Source Project: atlas   Source File: AtlasDesugarTransform.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Set this location of extracted desugar jar that is used for processing.
 */
private static void initDesugarJar(@Nullable FileCache cache) throws IOException {
    if (isDesugarJarInitialized()) {
        return;
    }

    URL url = DesugarProcessBuilder.class.getClassLoader().getResource(DESUGAR_JAR);
    Preconditions.checkNotNull(url);

    Path extractedDesugar = null;
    if (cache != null) {
        try {
            String fileHash;
            try (HashingInputStream stream =
                         new HashingInputStream(Hashing.sha256(), url.openStream())) {
                fileHash = stream.hash().toString();
            }
            FileCache.Inputs inputs =
                    new FileCache.Inputs.Builder(FileCache.Command.EXTRACT_DESUGAR_JAR)
                            .putString("pluginVersion", Version.ANDROID_GRADLE_PLUGIN_VERSION)
                            .putString("jarUrl", url.toString())
                            .putString("fileHash", fileHash)
                            .build();

            File cachedFile =
                    cache.createFileInCacheIfAbsent(
                            inputs, file -> copyDesugarJar(url, file.toPath()))
                            .getCachedFile();
            Preconditions.checkNotNull(cachedFile);
            extractedDesugar = cachedFile.toPath();
        } catch (IOException | ExecutionException e) {
            logger.error(e, "Unable to cache Desugar jar. Extracting to temp dir.");
        }
    }

    synchronized (desugarJar) {
        if (isDesugarJarInitialized()) {
            return;
        }

        if (extractedDesugar == null) {
            extractedDesugar = PathUtils.createTmpToRemoveOnShutdown(DESUGAR_JAR);
            copyDesugarJar(url, extractedDesugar);
        }
        desugarJar.set(extractedDesugar);
    }
}
 
Example 11
Source Project: buck   Source File: JarDumper.java    License: Apache License 2.0 4 votes vote down vote up
private Stream<String> dumpBinaryFile(String name, InputStream inputStream) throws IOException {
  try (HashingInputStream is = new HashingInputStream(Hashing.murmur3_128(), inputStream)) {
    ByteStreams.exhaust(is);
    return Stream.of(String.format("Murmur3-128 of %s: %s", name, is.hash().toString()));
  }
}