Example usage for com.google.common.hash Hashing sha1

List of usage examples for com.google.common.hash Hashing sha1

Introduction

In this page you can find the example usage for com.google.common.hash Hashing sha1.

Prototype

public static HashFunction sha1() 

Source Link

Document

Returns a hash function implementing the SHA-1 algorithm (160 hash bits) by delegating to the SHA-1 MessageDigest .

Usage

From source file:com.facebook.buck.rules.macros.OutputToFileExpander.java

/**
 * @return The absolute path to the temp file.
 *//*  ww w.j a va 2 s.  c  o m*/
private Path createTempFile(ProjectFilesystem filesystem, BuildTarget target, String input) throws IOException {
    Path directory = BuildTargets.getScratchPath(filesystem, target, "%s/tmp");
    filesystem.mkdirs(directory);

    // "prefix" should give a stable name, so that the same delegate with the same input can output
    // the same file. We won't optimise for this case, since it's actually unlikely to happen within
    // a single run, but using a random name would cause 'buck-out' to expand in an uncontrolled
    // manner.
    String prefix = Hashing.sha1().newHasher().putString(delegate.getClass().getName(), UTF_8)
            .putString(input, UTF_8).hash().toString();

    return filesystem.createTempFile(directory, prefix, ".macro");
}

From source file:com.facebook.buck.rules.modern.builders.IsolatedExecutionStrategy.java

IsolatedExecutionStrategy(IsolatedExecution executionStrategy, SourcePathRuleFinder ruleFinder,
        CellPathResolver cellResolver, Cell rootCell,
        ThrowingFunction<Path, HashCode, IOException> fileHasher) {
    this.executionStrategy = executionStrategy;
    this.cellResolver = cellResolver;
    this.rootCell = rootCell;
    this.fileHasher = fileHasher;
    this.nodeMap = new ConcurrentHashMap<>();

    Delegate delegate = (instance, data, children) -> {
        HashCode hash = Hashing.sha1().hashBytes(data);
        Node node = new Node(data, children.stream().collect(
                ImmutableSortedMap.toImmutableSortedMap(Ordering.natural(), HashCode::toString, nodeMap::get)));
        nodeMap.put(hash, node);//  w  w  w  . j  a  v  a  2 s  .c om
        return hash;
    };
    this.serializer = new Serializer(ruleFinder, cellResolver, delegate);

    this.cellNames = rootCell.getCellProvider().getLoadedCells().values().stream().map(Cell::getCanonicalName)
            .collect(ImmutableSet.toImmutableSet());

    this.cellToConfig = cellNames.stream().collect(ImmutableMap.toImmutableMap(v -> v, name -> serializeConfig(
            rootCell.getCellProvider().getCellByPath(cellResolver.getCellPath(name).get()).getBuckConfig())));

    HashFunction hasher = Hashing.sha1();
    this.configHashes = cellToConfig.entrySet().stream().collect(ImmutableMap
            .toImmutableMap(entry -> entry.getKey(), entry -> hasher.hashBytes(entry.getValue()).toString()));

    this.cellPathPrefix = MorePaths.splitOnCommonPrefix(cellNames.stream()
            .map(name -> cellResolver.getCellPath(name).get()).collect(ImmutableList.toImmutableList())).get()
            .getFirst();
}

From source file:com.facebook.buck.util.cache.DefaultFileHashCache.java

private HashCodeAndFileType getDirHashCode(Path path) throws IOException {
    Hasher hasher = Hashing.sha1().newHasher();
    ImmutableSet<Path> children = PathHashing.hashPath(hasher, this, projectFilesystem, path);
    return HashCodeAndFileType.ofDirectory(hasher.hash(), children);
}

From source file:com.facebook.buck.parser.DefaultParserTargetGroupFactory.java

@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)));
    }//from w  ww  . j  av  a  2  s. co m

    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());
    }
}

From source file:com.android.builder.internal.incremental.FileEntity.java

/**
 * Computes the sha1 of a file and returns it.
 *
 * @param f the file to compute the sha1 for.
 * @return the sha1 value/*from   w w  w  .  j  a  v  a  2s .  c  om*/
 * @throws Sha1Exception if the sha1 value cannot be computed.
 */
static String getSha1(File f) throws Sha1Exception {
    synchronized (sBuffer) {

        try {
            HashCode value = ByteStreams.hash(Files.newInputStreamSupplier(f), Hashing.sha1());
            return value.toString();
        } catch (Exception e) {
            throw new Sha1Exception(f, e);
        }
    }
}

