List of usage examples for com.google.common.hash Hashing sha1
public static HashFunction sha1()
From source file:io.macgyver.plugin.cmdb.AppInstanceManager.java
public String computeSignature(ObjectNode n, Set<String> exclusions) { List<String> list = Lists.newArrayList(n.fieldNames()); Collections.sort(list);//from w ww. j av a2s . com HashingOutputStream hos = new HashingOutputStream(Hashing.sha1(), ByteStreams.nullOutputStream()); list.forEach(it -> { if (exclusions != null && !exclusions.contains(it)) { JsonNode val = n.get(it); if (val.isObject() || val.isArray()) { // skipping } else { try { hos.write(it.getBytes()); hos.write(val.toString().getBytes()); } catch (IOException e) { } } } }); return hos.hash().toString(); }
From source file:org.sonatype.nexus.proxy.maven.maven2.M2GroupRepository.java
/** * Aggregates metadata from all member repositories *///from w w w.ja v a2 s. c o m private StorageItem doRetrieveMetadata(ResourceStoreRequest request) throws StorageException, IllegalOperationException, UnsupportedStorageOperationException, ItemNotFoundException { List<StorageItem> items = doRetrieveItems(request); if (items.isEmpty()) { throw new ItemNotFoundException( reasonFor(request, this, "Metadata %s not found in any of the members of %s.", request.getRequestPath(), RepositoryStringUtils.getHumanizedNameString(this))); } if (!isMergeMetadata()) { // not merging: return the 1st and ciao return items.get(0); } List<Metadata> existingMetadatas = new ArrayList<Metadata>(); try { for (StorageItem item : items) { if (!(item instanceof StorageFileItem)) { break; } StorageFileItem fileItem = (StorageFileItem) item; try { existingMetadatas.add(parseMetadata(fileItem)); } catch (IOException e) { log.warn("IOException during parse of metadata UID=\"" + fileItem.getRepositoryItemUid().toString() + "\", will be skipped from aggregation!", e); eventBus().post(newMetadataFailureEvent(fileItem, "Invalid metadata served by repository. If repository is proxy, please check out what is it serving!")); } catch (MetadataException e) { log.warn("Metadata exception during parse of metadata from UID=\"" + fileItem.getRepositoryItemUid().toString() + "\", will be skipped from aggregation!", e); eventBus().post(newMetadataFailureEvent(fileItem, "Invalid metadata served by repository. If repository is proxy, please check out what is it serving!")); } } if (existingMetadatas.isEmpty()) { throw new ItemNotFoundException( reasonFor(request, this, "Metadata %s not parseable in any of the members of %s.", request.getRequestPath(), RepositoryStringUtils.getHumanizedNameString(this))); } Metadata result = existingMetadatas.get(0); // do a merge if necessary if (existingMetadatas.size() > 1) { List<MetadataOperation> ops = new ArrayList<MetadataOperation>(); for (int i = 1; i < existingMetadatas.size(); i++) { ops.add(new NexusMergeOperation(new MetadataOperand(existingMetadatas.get(i)))); } final Collection<MetadataException> metadataExceptions = MetadataBuilder .changeMetadataIgnoringFailures(result, ops); if (metadataExceptions != null && !metadataExceptions.isEmpty()) { for (final MetadataException metadataException : metadataExceptions) { log.warn("Ignored exception during M2 metadata merging: " + metadataException.getMessage() + " (request " + request.getRequestPath() + ")", metadataException); } } } // build the result item ByteArrayOutputStream resultOutputStream = new ByteArrayOutputStream(); MetadataBuilder.write(result, resultOutputStream); StorageItem item = createMergedMetadataItem(request, resultOutputStream.toByteArray(), items); // build checksum files String md5Digest = Hashing.md5().hashBytes(resultOutputStream.toByteArray()).toString(); String sha1Digest = Hashing.sha1().hashBytes(resultOutputStream.toByteArray()).toString(); storeMergedMetadataItemDigest(request, md5Digest, items, "MD5"); storeMergedMetadataItemDigest(request, sha1Digest, items, "SHA1"); resultOutputStream.close(); if (log.isDebugEnabled()) { log.debug("Item for path " + request.toString() + " merged from " + Integer.toString(items.size()) + " found items."); } return item; } catch (IOException e) { throw new LocalStorageException("Got IOException during M2 metadata merging.", e); } catch (MetadataException e) { throw new LocalStorageException("Got MetadataException during M2 metadata merging.", e); } }
From source file:com.netflix.spinnaker.cats.dynomite.cache.DynomiteCache.java
private boolean hashCheck(Map<String, String> hashes, String id, String serializedValue, Map<String, String> updatedHashes, boolean hasTtl) { if (options.isHashingEnabled() && !hasTtl) { final String hash = Hashing.sha1().newHasher().putString(serializedValue, UTF_8).hash().toString(); final String existingHash = hashes.get(id); if (hash.equals(existingHash)) { return true; }/* w ww .ja va 2 s .co m*/ updatedHashes.put(id, hash); } return false; }
From source file:com.infinities.keystone4j.utils.Cms.java
public String hashToken(String tokenid, Algorithm mode) throws UnsupportedEncodingException, NoSuchAlgorithmException, DecoderException { if (mode == null) { mode = Algorithm.md5;/*from www. ja va 2s. co m*/ } if (Strings.isNullOrEmpty(tokenid)) { throw new NullPointerException("invalid tokenid"); } if (isAsn1Token(tokenid) || isPkiz(tokenid)) { HashFunction hf = Hashing.md5(); if (mode == Algorithm.sha1) { hf = Hashing.sha1(); } else if (mode == Algorithm.sha256) { hf = Hashing.sha256(); } else if (mode == Algorithm.sha512) { hf = Hashing.sha512(); } HashCode hc = hf.newHasher().putString(tokenid).hash(); return toHex(hc.asBytes()); } else { return tokenid; } }
From source file:org.apache.jackrabbit.oak.segment.Compactor.java
private static String getBlobKey(Blob blob) throws IOException { InputStream stream = blob.getNewStream(); try {/*from ww w. j av a2 s .c o m*/ byte[] buffer = new byte[SegmentWriter.BLOCK_SIZE]; int n = IOUtils.readFully(stream, buffer, 0, buffer.length); return blob.length() + ":" + Hashing.sha1().hashBytes(buffer, 0, n); } finally { stream.close(); } }
From source file:com.android.builder.internal.utils.FileCache.java
/** * Invokes a task that accesses a file/directory with both inter-process and inter-thread * locking.//from www . jav a 2 s.c o m */ private void doProcessLocked(@NonNull File accessedFile, @NonNull IOExceptionRunnable task) throws IOException { // We use Java's file-locking API to enable inter-process locking. The API permits only one // thread per process to wait on a file lock, and it will throw an // OverlappingFileLockException if more than one thread in a process attempt to acquire the // same file lock. Therefore, we run the file-locking mechanism also with inter-thread // locking. doThreadLocked(accessedFile, () -> { // Create a lock file for the task. We don't use the file being accessed // (which might not already exist) as the lock file since we don't want it to be // affected by our locking mechanism (specifically, the locking mechanism will // always create the lock file and delete it after the task is executed; // however, a task may or may not create the file that it is supposed to // access). String lockFileName = Hashing.sha1().hashString(accessedFile.getCanonicalPath(), StandardCharsets.UTF_8) .toString(); File lockFile = new File(mCacheDirectory, lockFileName); FileChannel fileChannel = new RandomAccessFile(lockFile, "rw").getChannel(); FileLock fileLock = fileChannel.lock(); try { task.run(); } finally { // Delete the lock file first; if we delete the lock file after the file // lock is released, another process might have grabbed the file lock and // we will be deleting the file while the other process is using it. lockFile.delete(); fileLock.release(); fileChannel.close(); } }); }
From source file:com.facebook.buck.android.SplitZipStep.java
private static String hexSha1(File file) throws IOException { return Files.hash(file, Hashing.sha1()).toString(); }
From source file:net.ftb.util.DownloadUtils.java
public static String fileSHA(File file) throws IOException { if (file.exists()) { return Files.hash(file, Hashing.sha1()).toString(); //FileInputStream fis = new FileInputStream(file); //String result = DigestUtils.sha1Hex(fis).toLowerCase(); //fis.close(); //return result; } else/*w w w. j ava 2 s .com*/ return ""; //return fileHash(file, "sha1").toLowerCase(); }
From source file:org.jooby.internal.assets.AssetWriter.java
private String sha1(final CharSequence source) { return BaseEncoding.base16().encode(Hashing.sha1().hashString(source, charset).asBytes()).substring(0, 8) .toLowerCase();// w ww . ja v a2s . co m }
From source file:com.google.devtools.build.lib.vfs.FileSystem.java
/** * Returns the MD5 digest of the file denoted by {@code path}. See * {@link Path#getMD5Digest} for specification. *///from w w w .j a va2 s. co m protected byte[] getSHA1Digest(final Path path) throws IOException { // Naive I/O implementation. TODO(olaola): optimize! return new ByteSource() { @Override public InputStream openStream() throws IOException { return getInputStream(path); } }.hash(Hashing.sha1()).asBytes(); }