com.google.common.hash.HashCode Java Examples

The following examples show how to use com.google.common.hash.HashCode. 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: SslContextStoreTest.java    From hivemq-community-edition with Apache License 2.0 6 votes vote down vote up
@Test(expected = FileNotFoundException.class)
public void test_hash_key_and_trust_store_throws_exception() throws Exception {
    final File keystore = keyStoreGenerator.generateKeyStore("test", "JKS", "pw", "pkpw");
    final File trust = keyStoreGenerator.generateKeyStore("test", "JKS", "pw", "pkpw");

    final Tls tls = new Tls.Builder()
            .withKeystorePath(keystore.getAbsolutePath())
            .withKeystoreType("JKS")
            .withKeystorePassword("pw")
            .withPrivateKeyPassword("pkpw")
            .withProtocols(new ArrayList<>())
            .withTruststorePath(trust.getAbsolutePath() + "/bad")
            .withTruststoreType("JKS")
            .withTruststorePassword("pw")
            .withClientAuthMode(Tls.ClientAuthMode.NONE)
            .withCipherSuites(new ArrayList<>())
            .withHandshakeTimeout(10)
            .build();

    final HashCode firstHash = SslContextStore.hashTrustAndKeyStore(tls);
}
 
Example #2
Source File: WatchedFileHashCacheTest.java    From buck with Apache License 2.0 6 votes vote down vote up
@Test
public void whenNotifiedOfParentChangeEventCacheEntryIsRemoved() throws IOException {
  ProjectFilesystem filesystem = new FakeProjectFilesystem();
  WatchedFileHashCache cache = new WatchedFileHashCache(filesystem, fileHashCacheMode);
  Path parent = filesystem.getPath("directory");
  Path path = parent.resolve("SomeClass.java");
  filesystem.mkdirs(parent);
  filesystem.touch(path);

  HashCodeAndFileType value = HashCodeAndFileType.ofFile(HashCode.fromInt(42));
  cache.fileHashCacheEngine.put(path, value);
  cache.fileHashCacheEngine.putSize(path, 1234L);
  cache.onFileSystemChange(
      WatchmanPathEvent.of(filesystem.getRootPath(), Kind.MODIFY, RelPath.of(parent)));
  assertFalse("Cache should not contain path", cache.getIfPresent(path).isPresent());
  assertThat(
      "Cache should not contain path",
      cache.fileHashCacheEngine.getSizeIfPresent(path),
      nullValue());
}
 
Example #3
Source File: Config.java    From buck with Apache License 2.0 6 votes vote down vote up
private HashCode computeOrderIndependentHashCode() {
  ImmutableMap<String, ImmutableMap<String, String>> rawValues = rawConfig.getValues();
  ImmutableSortedMap.Builder<String, ImmutableSortedMap<String, String>> expanded =
      ImmutableSortedMap.naturalOrder();
  for (String section : rawValues.keySet()) {
    expanded.put(section, ImmutableSortedMap.copyOf(get(section)));
  }

  ImmutableSortedMap<String, ImmutableSortedMap<String, String>> sortedConfigMap =
      expanded.build();

  Hasher hasher = Hashing.sha256().newHasher();
  for (Entry<String, ImmutableSortedMap<String, String>> entry : sortedConfigMap.entrySet()) {
    hasher.putString(entry.getKey(), StandardCharsets.UTF_8);
    for (Entry<String, String> nestedEntry : entry.getValue().entrySet()) {
      hasher.putString(nestedEntry.getKey(), StandardCharsets.UTF_8);
      hasher.putString(nestedEntry.getValue(), StandardCharsets.UTF_8);
    }
  }

  return hasher.hash();
}
 
