Search in sources :

Example 66 with Hasher

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

the class DexProducedFromJavaLibrary method computeAbiKey.

@VisibleForTesting
static Sha1HashCode computeAbiKey(ImmutableSortedMap<String, HashCode> classNames) {
    Hasher hasher = Hashing.sha1().newHasher();
    for (Map.Entry<String, HashCode> entry : classNames.entrySet()) {
        hasher.putUnencodedChars(entry.getKey());
        hasher.putByte((byte) 0);
        hasher.putUnencodedChars(entry.getValue().toString());
        hasher.putByte((byte) 0);
    }
    return Sha1HashCode.fromHashCode(hasher.hash());
}
Also used : Hasher(com.google.common.hash.Hasher) HashCode(com.google.common.hash.HashCode) Sha1HashCode(com.facebook.buck.util.sha1.Sha1HashCode) Map(java.util.Map) ImmutableSortedMap(com.google.common.collect.ImmutableSortedMap) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 67 with Hasher

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

the class DefaultParserTargetGroupFactory method createTargetNode.

@Override
public TargetGroup createTargetNode(Cell cell, Path buildFile, BuildTarget target, Map<String, Object> rawNode, Function<PerfEventId, SimplePerfEvent.Scope> perfEventScope) {
    Preconditions.checkArgument(!target.isFlavored());
    UnflavoredBuildTarget unflavoredBuildTarget = target.withoutCell().getUnflavoredBuildTarget();
    UnflavoredBuildTarget unflavoredBuildTargetFromRawData = RawNodeParsePipeline.parseBuildTargetFromRawRule(cell.getRoot(), rawNode, buildFile);
    if (!unflavoredBuildTarget.equals(unflavoredBuildTargetFromRawData)) {
        throw new IllegalStateException(String.format("Inconsistent internal state, target from data: %s, expected: %s, raw data: %s", unflavoredBuildTargetFromRawData, unflavoredBuildTarget, Joiner.on(',').withKeyValueSeparator("->").join(rawNode)));
    }
    BuildRuleType buildRuleType = parseBuildRuleTypeFromRawRule(cell, rawNode);
    // Because of the way that the parser works, we know this can never return null.
    Description<?> description = cell.getDescription(buildRuleType);
    Cell targetCell = cell.getCell(target);
    TargetGroupDescription.Arg constructorArg = (TargetGroupDescription.Arg) description.createUnpopulatedConstructorArg();
    try {
        ImmutableSet.Builder<BuildTarget> declaredDeps = ImmutableSet.builder();
        ImmutableSet.Builder<VisibilityPattern> visibilityPatterns = ImmutableSet.builder();
        try (SimplePerfEvent.Scope scope = perfEventScope.apply(PerfEventId.of("MarshalledConstructorArg"))) {
            marshaller.populate(targetCell.getCellPathResolver(), targetCell.getFilesystem(), target, constructorArg, declaredDeps, visibilityPatterns, rawNode);
        }
        try (SimplePerfEvent.Scope scope = perfEventScope.apply(PerfEventId.of("CreatedTargetNode"))) {
            Hasher hasher = Hashing.sha1().newHasher();
            hasher.putString(BuckVersion.getVersion(), UTF_8);
            JsonObjectHashing.hashJsonObject(hasher, rawNode);
            TargetGroup node = new TargetGroup(constructorArg.targets, constructorArg.restrictOutboundVisibility, target);
            return node;
        }
    } catch (ParamInfoException e) {
        throw new HumanReadableException("%s: %s", target, e.getMessage());
    }
}
Also used : TargetGroupDescription(com.facebook.buck.groups.TargetGroupDescription) UnflavoredBuildTarget(com.facebook.buck.model.UnflavoredBuildTarget) VisibilityPattern(com.facebook.buck.rules.VisibilityPattern) ParamInfoException(com.facebook.buck.rules.ParamInfoException) Hasher(com.google.common.hash.Hasher) ImmutableSet(com.google.common.collect.ImmutableSet) UnflavoredBuildTarget(com.facebook.buck.model.UnflavoredBuildTarget) BuildTarget(com.facebook.buck.model.BuildTarget) HumanReadableException(com.facebook.buck.util.HumanReadableException) TargetGroup(com.facebook.buck.rules.TargetGroup) BuildRuleType(com.facebook.buck.rules.BuildRuleType) SimplePerfEvent(com.facebook.buck.event.SimplePerfEvent) Cell(com.facebook.buck.rules.Cell)

Example 68 with Hasher

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

the class ActionGraphCache method getTargetGraphHash.

