Java Code Examples for com.google.common.hash.Hashing#sha256()

The following examples show how to use com.google.common.hash.Hashing#sha256() . 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: HashFieldTransformator.java    From SkaETL with Apache License 2.0 6 votes vote down vote up
public void apply(String idProcess, ParameterTransformation parameterTransformation, ObjectNode jsonValue) {
    JsonNode valueField = at(parameterTransformation.getProcessHashData().getField(), jsonValue);
    if(valueField != null &&
            StringUtils.isNotBlank(valueField.asText())){
        switch (parameterTransformation.getProcessHashData().getTypeHash()){
            case SHA256:
                HashFunction m_hash256 = Hashing.sha256();
                String valueHash256 = m_hash256.hashBytes(valueField.asText().getBytes()).toString();
                put(jsonValue, parameterTransformation.getProcessHashData().getField(), valueHash256);
                break;
            case MURMUR3:
                HashFunction m_hashMurmur3 = Hashing.murmur3_128();
                String valueHashMurmur3 = m_hashMurmur3.hashBytes(valueField.asText().getBytes()).toString();
                put(jsonValue, parameterTransformation.getProcessHashData().getField(), valueHashMurmur3);
                break;
            default:
                log.error("Type Hash not manage {}",parameterTransformation.getProcessHashData().getTypeHash());
                break;

        }
    }
}
 
Example 2
Source File: SchemaServiceTest.java    From pulsar with Apache License 2.0 6 votes vote down vote up
@Test
public void trimDeletedSchemaAndGetListTest() throws Exception {
    List<SchemaAndMetadata> list = new ArrayList<>();
    CompletableFuture<SchemaVersion> put = schemaRegistryService.putSchemaIfAbsent(
            schemaId1, schema1, SchemaCompatibilityStrategy.FULL);
    SchemaVersion newVersion = put.get();
    list.add(new SchemaAndMetadata(schemaId1, schema1, newVersion));
    put = schemaRegistryService.putSchemaIfAbsent(
            schemaId1, schema2, SchemaCompatibilityStrategy.FULL);
    newVersion = put.get();
    list.add(new SchemaAndMetadata(schemaId1, schema2, newVersion));
    List<SchemaAndMetadata> list1 = schemaRegistryService.trimDeletedSchemaAndGetList(schemaId1).get();
    assertEquals(list.size(), list1.size());
    HashFunction hashFunction = Hashing.sha256();
    for (int i = 0; i < list.size(); i++) {
        SchemaAndMetadata schemaAndMetadata1 = list.get(i);
        SchemaAndMetadata schemaAndMetadata2 = list1.get(i);
        assertEquals(hashFunction.hashBytes(schemaAndMetadata1.schema.getData()).asBytes(),
                hashFunction.hashBytes(schemaAndMetadata2.schema.getData()).asBytes());
        assertEquals(((LongSchemaVersion)schemaAndMetadata1.version).getVersion()
                , ((LongSchemaVersion)schemaAndMetadata2.version).getVersion());
        assertEquals(schemaAndMetadata1.id, schemaAndMetadata2.id);
    }
}
 
Example 3
Source File: HashingUtil.java    From datacollector with Apache License 2.0 6 votes vote down vote up
public static HashFunction getHasher(HashType hashType) {
  switch(hashType) {
    case MURMUR3_128:
      return Hashing.murmur3_128();
    case MURMUR3_32:
      return Hashing.murmur3_32();
    case SIPHASH24:
      return Hashing.sipHash24();
    case MD5:
      return Hashing.md5();
    case SHA1:
      return Hashing.sha1();
    case SHA256:
      return Hashing.sha256();
    case SHA512:
      return Hashing.sha512();
    case ADLER32:
      return Hashing.adler32();
    case CRC32:
      return Hashing.crc32();
    case CRC32C:
      return Hashing.crc32c();
    default:
      throw new IllegalArgumentException(Utils.format("Unsupported Hashing Algorithm: {}", hashType.name()));
  }
}
 
Example 4
Source File: Hasher.java    From datafu with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the HashFunction named by algorithm
 *
 * See the Hasher class docs for a list of algorithms and guidance on selection.
 *
 * @param algorithm the hash algorithm to use
 * @throws IllegalArgumentException for an invalid seed given the algorithm
 * @throws RuntimeException when the seed cannot be parsed
 */