Example #4
Source File: ByteStreamUploader.java    From bazel with Apache License 2.0 6 votes vote down vote up
/** Starts a file upload an returns a future representing the upload. */
private ListenableFuture<Void> startAsyncUpload(HashCode hash, Chunker chunker) {
  try {
    chunker.reset();
  } catch (IOException e) {
    return Futures.immediateFailedFuture(e);
  }

  UUID uploadId = UUID.randomUUID();
  String resourceName = uploadResourceName(instanceName, uploadId, hash, chunker.getSize());
  AsyncUpload newUpload =
      new AsyncUpload(channel, callCredentials, callTimeoutSecs, retrier, resourceName, chunker);
  ListenableFuture<Void> currUpload = newUpload.start();
  currUpload.addListener(
      () -> {
        if (currUpload.isCancelled()) {
          newUpload.cancel();
        }
      },
      MoreExecutors.directExecutor());
  return currUpload;
}
 
Example #5
Source File: ParserWithConfigurableAttributesTest.java    From buck with Apache License 2.0 6 votes vote down vote up
@Test
public void buildTargetHashCodePopulatesCorrectly() throws Exception {
  tempDir.newFolder("foo");

  Path testFooBuckFile = tempDir.newFile("foo/BUCK");
  Files.write(
      testFooBuckFile, "java_library(name = 'lib', visibility=['PUBLIC'])\n".getBytes(UTF_8));

  BuildTarget fooLibTarget = BuildTargetFactory.newInstance("//foo", "lib");

  // We can't precalculate the hash, since it depends on the buck version. Check for the presence
  // of a hash for the right key.
  HashCode hashCode = buildTargetGraphAndGetHashCodes(parser, fooLibTarget).get(fooLibTarget);

  assertNotNull(hashCode);
}
 
Example #6
Source File: PBKDF2PasswordEncryptor.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public boolean test(String password, String encryptedPassword) {
  requireNonNull(password, "Required non-null password");
  requireNonNull(password, "Required non-null encrypted password");
  final Matcher matcher = PWD_REGEX.matcher(encryptedPassword);
  if (!matcher.matches()) {
    return false;
  }
  // retrieve salt, password hash and iterations count from hash
  final Integer iterations = tryParse(matcher.group("iterations"));
  final String salt = matcher.group("saltHash");
  final String pwdHash = matcher.group("pwdHash");
  // compute password's hash and test whether it matches to given hash
  final HashCode hash =
      computeHash(password.toCharArray(), HashCode.fromString(salt).asBytes(), iterations);
  return hash.toString().equals(pwdHash);
}
 
Example #7
Source File: ManifestTest.java    From buck with Apache License 2.0 6 votes vote down vote up
@Test
public void lookupMatch() throws IOException {
  RuleKey key = new RuleKey("aa");
  SourcePath input = FakeSourcePath.of("input.h");
  HashCode hashCode = HashCode.fromInt(20);
  Manifest manifest =
      ManifestUtil.fromMap(
          new RuleKey("cc"),
          ImmutableMap.of(
              key, ImmutableMap.of(RESOLVER.getRelativePath(input).toString(), hashCode)));
  FileHashLoader fileHashLoader =
      new FakeFileHashCache(ImmutableMap.of(RESOLVER.getAbsolutePath(input), hashCode));
  assertThat(
      manifest.lookup(fileHashLoader, RESOLVER, ImmutableSet.of(input)),
      Matchers.equalTo(Optional.of(key)));
}
 
Example #8
Source File: ManifestTest.java    From buck with Apache License 2.0 6 votes vote down vote up
@Test
public void lookupHashMismatch() throws IOException {
  RuleKey key = new RuleKey("aa");
  SourcePath input = FakeSourcePath.of("input.h");
  Manifest manifest =
      ManifestUtil.fromMap(
          new RuleKey("cc"),
          ImmutableMap.of(
              key,
              ImmutableMap.of(RESOLVER.getRelativePath(input).toString(), HashCode.fromInt(1))));
  FileHashLoader fileHashLoader =
      new FakeFileHashCache(
          ImmutableMap.of(RESOLVER.getAbsolutePath(input), HashCode.fromInt(2)));
  assertThat(
      manifest.lookup(fileHashLoader, RESOLVER, ImmutableSet.of(input)),
      Matchers.equalTo(Optional.empty()));
}
 