private static HashCode getTargetGraphHash(TargetGraph targetGraph) {
    Hasher hasher = Hashing.sha1().newHasher();
    ImmutableSet<TargetNode<?, ?>> nodes = targetGraph.getNodes();
    for (TargetNode<?, ?> targetNode : ImmutableSortedSet.copyOf(nodes)) {
        hasher.putBytes(targetNode.getRawInputsHashCode().asBytes());
    }
    return hasher.hash();
}
Also used : Hasher(com.google.common.hash.Hasher)

Example 69 with Hasher

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

the class Sha1HashCodeTest method testUpdate.

@Test
public void testUpdate() {
    Hasher hasher1 = Hashing.sha1().newHasher();
    Sha1HashCode sha1 = Sha1HashCode.of("a002b39af204cdfaa5fdb67816b13867c32ac52c");
    Hasher hasher2 = sha1.update(hasher1);
    assertSame(hasher1, hasher2);
    HashCode expectedHash = Hashing.sha1().newHasher().putBytes(new byte[] { (byte) 0xa0, (byte) 0x02, (byte) 0xb3, (byte) 0x9a, (byte) 0xf2, (byte) 0x04, (byte) 0xcd, (byte) 0xfa, (byte) 0xa5, (byte) 0xfd, (byte) 0xb6, (byte) 0x78, (byte) 0x16, (byte) 0xb1, (byte) 0x38, (byte) 0x67, (byte) 0xc3, (byte) 0x2a, (byte) 0xc5, (byte) 0x2c }).hash();
    HashCode observedHash = hasher1.hash();
    assertEquals(expectedHash, observedHash);
}
Also used : Hasher(com.google.common.hash.Hasher) HashCode(com.google.common.hash.HashCode) Test(org.junit.Test)

Example 70 with Hasher

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

the class HashInputJarsToDexStep method execute.

@Override
public StepExecutionResult execute(final ExecutionContext context) {
    ImmutableList.Builder<Path> allInputs = ImmutableList.builder();
    allInputs.addAll(primaryInputsToDex.get());
    if (secondaryOutputToInputs.isPresent()) {
        allInputs.addAll(secondaryOutputToInputs.get().get().values());
    }
    final Map<String, HashCode> classNamesToHashes = classNamesToHashesSupplier.get();
    for (Path path : allInputs.build()) {
        try {
            final Hasher hasher = Hashing.sha1().newHasher();
            new DefaultClasspathTraverser().traverse(new ClasspathTraversal(Collections.singleton(path), filesystem) {

                @Override
                public void visit(FileLike fileLike) throws IOException {
                    String className = fileLike.getRelativePath().replaceAll("\\.class$", "");
                    if (classNamesToHashes.containsKey(className)) {
                        HashCode classHash = Preconditions.checkNotNull(classNamesToHashes.get(className));
                        hasher.putBytes(classHash.asBytes());
                    }
                }
            });
            dexInputsToHashes.put(path, Sha1HashCode.fromHashCode(hasher.hash()));
        } catch (IOException e) {
            context.logError(e, "Error hashing smart dex input: %s", path);
            return StepExecutionResult.ERROR;
        }
    }
    stepFinished = true;
    return StepExecutionResult.SUCCESS;
}
Also used : Path(java.nio.file.Path) ClasspathTraversal(com.facebook.buck.jvm.java.classes.ClasspathTraversal) Hasher(com.google.common.hash.Hasher) HashCode(com.google.common.hash.HashCode) Sha1HashCode(com.facebook.buck.util.sha1.Sha1HashCode) ImmutableList(com.google.common.collect.ImmutableList) DefaultClasspathTraverser(com.facebook.buck.jvm.java.classes.DefaultClasspathTraverser) IOException(java.io.IOException) FileLike(com.facebook.buck.jvm.java.classes.FileLike)

Aggregations

Hasher (com.google.common.hash.Hasher)106 HashCode (com.google.common.hash.HashCode)14 IOException (java.io.IOException)12 Test (org.junit.Test)10 HashFunction (com.google.common.hash.HashFunction)6 InputStream (java.io.InputStream)6 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 Hcr (org.apache.servicecomb.demo.edge.authentication.encrypt.Hcr)4 EncryptContext (org.apache.servicecomb.demo.edge.service.encrypt.EncryptContext)4 Hcr (org.apache.servicecomb.it.authentication.encrypt.Hcr)4 EncryptContext (org.apache.servicecomb.it.edge.encrypt.EncryptContext)4 Buffer (io.vertx.core.buffer.Buffer)3 OutputStream (java.io.OutputStream)3 Path (java.nio.file.Path)3 StringUtils.byteToHexString (org.apache.flink.util.StringUtils.byteToHexString)3 EObject (org.eclipse.emf.ecore.EObject)3