Search in sources :

Example 16 with Hasher

use of com.google.common.hash.Hasher in project buck by facebook.

the class JsonObjectHashingTest method mapOrderDoesNotChangeHash.

@Test
public void mapOrderDoesNotChangeHash() {
    Map<?, ?> map1 = ImmutableMap.of("firstKey", 24, "secondKey", new Float(13.5));
    Map<?, ?> map2 = ImmutableMap.of("secondKey", new Float(13.5), "firstKey", 24);
    Hasher hasher1 = Hashing.sha1().newHasher();
    Hasher hasher2 = Hashing.sha1().newHasher();
    JsonObjectHashing.hashJsonObject(hasher1, map1);
    JsonObjectHashing.hashJsonObject(hasher2, map2);
    assertEquals(hasher1.hash().toString(), hasher2.hash().toString());
}
Also used : Hasher(com.google.common.hash.Hasher) Test(org.junit.Test)

Example 17 with Hasher

use of com.google.common.hash.Hasher in project aerosolve by airbnb.

the class Util method getHashCode.

public static HashCode getHashCode(String family, String value) {
    Hasher hasher = Hashing.murmur3_128().newHasher();
    hasher.putBytes(family.getBytes());
    hasher.putBytes(value.getBytes());
    return hasher.hash();
}
Also used : Hasher(com.google.common.hash.Hasher)

Example 18 with Hasher

use of com.google.common.hash.Hasher in project presto by prestodb.

the class TestTimeZoneKey method testZoneKeyData.

@Test
public void testZoneKeyData() throws Exception {
    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 is more difficult
    assertEquals(hasher.hash().asLong(), 5498515770239515435L, "zone-index.properties file contents changed!");
}
Also used : Hasher(com.google.common.hash.Hasher) Test(org.testng.annotations.Test)

Example 19 with Hasher

use of com.google.common.hash.Hasher in project torodb by torodb.

the class ObjectIdFactory method createMachineId.

private static int createMachineId() {
    int machineId;
    try {
        Hasher hasher = Hashing.crc32c().newHasher();
        Enumeration<NetworkInterface> nics = NetworkInterface.getNetworkInterfaces();
        boolean atLeastOne = false;
        while (nics.hasMoreElements()) {
            NetworkInterface ni = nics.nextElement();
            if (ni != null) {
                byte[] macAddress = ni.getHardwareAddress();
                if (macAddress != null) {
                    for (byte b : macAddress) {
                        atLeastOne = true;
                        hasher.putByte(b);
                    }
                }
            }
        }
        if (!atLeastOne) {
            LOGGER.warn("Failed to calculate the machine id. A random number is used");
            machineId = new SecureRandom().nextInt();
        } else {
            machineId = hasher.hash().asInt();
        }
    } catch (SocketException ex) {
        LOGGER.warn("Failed to calculate the machine id. A random number is used");
        machineId = new SecureRandom().nextInt();
    }
    return machineId & 0xFFFFFF;
}
Also used : SocketException(java.net.SocketException) Hasher(com.google.common.hash.Hasher) NetworkInterface(java.net.NetworkInterface) SecureRandom(java.security.SecureRandom)

Example 20 with Hasher

use of com.google.common.hash.Hasher in project gradle by gradle.

the class DefaultHashingClassLoaderFactory method calculateFilterSpecHash.

private static HashCode calculateFilterSpecHash(FilteringClassLoader.Spec spec) {
    Hasher hasher = Hashing.md5().newHasher();
    addToHash(hasher, spec.getClassNames());
    addToHash(hasher, spec.getPackageNames());
    addToHash(hasher, spec.getPackagePrefixes());
    addToHash(hasher, spec.getResourcePrefixes());
    addToHash(hasher, spec.getResourceNames());
    addToHash(hasher, spec.getDisallowedClassNames());
    addToHash(hasher, spec.getDisallowedPackagePrefixes());
    return hasher.hash();
}
Also used : Hasher(com.google.common.hash.Hasher)

Aggregations

Hasher (com.google.common.hash.Hasher)59 IOException (java.io.IOException)10 Test (org.junit.Test)9 HashCode (com.google.common.hash.HashCode)8 InputStream (java.io.InputStream)5 Map (java.util.Map)5 BuildTarget (com.facebook.buck.model.BuildTarget)4 FakeProjectFilesystem (com.facebook.buck.testutil.FakeProjectFilesystem)4 SettableFakeClock (com.facebook.buck.timing.SettableFakeClock)4 ImmutableMap (com.google.common.collect.ImmutableMap)4 VisibleForTesting (com.google.common.annotations.VisibleForTesting)3 HashFunction (com.google.common.hash.HashFunction)3 OutputStream (java.io.OutputStream)3 Path (java.nio.file.Path)3 SimplePerfEvent (com.facebook.buck.event.SimplePerfEvent)2 ArchiveMemberPath (com.facebook.buck.io.ArchiveMemberPath)2 UnflavoredBuildTarget (com.facebook.buck.model.UnflavoredBuildTarget)2 BuildRuleType (com.facebook.buck.rules.BuildRuleType)2 Cell (com.facebook.buck.rules.Cell)2 ParamInfoException (com.facebook.buck.rules.ParamInfoException)2