private void makeHashFunc(String algorithm) throws IllegalArgumentException, RuntimeException
{
  if (hash_func != null) { throw new RuntimeException("The hash function should only be set once per instance"); }

  if (algorithm.startsWith("good-")) {
    int bits = Integer.parseInt(algorithm.substring(5));
    hash_func = Hashing.goodFastHash(bits);
  }
  else if (algorithm.equals("murmur3-32")) { hash_func = Hashing.murmur3_32();  }
  else if (algorithm.equals("murmur3-128")){ hash_func = Hashing.murmur3_128(); }
  else if (algorithm.equals("sip24"))      { hash_func = Hashing.sipHash24();   }
  else if (algorithm.equals("sha1"))       { hash_func = Hashing.sha1();        }
  else if (algorithm.equals("sha256"))     { hash_func = Hashing.sha256();      }
  else if (algorithm.equals("sha512"))     { hash_func = Hashing.sha512();      }
  else if (algorithm.equals("md5"))        { hash_func = Hashing.md5();         }
  else if (algorithm.equals("adler32"))    { hash_func = Hashing.adler32();     }
  else if (algorithm.equals("crc32"))      { hash_func = Hashing.crc32();       }
  else { throw new IllegalArgumentException("No hash function found for algorithm "+algorithm+". Allowed values include "+HASH_NAMES); }
}
 
Example 5
Source File: Launcher.java    From launcher with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private static byte[] download(String path, String hash, IntConsumer progress) throws IOException, VerificationException
{
	HashFunction hashFunction = Hashing.sha256();
	Hasher hasher = hashFunction.newHasher();
	ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

	URL url = new URL(path);
	HttpURLConnection conn = (HttpURLConnection) url.openConnection();
	conn.setRequestProperty("User-Agent", USER_AGENT);
	conn.getResponseCode();

	InputStream err = conn.getErrorStream();
	if (err != null)
	{
		err.close();
		throw new IOException("Unable to download " + path + " - " + conn.getResponseMessage());
	}

	int downloaded = 0;
	try (InputStream in = conn.getInputStream())
	{
		int i;
		byte[] buffer = new byte[1024 * 1024];
		while ((i = in.read(buffer)) != -1)
		{
			byteArrayOutputStream.write(buffer, 0, i);
			hasher.putBytes(buffer, 0, i);
			downloaded += i;
			progress.accept(downloaded);
		}
	}

	HashCode hashCode = hasher.hash();
	if (!hash.equals(hashCode.toString()))
	{
		throw new VerificationException("Unable to verify resource " + path + " - expected " + hash + " got " + hashCode.toString());
	}

	return byteArrayOutputStream.toByteArray();
}
 
Example 6
Source File: TableAuthIdentityManagerDAOTest.java    From emodb with Apache License 2.0 5 votes vote down vote up
/**
 * There are two tables which store identities in TableAuthIdentityManagerDAO: One table keyed by a hash of the
 * API key, and an index table ID'd by the identity ID which contains the API key hash.  This second table is used
 * to look up API keys by ID.  It should be rare, but it is possible for an API key record tNAo exist
 * without a corresponding ID.  One possible way for this to happen is grandfathered in API keys
 * created before the introduction of IDs.  TableAuthIdentityManagerDAO should rebuild the index
 * when there is a missing or incorrect index record.  This test verifies that works as expected.
 */
