List of usage examples for com.google.common.hash Hasher putBytes
@Override Hasher putBytes(byte[] bytes, int off, int len);
From source file:de.dentrassi.pm.aspect.common.HashHelper.java
public static Map<String, HashCode> createChecksums(final Path file, final Map<String, HashFunction> functions) throws IOException { if (functions.isEmpty()) { return Collections.emptyMap(); }/*from w ww . j a v a 2 s.co m*/ // init hashers final Map<String, Hasher> hasherMap = new HashMap<>(); final Hasher[] hashers = new Hasher[functions.size()]; int i = 0; for (final Map.Entry<String, HashFunction> entry : functions.entrySet()) { hashers[i] = entry.getValue().newHasher(); hasherMap.put(entry.getKey(), hashers[i]); i++; } // read data try (BufferedInputStream is = new BufferedInputStream(new FileInputStream(file.toString()))) { final byte[] buffer = new byte[4096]; int len; while ((len = is.read(buffer)) >= 0) { for (final Hasher hasher : hashers) { hasher.putBytes(buffer, 0, len); } } } // finalize hashes final Map<String, HashCode> result = new HashMap<String, HashCode>(hashers.length); for (final Map.Entry<String, Hasher> entry : hasherMap.entrySet()) { result.put(entry.getKey(), entry.getValue().hash()); } // return result return result; }
From source file:de.dentrassi.pm.common.utils.HashHelper.java
public static Map<String, HashCode> createChecksums(final InputStream stream, final Map<String, HashFunction> functions) throws IOException { if (functions.isEmpty()) { return Collections.emptyMap(); }//from ww w .j ava2 s. c om // init hashers final Map<String, Hasher> hasherMap = new HashMap<>(); final Hasher[] hashers = new Hasher[functions.size()]; int i = 0; for (final Map.Entry<String, HashFunction> entry : functions.entrySet()) { hashers[i] = entry.getValue().newHasher(); hasherMap.put(entry.getKey(), hashers[i]); i++; } // read data final byte[] buffer = new byte[4096]; int len; while ((len = stream.read(buffer)) >= 0) { for (final Hasher hasher : hashers) { hasher.putBytes(buffer, 0, len); } } // finalize hashes final Map<String, HashCode> result = new HashMap<String, HashCode>(hashers.length); for (final Map.Entry<String, Hasher> entry : hasherMap.entrySet()) { result.put(entry.getKey(), entry.getValue().hash()); } // return result return result; }
From source file:org.apache.servicecomb.demo.signature.SignatureUtils.java
public static String genSignature(HttpServletResponseEx responseEx) { Hasher hasher = Hashing.sha256().newHasher(); byte[] bytes = responseEx.getBodyBytes(); if (bytes != null) { hasher.putBytes(bytes, 0, responseEx.getBodyBytesLength()); }/* w w w.j a v a 2s. c o m*/ return hasher.hash().toString(); }
From source file:com.android.repository.api.Downloader.java
/** * Hash the given input stream./*w w w . java2s . c o m*/ * @param in The stream to hash. It will be fully consumed but not closed. * @param fileSize The expected length of the stream, for progress display purposes. * @param progress The indicator will be updated with the expected completion fraction. * @return The sha1 hash of the input stream. * @throws IOException IF there's a problem reading from the stream. */ @VisibleForTesting @NonNull static String hash(@NonNull InputStream in, long fileSize, @NonNull ProgressIndicator progress) throws IOException { progress.setText("Checking existing file..."); Hasher sha1 = Hashing.sha1().newHasher(); byte[] buf = new byte[5120]; long totalRead = 0; int bytesRead; while ((bytesRead = in.read(buf)) > 0) { sha1.putBytes(buf, 0, bytesRead); progress.setFraction((double) totalRead / (double) fileSize); } return sha1.hash().toString(); }
From source file:com.google.devtools.build.android.FileDeDuplicator.java
private static HashCode hashPath(Path file, final Hasher hasher) throws IOException { byte[] tmpBuffer = new byte[512]; final InputStream in = Files.newInputStream(file); for (int read = in.read(tmpBuffer); read > 0; read = in.read(tmpBuffer)) { hasher.putBytes(tmpBuffer, 0, read); }/* w ww . jav a 2s . c o m*/ final HashCode fileHash = hasher.hash(); in.close(); return fileHash; }
From source file:io.takari.jdkget.OracleWebsiteTransport.java
private static String hash(File f, HashFunction hf) throws IOException { Hasher h = hf.newHasher(); try (InputStream in = new FileInputStream(f)) { byte[] buf = new byte[8192]; int l;// w ww.java2s .co m while ((l = in.read(buf)) != -1) { h.putBytes(buf, 0, l); } } return h.hash().toString(); }
From source file:com.google.devtools.build.lib.bazel.repository.HttpDownloader.java
public static String getHash(Hasher hasher, Path path) throws IOException { byte byteBuffer[] = new byte[BUFFER_SIZE]; try (InputStream stream = path.getInputStream()) { int numBytesRead = stream.read(byteBuffer); while (numBytesRead != -1) { if (numBytesRead != 0) { // If more than 0 bytes were read, add them to the hash. hasher.putBytes(byteBuffer, 0, numBytesRead); }//from w w w .j a va2 s .com numBytesRead = stream.read(byteBuffer); } } return hasher.hash().toString(); }
From source file:com.google.devtools.build.lib.bazel.repository.cache.RepositoryCache.java
/** * Obtain the checksum of a file.// ww w . ja v a 2 s .c om * * @param keyType The type of hash function. e.g. SHA-1, SHA-256. * @param path The path to the file. * @throws IOException */ public static String getChecksum(KeyType keyType, Path path) throws IOException { Hasher hasher = keyType.newHasher(); byte[] byteBuffer = new byte[BUFFER_SIZE]; try (InputStream stream = path.getInputStream()) { int numBytesRead = stream.read(byteBuffer); while (numBytesRead != -1) { if (numBytesRead != 0) { // If more than 0 bytes were read, add them to the hash. hasher.putBytes(byteBuffer, 0, numBytesRead); } numBytesRead = stream.read(byteBuffer); } } return hasher.hash().toString(); }
From source file:io.servicecomb.demo.signature.SignatureUtils.java
public static String genSignature(HttpServletRequestEx requestEx) { Hasher hasher = Hashing.sha256().newHasher(); hasher.putString(requestEx.getRequestURI(), StandardCharsets.UTF_8); for (String paramName : paramNames) { String paramValue = requestEx.getHeader(paramName); if (paramValue != null) { hasher.putString(paramName, StandardCharsets.UTF_8); hasher.putString(paramValue, StandardCharsets.UTF_8); System.out.printf("%s %s\n", paramName, paramValue); }/*from w ww . j a v a 2s . c o m*/ } byte[] bytes = requestEx.getBodyBytes(); if (bytes != null) { hasher.putBytes(bytes, 0, requestEx.getBodyBytesLength()); } return hasher.hash().toString(); }
From source file:org.apache.servicecomb.demo.signature.SignatureUtils.java
public static String genSignature(HttpServletRequestEx requestEx) { Hasher hasher = Hashing.sha256().newHasher(); hasher.putString(requestEx.getRequestURI(), StandardCharsets.UTF_8); for (String paramName : paramNames) { String paramValue = requestEx.getHeader(paramName); if (paramValue != null) { hasher.putString(paramName, StandardCharsets.UTF_8); hasher.putString(paramValue, StandardCharsets.UTF_8); System.out.printf("%s %s\n", paramName, paramValue); }//from w w w. j a v a 2 s . c om } if (!StringUtils.startsWithIgnoreCase(requestEx.getContentType(), MediaType.APPLICATION_FORM_URLENCODED)) { byte[] bytes = requestEx.getBodyBytes(); if (bytes != null) { hasher.putBytes(bytes, 0, requestEx.getBodyBytesLength()); } } return hasher.hash().toString(); }