Example #9
Source File: HashUtils.java    From MHAP with Apache License 2.0 6 votes vote down vote up
public static long[] computeHashes(String item, int numWords, int seed)
{
	long[] hashes = new long[numWords];

	for (int word = 0; word < numWords; word += 2)
	{
		HashFunction hashFunc = Hashing.murmur3_128(seed + word);
		Hasher hasher = hashFunc.newHasher();
		hasher.putUnencodedChars(item);

		// get the two longs out
		HashCode hc = hasher.hash();
		ByteBuffer bb = ByteBuffer.wrap(hc.asBytes());
		hashes[word] = bb.getLong(0);
		if (word + 1 < numWords)
			hashes[word + 1] = bb.getLong(8);
	}

	return hashes;
}
 
Example #10
Source File: LcUuidContentsScrubber.java    From buck with Apache License 2.0 6 votes vote down vote up
@Override
public void scrubFile(FileChannel file) throws IOException, ScrubException {
  if (!Machos.isMacho(file)) {
    return;
  }

  long size = file.size();
  MappedByteBuffer map = file.map(FileChannel.MapMode.READ_WRITE, 0, size);

  resetUuidIfPresent(map);
  HashCode hashCode = computeHash(map, size);

  map.rewind();
  try {
    Machos.setUuidIfPresent(map, Arrays.copyOf(hashCode.asBytes(), UUID_LENGTH));
  } catch (Machos.MachoException e) {
    throw new ScrubException(e.getMessage());
  }
}
 
Example #11
Source File: DiskCacheTest.java    From metanome-algorithms with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void testStoreAndGetDifferent() {
	CacheableSupplier<Integer> supplier2 = Mockito.mock(CacheableSupplier.class);
	HashCode hash1 = Hashing.sha256().newHasher()
		.putBoolean(true)
		.hash();
	HashCode hash2 = Hashing.sha256().newHasher()
		.putBoolean(false)
		.hash();
	when(supplier.hash()).thenReturn(hash1);
	when(supplier2.hash()).thenReturn(hash2);
	when(supplier.get()).thenReturn(1);
	when(supplier2.get()).thenReturn(2);
	File f = folder.getRoot();
	DiskCache<Integer> cache = new DiskCache<>(supplier, f);
	assertThat(cache.get(true)).isEqualTo(1);
	DiskCache<Integer> cache2 = new DiskCache<>(supplier2, f);
	assertThat(cache2.get(true)).isEqualTo(2);
}
 
Example #12
Source File: PreDex.java    From javaide with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Returns a unique File for the pre-dexed library, even
 * if there are 2 libraries with the same file names (but different
 * paths)
 * <p>
 * If multidex is enabled the return File is actually a folder.
 *
 * @param outFolder the output folder.
 * @param inputFile the library.
 */
@NonNull
static File getDexFileName(@NonNull File outFolder, @NonNull File inputFile) {
    // get the filename
    String name = inputFile.getName();
    // remove the extension
    int pos = name.lastIndexOf('.');
    if (pos != -1) {
        name = name.substring(0, pos);
    }

    // add a hash of the original file path.
    String input = inputFile.getAbsolutePath();
    HashFunction hashFunction = Hashing.sha1();
    HashCode hashCode = hashFunction.hashString(input, Charsets.UTF_16LE);

    return new File(outFolder, name + "-" + hashCode.toString() + SdkConstants.DOT_JAR);
}
 
Example #13
Source File: NullBlobStore.java    From s3proxy with Apache License 2.0 6 votes vote down vote up
@Override
public String putBlob(String containerName, Blob blob,
        PutOptions options) {
    long length;
    try (InputStream is = blob.getPayload().openStream()) {
        length = ByteStreams.copy(is, ByteStreams.nullOutputStream());
    } catch (IOException ioe) {
        throw new RuntimeException(ioe);
    }

    byte[] array = Longs.toByteArray(length);
    ByteSourcePayload payload = new ByteSourcePayload(
            ByteSource.wrap(array));
    payload.setContentMetadata(blob.getPayload().getContentMetadata());
    payload.getContentMetadata().setContentLength((long) array.length);
    payload.getContentMetadata().setContentMD5((HashCode) null);
    blob.setPayload(payload);

    return super.putBlob(containerName, blob, options);
}
 
