Search in sources :

Example 66 with HashCode

use of com.google.common.hash.HashCode in project che by eclipse.

the class PBKDF2PasswordEncryptor method encrypt.

@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);
}
Also used : HashCode(com.google.common.hash.HashCode)

Example 67 with HashCode

use of com.google.common.hash.HashCode in project che by eclipse.

the class SHA512PasswordEncryptor method encrypt.

@Override
public String encrypt(String password) {
    requireNonNull(password, "Required non-null password");
    // generate salt
    final byte[] salt = new byte[SALT_BYTES_LENGTH];
    SECURE_RANDOM.nextBytes(salt);
    // sha512(password + salt)
    final HashCode hash = Hashing.sha512().hashBytes(Bytes.concat(password.getBytes(PWD_CHARSET), salt));
    final HashCode saltHash = HashCode.fromBytes(salt);
    // add salt to the hash, result length (512 / 8) * 2 + (64 / 8) * 2 = 144
    return hash.toString() + saltHash.toString();
}
Also used : HashCode(com.google.common.hash.HashCode)

Example 68 with HashCode

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

the class TargetsCommand method processTargetHash.

private void processTargetHash(BuildTarget buildTarget, Map<BuildTarget, ShowOptions.Builder> showRulesResult, ImmutableMap<BuildTarget, HashCode> finalHashes) {
    ShowOptions.Builder showOptionsBuilder = getShowOptionBuilder(showRulesResult, buildTarget);
    HashCode hashCode = getHashCodeOrThrow(finalHashes, buildTarget);
    showOptionsBuilder.setTargetHash(hashCode.toString());
}
Also used : HashCode(com.google.common.hash.HashCode)

Example 69 with HashCode

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

the class AutodepsWriter method writeSignedFile.

/**
   * Writes the file only if the contents are different to avoid creating noise for Watchman/buckd.
   * @param deps Keys must be sorted so the output is generated consistently.
   * @param includeSignature Whether to insert a signature for the contents of the file.
   * @param generatedFile Where to write the generated output.
   * @param mapper To aid in JSON serialization.
   * @return whether the file was written
   */
private static boolean writeSignedFile(SortedMap<String, SortedMap<String, Iterable<String>>> deps, boolean includeSignature, Path generatedFile, ObjectMapper mapper) throws IOException {
    try (ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        HashingOutputStream hashingOutputStream = new HashingOutputStream(Hashing.sha1(), bytes)) {
        ObjectWriter jsonWriter = mapper.writer(PRETTY_PRINTER.get());
        jsonWriter.writeValue(includeSignature ? hashingOutputStream : bytes, deps);
        // Flush a trailing newline through the HashingOutputStream so it is included both the
        // output and the signature calculation.
        hashingOutputStream.write('\n');
        String serializedJson = bytes.toString(Charsets.UTF_8.name());
        String contentsToWrite;
        if (includeSignature) {
            HashCode hash = hashingOutputStream.hash();
            contentsToWrite = String.format(AUTODEPS_CONTENTS_FORMAT_STRING, hash, serializedJson);
        } else {
            contentsToWrite = serializedJson;
        }
        // to indiscriminately invalidate any cached build rules for the associated build file.
        if (generatedFile.toFile().isFile()) {
            String existingContents = com.google.common.io.Files.toString(generatedFile.toFile(), Charsets.UTF_8);
            if (contentsToWrite.equals(existingContents)) {
                return false;
            }
        }
        try (Writer writer = Files.newBufferedWriter(generatedFile, Charsets.UTF_8)) {
            writer.write(contentsToWrite);
        }
        return true;
    }
}
Also used : HashCode(com.google.common.hash.HashCode) HashingOutputStream(com.google.common.hash.HashingOutputStream) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectWriter(com.fasterxml.jackson.databind.ObjectWriter) Writer(java.io.Writer)

Example 70 with HashCode

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

the class RuleKeyTest method createEmptyRuleKey.

private RuleKeyBuilder<RuleKeyResult<RuleKey>> createEmptyRuleKey(SourcePathResolver resolver, SourcePathRuleFinder ruleFinder) {
    FileHashCache fileHashCache = new FileHashCache() {

        @Override
        public void invalidate(Path path) {
        }

        @Override
        public void invalidateAll() {
        }

        @Override
        public HashCode get(Path path) {
            return HashCode.fromString("deadbeef");
        }

        @Override
        public HashCode get(ArchiveMemberPath archiveMemberPath) {
            return HashCode.fromString("deadbeef");
        }

        @Override
        public long getSize(Path path) {
            return 0;
        }

        @Override
        public void set(Path path, HashCode hashCode) {
        }
    };
    BuildTarget buildTarget = BuildTargetFactory.newInstance("//some:example");
    BuildRule buildRule = new FakeBuildRule(buildTarget, resolver);
    return new DefaultRuleKeyFactory(0, fileHashCache, resolver, ruleFinder).newBuilderForTesting(buildRule);
}
Also used : ArchiveMemberPath(com.facebook.buck.io.ArchiveMemberPath) Path(java.nio.file.Path) NullFileHashCache(com.facebook.buck.util.cache.NullFileHashCache) FakeFileHashCache(com.facebook.buck.testutil.FakeFileHashCache) DefaultFileHashCache(com.facebook.buck.util.cache.DefaultFileHashCache) StackedFileHashCache(com.facebook.buck.util.cache.StackedFileHashCache) FileHashCache(com.facebook.buck.util.cache.FileHashCache) ArchiveMemberPath(com.facebook.buck.io.ArchiveMemberPath) HashCode(com.google.common.hash.HashCode) DefaultRuleKeyFactory(com.facebook.buck.rules.keys.DefaultRuleKeyFactory) BuildTarget(com.facebook.buck.model.BuildTarget)

Aggregations

HashCode (com.google.common.hash.HashCode)115 Path (java.nio.file.Path)41 Test (org.junit.Test)39 BuildTarget (com.facebook.buck.model.BuildTarget)19 FakeFileHashCache (com.facebook.buck.testutil.FakeFileHashCache)13 ProjectFilesystem (com.facebook.buck.io.ProjectFilesystem)12 ImmutableMap (com.google.common.collect.ImmutableMap)12 IOException (java.io.IOException)12 PathSourcePath (com.facebook.buck.rules.PathSourcePath)11 FileHashCache (com.facebook.buck.util.cache.FileHashCache)11 FakeProjectFilesystem (com.facebook.buck.testutil.FakeProjectFilesystem)10 Sha1HashCode (com.facebook.buck.util.sha1.Sha1HashCode)10 Hasher (com.google.common.hash.Hasher)10 Map (java.util.Map)8 UnflavoredBuildTarget (com.facebook.buck.model.UnflavoredBuildTarget)7 SourcePath (com.facebook.buck.rules.SourcePath)7 BuckEventBus (com.facebook.buck.event.BuckEventBus)6 DefaultBuildTargetSourcePath (com.facebook.buck.rules.DefaultBuildTargetSourcePath)6 SourcePathResolver (com.facebook.buck.rules.SourcePathResolver)5 SourcePathRuleFinder (com.facebook.buck.rules.SourcePathRuleFinder)5