@Test
public void testRebuildIdIndex() {
    DataStore dataStore = new InMemoryDataStore(new MetricRegistry());
    Supplier<String> idSupplier = () -> "id0";
    TableAuthIdentityManagerDAO<ApiKey> tableAuthIdentityManagerDAO = new TableAuthIdentityManagerDAO<>(
            ApiKey.class, dataStore, "__auth:keys", "__auth:internal_ids", "app_global:sys",
            idSupplier, Hashing.sha256());

    tableAuthIdentityManagerDAO.createIdentity("testkey",
            new ApiKeyModification()
                    .withOwner("testowner")
                    .addRoles("role1", "role2"));

    // Verify both tables have been written

    String keyTableId = Hashing.sha256().hashUnencodedChars("testkey").toString();

    Map<String, Object> keyMap = dataStore.get("__auth:keys", keyTableId);
    assertFalse(Intrinsic.isDeleted(keyMap));
    assertEquals(keyMap.get("owner"), "testowner");

    Map<String, Object> indexMap = dataStore.get("__auth:internal_ids", "id0");
    assertFalse(Intrinsic.isDeleted(indexMap));
    assertEquals(indexMap.get("hashedId"), keyTableId);

    // Deliberately delete the index map record
    dataStore.update("__auth:internal_ids", "id0", TimeUUIDs.newUUID(), Deltas.delete(),
            new AuditBuilder().setComment("test delete").build());

    Set<String> roles = tableAuthIdentityManagerDAO.getIdentity("id0").getRoles();
    assertEquals(roles, ImmutableSet.of("role1", "role2"));

    // Verify that the index record is re-created
    indexMap = dataStore.get("__auth:internal_ids", "id0");
    assertFalse(Intrinsic.isDeleted(indexMap));
    assertEquals(indexMap.get("hashedId"), keyTableId);
}
 
Example 7
Source File: AcheToCdrExporter.java    From ache with Apache License 2.0 5 votes vote down vote up
private String uploadMediaFile(byte[] content, String url) throws IOException {
    HashFunction hf = Hashing.sha256();
    Hasher hasher = hf.newHasher();
    hasher.putBytes(content);
    String host = new URL(url).getHost();
    String hs = reverseDomain(host) + "/" + hasher.hash().toString();
    if (skipUpload == false) {
        this.s3Uploader.upload(hs, content);
        System.out.println("Uploaded object: " + hs);
    } else {
        System.out.println("Created object: " + hs);
    }
    return hs;
}
 
Example 8
Source File: JPAUser.java    From james-project with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
private static HashFunction chooseHashing(String algorithm) {
    switch (algorithm) {
        case "MD5":
            return Hashing.md5();
        case "SHA-256":
            return Hashing.sha256();
        case "SHA-512":
            return Hashing.sha512();
        default:
            return Hashing.sha1();
    }
}
 
Example 9
Source File: CassandraBlobStore.java    From james-project with 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 10
Source File: FileHash.java    From buck with Apache License 2.0 5 votes vote down vote up
/** Get the hash function for this type of hash */
public HashFunction getHashFunction() {
  if (sha1OrSha256.isLeft()) {
    return Hashing.sha1();
  } else {
    return Hashing.sha256();
  }
}
 
Example 11
Source File: DesugarTask.java    From javafxmobile-plugin with BSD 3-Clause "New" or "Revised" License 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 12
Source File: AtlasDesugarTransform.java    From atlas with 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 13
Source File: Launcher.java    From launcher with BSD 2-Clause "Simplified" License 4 votes vote down vote up
private static String hash(File file) throws IOException
{
	HashFunction sha256 = Hashing.sha256();
	return Files.asByteSource(file).hash(sha256).toString();
}
 
Example 14
Source File: KeyHasher.java    From EVCache with Apache License 2.0 4 votes vote down vote up
public static String getHashedKey(String key, String hashingAlgorithm) {
    final long start = System.nanoTime();
    HashFunction hf = null; 
    switch(hashingAlgorithm.toLowerCase()) {
        case "murmur3" :
            hf = Hashing.murmur3_128();
            break;

        case "adler32" :
            hf = Hashing.adler32();
            break;

        case "crc32" :
            hf = Hashing.crc32();
            break;

        case "sha1" :
            hf = Hashing.sha1();
            break;

        case "sha256" :
            hf = Hashing.sha256();
            break;

        case "siphash24" :
            hf = Hashing.sipHash24();
            break;

        case "goodfasthash" :
            hf = Hashing.goodFastHash(128);
            break;

        case "md5" :
        default :
            hf = Hashing.md5();
            break;
    }

    final HashCode hc = hf.newHasher().putString(key, Charsets.UTF_8).hash();
    final byte[] digest = hc.asBytes();
    if(log.isDebugEnabled()) log.debug("Key : " + key +"; digest length : " + digest.length + "; byte Array contents : " + Arrays.toString(digest) );
    final String hKey = encoder.encodeToString(digest);
    if(log.isDebugEnabled()) log.debug("Key : " + key +"; Hashed & encoded key : " + hKey + "; Took " + (System.nanoTime() - start) + " nanos");
    return hKey;
}