Example #14
Source File: DownloadStepTest.java    From buck with Apache License 2.0 6 votes vote down vote up
@Test
public void descriptionUsesCanonicalUri() {
  ProjectFilesystem projectFilesystem =
      new FakeProjectFilesystem() {
        @Override
        public Path resolve(Path relativePath) {
          return Paths.get("/abs/path").resolve(relativePath);
        }
      };

  DownloadStep step =
      new DownloadStep(
          projectFilesystem,
          new ConditionallyExplodingDownloader(),
          URI.create("https://example.com/"),
          ImmutableList.of(
              URI.create("https://mirror1.example.com/"),
              URI.create("https://mirror2.example.com/"),
              URI.create("https://mirror3.example.com/")),
          FileHash.ofSha1(HashCode.fromString("a94a8fe5ccb19ba61c4c0873d391e987982fbbd3")),
          outputPath);

  Assert.assertEquals(
      "curl https://example.com/ -o '/abs/path/some/dir/out.txt'", step.getDescription(context));
}
 
Example #15
Source File: CustomZipEntryTest.java    From buck with Apache License 2.0 6 votes vote down vote up
@Test
public void producedZipFilesAreTimezoneAgnostic() throws Exception {
  HashCode referenceHash = writeSimpleJarAndGetHash();
  TimeZone previousDefault = TimeZone.getDefault();
  try {
    String[] availableIDs = TimeZone.getAvailableIDs();
    assertThat(availableIDs.length, Matchers.greaterThan(1));

    for (String timezoneID : availableIDs) {
      TimeZone timeZone = TimeZone.getTimeZone(timezoneID);
      TimeZone.setDefault(timeZone);

      assertThat(writeSimpleJarAndGetHash(), Matchers.equalTo(referenceHash));
    }
  } finally {
    TimeZone.setDefault(previousDefault);
  }
}
 
Example #16
Source File: SQLiteArtifactCache.java    From buck with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
ImmutableList<RuleKey> directoryFileContentHashes() throws SQLException {
  ImmutableList.Builder<RuleKey> keys = ImmutableList.builder();
  try (ResultSet rs =
      db.connection
          .createStatement()
          .executeQuery("SELECT sha1 FROM content WHERE filepath NOTNULL")) {
    while (rs.next()) {
      keys.add(new RuleKey(HashCode.fromBytes(rs.getBytes(1))));
    }
  }
  return keys.build();
}
 
Example #17
Source File: RemoteFileTest.java    From buck with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldNotSaveFileToFinalLocationIfTheDownloadFails() throws Exception {
  String value = "I also like cake";
  HashCode hashCode = Hashing.sha1().hashBytes(value.getBytes(UTF_8));
  Path output = runTheMagic(new ExplodingDownloader(), value, hashCode);

  assertThat(output, not(exists()));
}
 
Example #18
Source File: BlazeEditProjectViewControl.java    From intellij with Apache License 2.0 5 votes vote down vote up
public void update(BlazeNewProjectBuilder builder) {
  this.workspaceData = builder.getWorkspaceData();
  this.projectViewOption = builder.getProjectViewOption();
  WorkspaceRoot workspaceRoot = workspaceData.workspaceRoot();
  WorkspacePath workspacePath = projectViewOption.getSharedProjectView();
  String initialProjectViewText = projectViewOption.getInitialProjectViewText();
  boolean allowAddDefaultValues =
      projectViewOption.allowAddDefaultProjectViewValues()
          && allowAddprojectViewDefaultValues.getValue();
  WorkspacePathResolver workspacePathResolver = workspaceData.workspacePathResolver();

  HashCode hashCode =
      Hashing.md5()
          .newHasher()
          .putUnencodedChars(workspaceRoot.toString())
          .putUnencodedChars(workspacePath != null ? workspacePath.toString() : "")
          .putUnencodedChars(initialProjectViewText != null ? initialProjectViewText : "")
          .putBoolean(allowAddDefaultValues)
          .hash();

  // If any params have changed, reinit the control
  if (!hashCode.equals(paramsHash)) {
    this.paramsHash = hashCode;
    this.isInitialising = true;
    init(
        workspaceData.buildSystem(),
        workspacePathResolver,
        workspacePath,
        initialProjectViewText,
        allowAddDefaultValues);
    this.isInitialising = false;
  }
}
 