From source file:org.glowroot.agent.central.SharedQueryTextLimiter.java

List<Trace.SharedQueryText> reduceTracePayloadWherePossible(List<Trace.SharedQueryText> sharedQueryTexts) {
    List<Trace.SharedQueryText> updatedSharedQueryTexts = Lists.newArrayList();
    for (Trace.SharedQueryText sharedQueryText : sharedQueryTexts) {
        // local collection always passes in full text
        checkState(sharedQueryText.getTruncatedText().isEmpty());
        checkState(sharedQueryText.getTruncatedEndText().isEmpty());
        checkState(sharedQueryText.getFullTextSha1().isEmpty());
        String fullText = sharedQueryText.getFullText();
        if (fullText.length() > 2 * Constants.TRACE_QUERY_TEXT_TRUNCATE) {
            String fullTextSha1 = Hashing.sha1().hashString(fullText, UTF_8).toString();
            if (sentInThePastDay.getIfPresent(fullTextSha1) == null) {
                // need to send full text
                updatedSharedQueryTexts.add(sharedQueryText);
            } else {
                // ok to just send truncated text
                updatedSharedQueryTexts.add(Trace.SharedQueryText.newBuilder()
                        .setTruncatedText(fullText.substring(0, Constants.TRACE_QUERY_TEXT_TRUNCATE))
                        .setTruncatedEndText(fullText.substring(
                                fullText.length() - Constants.TRACE_QUERY_TEXT_TRUNCATE, fullText.length()))
                        .setFullTextSha1(fullTextSha1).build());
            }//from w  ww .  ja v  a  2 s  .  co  m
        } else {
            updatedSharedQueryTexts.add(sharedQueryText);
        }
    }
    return updatedSharedQueryTexts;
}

From source file:org.elasticsearch.script.groovy.GroovyScriptEngineService.java

@Override
public Object compile(String script) {
    try {// w w  w .j a v a2 s .  c o  m
        return loader.parseClass(script, Hashing.sha1().hashString(script, Charsets.UTF_8).toString());
    } catch (Throwable e) {
        if (logger.isTraceEnabled()) {
            logger.trace("exception compiling Groovy script:", e);
        }
        throw new GroovyScriptCompilationException(ExceptionsHelper.detailedMessage(e));
    }
}

From source file:com.facebook.buck.rules.AbstractNodeBuilder.java

public TargetNode<A, B> build() {
    try {//  w  w  w .ja v a2  s  . c  o  m
        HashCode hash = rawHashCode == null ? Hashing.sha1().hashString(target.getFullyQualifiedName(), UTF_8)
                : rawHashCode;
        TargetNodeFactory factory = new TargetNodeFactory(TYPE_COERCER_FACTORY);
        TargetNode<A, B> node = factory.create(
                // This hash will do in a pinch.
                hash, description, arg, filesystem, target, getDepsFromArg(),
                ImmutableSet
                        .of(VISIBILITY_PATTERN_PARSER.parse(null, VisibilityPatternParser.VISIBILITY_PUBLIC)),
                cellRoots);
        if (selectedVersions.isPresent()) {
            node = node.withTargetConstructorArgDepsAndSelectedVerisons(node.getBuildTarget(),
                    node.getConstructorArg(), node.getDeclaredDeps(), node.getExtraDeps(), selectedVersions);
        }
        return node;
    } catch (NoSuchBuildTargetException e) {
        throw Throwables.propagate(e);
    }
}

From source file:org.haiku.haikudepotserver.userrating.CreatedUserRatingSyndEntrySupplier.java

