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

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

Introduction

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

Prototype

public static HashFunction sha256() 

Source Link

Document

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

Usage

From source file:com.google.devtools.kythe.extractors.jvm.JarExtractor.java

public static void main(String[] args) throws IOException, ExtractionException {
    JvmExtractor.Options options = new JvmExtractor.Options();
    JCommander jc = new JCommander(options, args);
    jc.setProgramName("jar_extractor");
    if (options.help) {
        jc.usage();// www . j av  a 2 s.  com
        System.exit(2);
    }

    CompilationDescription indexInfo = JvmExtractor.extract(options);

    String outputFile = System.getenv("KYTHE_OUTPUT_FILE");
    if (!Strings.isNullOrEmpty(outputFile)) {
        IndexInfoUtils.writeIndexInfoToFile(indexInfo, outputFile);
    } else {
        String outputDir = System.getenv("KYTHE_OUTPUT_DIRECTORY");
        if (Strings.isNullOrEmpty(outputDir)) {
            throw new IllegalArgumentException(
                    "required KYTHE_OUTPUT_FILE or KYTHE_OUTPUT_DIRECTORY environment variable is unset");
        }
        if (Strings.isNullOrEmpty(System.getenv("KYTHE_INDEX_PACK"))) {
            String name = Hashing.sha256().hashUnencodedChars(Joiner.on(" ").join(args)).toString();
            String path = IndexInfoUtils.getIndexPath(outputDir, name).toString();
            IndexInfoUtils.writeIndexInfoToFile(indexInfo, path);
        } else {
            new Archive(outputDir).writeDescription(indexInfo);
        }
    }
}

From source file:utils.SHA2Generator.java

/**
 *  Return file's SHA256./*from  w ww .java 2 s. c  o m*/
 * @param filePath the path where the file to have the md5 generated belongs.
 * @return The SHA256 in String
 * @throws NoSuchAlgorithmException
 * @throws IOException
 */
public static String generate(String filePath) throws NoSuchAlgorithmException, IOException {
    File file = new File(filePath);
    String sha2 = BaseEncoding.base16().encode(Files.hash(file, Hashing.sha256()).asBytes()).toLowerCase();
    return sha2;
}

From source file:it.anyplace.sync.core.utils.BlockUtils.java

public static String hashBlocks(List<BlockInfo> blocks) {
    return BaseEncoding.base16().encode(Hashing.sha256()
            .hashBytes(Joiner.on(",").join(Iterables.transform(blocks, new Function<BlockInfo, String>() {
                @Override//from   w  ww.  j ava2s. co  m
                public String apply(BlockInfo input) {
                    return input.getHash();
                }
            })).getBytes()).asBytes());
}

From source file:brooklyn.rest.security.PasswordHasher.java

public static String sha256(String salt, String password) {
    byte[] salted = Charsets.UTF_8.encode((salt == null ? "" : salt) + password).array();
    HashCode hash = Hashing.sha256().hashBytes(salted);
    return hash.toString();
}

From source file:org.apache.brooklyn.rest.security.PasswordHasher.java

public static String sha256(String salt, String password) {
    if (salt == null)
        salt = "";
    byte[] bytes = (salt + password).getBytes(Charsets.UTF_8);
    HashCode hash = Hashing.sha256().hashBytes(bytes);
    return hash.toString();
}

From source file:com.zxy.commons.codec.utils.SHAUtils.java

/**
 * sha256 encode//from   w  ww .  ja  va  2  s.c  o  m
 * 
 * @param source 
 * @return sha256 encode string
 */
public static String encodeSHA256(String source) {
    return Hashing.sha256().hashBytes(source.getBytes()).toString();
}

From source file:com.google.devtools.build.lib.worker.WorkerFilesHash.java

public static HashCode getWorkerFilesHash(Iterable<? extends ActionInput> toolFiles,
        ActionExecutionContext actionExecutionContext) throws IOException {
    Hasher hasher = Hashing.sha256().newHasher();
    for (ActionInput tool : toolFiles) {
        hasher.putString(tool.getExecPathString(), Charset.defaultCharset());
        hasher.putBytes(actionExecutionContext.getActionInputFileCache().getDigest(tool));
    }/*from w w w . j  av  a2s .  c om*/
    return hasher.hash();
}

From source file:com.lithium.flow.util.HashFunctions.java

@Nonnull
public static HashFunction of(@Nonnull String name) {
    checkNotNull(name);/*  www.  ja v  a 2s. c  om*/
    switch (name) {
    case "adler32":
        return Hashing.adler32();
    case "crc32":
        return Hashing.crc32();
    case "md5":
        return Hashing.md5();
    case "sha1":
        return Hashing.sha1();
    case "sha256":
        return Hashing.sha256();
    case "sha512":
        return Hashing.sha512();
    case "sipHash24":
        return Hashing.sipHash24();
    case "murmur3_32":
        return Hashing.murmur3_32();
    case "murmur3_128":
        return Hashing.murmur3_128();
    default:
        throw new RuntimeException("unknown hash: " + name);
    }
}

From source file:io.atomix.primitive.PrimitiveId.java

/**
 * Creates a snapshot ID from the given string.
 *
 * @param id the string from which to create the identifier
 * @return the snapshot identifier//from   www  . j  a va  2  s .c o  m
 */
public static PrimitiveId from(String id) {
    return from(Hashing.sha256().hashString(id, StandardCharsets.UTF_8).asLong());
}

From source file:utility.HashedPasswordGenerator.java

public String generateHash(String password) {
    return Hashing.sha256().hashString(password, Charsets.UTF_8).toString();
}