Example #19
Source File: Files.java    From styx with Apache License 2.0 5 votes vote down vote up
public static HashCode fileContentMd5(Path path) {
    try (InputStream stream = new FileInputStream(path.toFile())) {
       return md5().hashBytes(toByteArray(stream));
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example #20
Source File: DefaultFileHashCacheTest.java    From buck with Apache License 2.0 5 votes vote down vote up
@Test
public void whenPathIsPutCacheContainsPath() throws IOException {
  ProjectFilesystem filesystem = TestProjectFilesystems.createProjectFilesystem(tmp.getRoot());
  Path path = Paths.get("SomeClass.java");
  filesystem.touch(path);

  DefaultFileHashCache cache =
      DefaultFileHashCache.createDefaultFileHashCache(filesystem, fileHashCacheMode);
  HashCodeAndFileType value = HashCodeAndFileType.ofFile(HashCode.fromInt(42));
  cache.fileHashCacheEngine.put(path, value);
  assertTrue("Cache should contain path", cache.getIfPresent(path).isPresent());
}
 
Example #21
Source File: FakeFileHashCache.java    From buck with Apache License 2.0 5 votes vote down vote up
public FakeFileHashCache(
    Map<Path, HashCode> pathsToHashes,
    Map<ArchiveMemberPath, HashCode> archiveMemberPathsToHashes,
    Map<Path, Long> pathsToSizes) {
  this.pathsToHashes = pathsToHashes;
  this.archiveMemberPathsToHashes = archiveMemberPathsToHashes;
  this.pathsToSizes = pathsToSizes;
  this.usePathsForArchives = false;
}
 
Example #22
Source File: ClusterManager.java    From rubix with Apache License 2.0 5 votes vote down vote up
public int getNodeIndex(int numNodes, String key)
{
  HashFunction hf = Hashing.md5();
  HashCode hc = hf.hashString(key, Charsets.UTF_8);
  int initialNodeIndex = Hashing.consistentHash(hc, numNodes);
  int finalNodeIndex = initialNodeIndex;
  if (hc.asInt() % 2 == 0) {
    finalNodeIndex = getNextRunningNodeIndex(initialNodeIndex);
  }
  else {
    finalNodeIndex = getPreviousRunningNodeIndex(initialNodeIndex);
  }

  return finalNodeIndex;
}
 
Example #23
Source File: Manifest.java    From buck with Apache License 2.0 5 votes vote down vote up
/** Serializes the manifest to the given {@link OutputStream}. */
public void serialize(OutputStream rawOutput) throws IOException {
  DataOutputStream output = new DataOutputStream(rawOutput);

  output.writeInt(VERSION);

  output.writeUTF(key.toString());

  output.writeInt(inputs.size());
  for (String input : inputs) {
    output.writeUTF(input);
  }

  output.writeInt(hashes.size());
  for (Pair<Integer, HashCode> hash : hashes) {
    output.writeInt(hash.getFirst());
    output.writeUTF(hash.getSecond().toString());
  }

  output.writeInt(entries.size());
  for (Pair<RuleKey, int[]> entry : entries) {
    output.writeInt(entry.getSecond().length);
    for (int hashIndex : entry.getSecond()) {
      output.writeInt(hashIndex);
    }
    output.writeUTF(entry.getFirst().toString());
  }
}
 
Example #24
Source File: HttpFileBinary.java    From buck with Apache License 2.0 5 votes vote down vote up
public HttpFileBinary(
    BuildTarget buildTarget,
    ProjectFilesystem projectFilesystem,
    BuildRuleParams params,
    Downloader downloader,
    ImmutableList<URI> uris,
    HashCode sha256,
    String out) {
  super(buildTarget, projectFilesystem, params, downloader, uris, sha256, out, true);
}
 
Example #25
Source File: Hashes.java    From nexus-public with 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 #26
Source File: HashAspectFactory.java    From packagedrone with Eclipse Public License 1.0 5 votes vote down vote up
private static void makeChecksumMetadata ( final Path file, final Map<String, String> metadata ) throws IOException
{
    final Map<String, HashCode> result = HashHelper.createChecksums ( file, functions );
    for ( final Map.Entry<String, HashCode> entry : result.entrySet () )
    {
        metadata.put ( entry.getKey (), entry.getValue ().toString () );
    }
}
 
Example #27
Source File: ByteStreamBuildEventArtifactUploaderTest.java    From bazel with Apache License 2.0 5 votes vote down vote up
@Test
public void remoteFileShouldNotBeUploaded_findMissingDigests() throws Exception {
  // Test that findMissingDigests is called to check which files exist remotely
  // and that those are not uploaded.

  // arrange
  Path remoteFile = fs.getPath("/remote-file");
  FileSystemUtils.writeContent(remoteFile, StandardCharsets.UTF_8, "hello world");
  Digest remoteDigest = DIGEST_UTIL.compute(remoteFile);
  Path localFile = fs.getPath("/local-file");
  FileSystemUtils.writeContent(localFile, StandardCharsets.UTF_8, "foo bar");
  Digest localDigest = DIGEST_UTIL.compute(localFile);

  StaticMissingDigestsFinder digestQuerier =
      Mockito.spy(new StaticMissingDigestsFinder(ImmutableSet.of(remoteDigest)));
  ByteStreamUploader uploader = Mockito.mock(ByteStreamUploader.class);
  when(uploader.uploadBlobAsync(any(), any(), anyBoolean()))
      .thenReturn(Futures.immediateFuture(null));
  ByteStreamBuildEventArtifactUploader artifactUploader =
      newArtifactUploader(uploader, digestQuerier);

  // act
  Map<Path, LocalFile> files =
      ImmutableMap.of(
          remoteFile,
          new LocalFile(remoteFile, LocalFileType.OUTPUT),
          localFile,
          new LocalFile(localFile, LocalFileType.OUTPUT));
  PathConverter pathConverter = artifactUploader.upload(files).get();

  // assert
  verify(digestQuerier).findMissingDigests(any());
  verify(uploader)
      .uploadBlobAsync(eq(HashCode.fromString(localDigest.getHash())), any(), anyBoolean());
  assertThat(pathConverter.apply(remoteFile)).contains(remoteDigest.getHash());
  assertThat(pathConverter.apply(localFile)).contains(localDigest.getHash());
}
 
Example #28
Source File: TempBlob.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
public TempBlob(final Blob blob,
                final Map<HashAlgorithm, HashCode> hashes,
                final boolean hashesVerified,
                final BlobStore blobStore)
{
  this.blob = checkNotNull(blob);
  this.hashes = checkNotNull(hashes);
  this.hashesVerified = hashesVerified;
  this.blobStore = checkNotNull(blobStore);
}
 
Example #29
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 #30
Source File: PBKDF2PasswordEncryptor.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public String encrypt(String password) {
  requireNonNull(password, "Required non-null password");
  final byte[] salt = new byte[SALT_LENGTH];
  SECURE_RANDOM.nextBytes(salt);
  final HashCode hash = computeHash(password.toCharArray(), salt, ITERATIONS_COUNT);
  final HashCode saltHash = HashCode.fromBytes(salt);
  return format(PWD_FMT, hash, saltHash, ITERATIONS_COUNT);
}