@Override
public List<SyndEntry> generate(final FeedSpecification specification) {
    Preconditions.checkNotNull(specification);

    if (specification.getSupplierTypes().contains(FeedSpecification.SupplierType.CREATEDUSERRATING)) {

        if (null != specification.getPkgNames() && specification.getPkgNames().isEmpty()) {
            return Collections.emptyList();
        }/*from  w w  w.ja va  2 s  .com*/

        ObjectSelect<UserRating> objectSelect = ObjectSelect.query(UserRating.class)
                .where(UserRating.ACTIVE.isTrue()).and(UserRating.PKG_VERSION.dot(PkgVersion.ACTIVE).isTrue())
                .and(UserRating.PKG_VERSION.dot(PkgVersion.PKG).dot(Pkg.ACTIVE).isTrue())
                .statementFetchSize(specification.getLimit()).orderBy(UserRating.CREATE_TIMESTAMP.desc());

        if (null != specification.getPkgNames()) {
            objectSelect.and(
                    UserRating.PKG_VERSION.dot(PkgVersion.PKG).dot(Pkg.NAME).in(specification.getPkgNames()));
        }

        List<UserRating> userRatings = objectSelect.select(serverRuntime.newContext());

        return userRatings.stream().map(ur -> {
            SyndEntry entry = new SyndEntryImpl();
            entry.setPublishedDate(ur.getCreateTimestamp());
            entry.setUpdatedDate(ur.getModifyTimestamp());
            entry.setAuthor(ur.getUser().getNickname());
            entry.setUri(URI_PREFIX + Hashing.sha1()
                    .hashUnencodedChars(String.format("%s_::_%s_::_%s_::_%s",
                            this.getClass().getCanonicalName(), ur.getPkgVersion().getPkg().getName(),
                            ur.getPkgVersion().toVersionCoordinates().toString(), ur.getUser().getNickname()))
                    .toString());
            entry.setLink(String.format("%s/#!/userrating/%s", baseUrl, ur.getCode()));

            entry.setTitle(messageSource.getMessage(
                    "feed.createdUserRating.atom.title", new Object[] {
                            ur.getPkgVersion().toStringWithPkgAndArchitecture(), ur.getUser().getNickname() },
                    new Locale(specification.getNaturalLanguageCode())));

            String contentString = ur.getComment();

            if (null != contentString && contentString.length() > CONTENT_LENGTH) {
                contentString = contentString.substring(0, CONTENT_LENGTH) + "...";
            }

            // if there is a rating then express this as a string using unicode
            // characters.

            if (null != ur.getRating()) {
                contentString = buildRatingIndicator(ur.getRating())
                        + (Strings.isNullOrEmpty(contentString) ? "" : " -- " + contentString);
            }

            SyndContentImpl content = new SyndContentImpl();
            content.setType(MediaType.PLAIN_TEXT_UTF_8.type());
            content.setValue(contentString);
            entry.setDescription(content);

            return entry;
        }).collect(Collectors.toList());

    }

    return Collections.emptyList();
}

From source file:com.facebook.buck.apple.AbstractProvisioningProfileMetadata.java

public static ProvisioningProfileMetadata fromProvisioningProfilePath(ProcessExecutor executor,
        ImmutableList<String> readCommand, Path profilePath) throws IOException, InterruptedException {
    Set<ProcessExecutor.Option> options = EnumSet.of(ProcessExecutor.Option.EXPECTING_STD_OUT);

    // Extract the XML from its signed message wrapper.
    ProcessExecutorParams processExecutorParams = ProcessExecutorParams.builder().addAllCommand(readCommand)
            .addCommand(profilePath.toString()).build();
    ProcessExecutor.Result result;
    result = executor.launchAndExecute(processExecutorParams, options, /* stdin */ Optional.empty(),
            /* timeOutMs */ Optional.empty(), /* timeOutHandler */ Optional.empty());
    if (result.getExitCode() != 0) {
        throw new IOException(result.getMessageForResult("Invalid provisioning profile: " + profilePath));
    }//  w  w w  .  ja v  a  2  s .co m

    try {
        NSDictionary plist = (NSDictionary) PropertyListParser.parse(result.getStdout().get().getBytes());
        Date expirationDate = ((NSDate) plist.get("ExpirationDate")).getDate();
        String uuid = ((NSString) plist.get("UUID")).getContent();

        ImmutableSet.Builder<HashCode> certificateFingerprints = ImmutableSet.builder();
        NSArray certificates = (NSArray) plist.get("DeveloperCertificates");
        HashFunction hasher = Hashing.sha1();
        if (certificates != null) {
            for (NSObject item : certificates.getArray()) {
                certificateFingerprints.add(hasher.hashBytes(((NSData) item).bytes()));
            }
        }

        ImmutableMap.Builder<String, NSObject> builder = ImmutableMap.builder();
        NSDictionary entitlements = ((NSDictionary) plist.get("Entitlements"));
        for (String key : entitlements.keySet()) {
            builder = builder.put(key, entitlements.objectForKey(key));
        }
        String appID = entitlements.get("application-identifier").toString();

        return ProvisioningProfileMetadata.builder().setAppID(ProvisioningProfileMetadata.splitAppID(appID))
                .setExpirationDate(expirationDate).setUUID(uuid).setProfilePath(profilePath)
                .setEntitlements(builder.build())
                .setDeveloperCertificateFingerprints(certificateFingerprints.build()).build();
    } catch (Exception e) {
        throw new IllegalArgumentException("Malformed embedded plist: " + e);
    }
}