List of usage examples for com.google.common.hash Hashing sha1
public static HashFunction sha1()
From source file:org.sonatype.nexus.proxy.maven.maven2.M2Repository.java
@Override protected StorageItem doRetrieveItem(ResourceStoreRequest request) throws IllegalOperationException, ItemNotFoundException, StorageException { // we do all this mockery (NEXUS-4218 and NEXUS-4243) ONLY when we are sure // that we deal with REMOTE REQUEST INITIATED BY OLD 2.x MAVEN and nothing else. // Before this fix, the code was executed whenever // "!ModelVersionUtility.LATEST_MODEL_VERSION.equals( userSupportedVersion ) )" was true // and it was true even for userSupportedVersion being null (ie. not user agent string supplied!)!!! final boolean remoteCall = request.getRequestContext().containsKey(AccessManager.REQUEST_REMOTE_ADDRESS); final String userAgent = (String) request.getRequestContext().get(AccessManager.REQUEST_AGENT); if (remoteCall && null != userAgent) { final Version userSupportedVersion = getClientSupportedVersion(userAgent); // we still can make up our mind here, we do this only if we know: this request is about metadata, // the client's metadata version is known and it is not the latest one if (M2ArtifactRecognizer.isMetadata(request.getRequestPath()) && userSupportedVersion != null && !ModelVersionUtility.LATEST_MODEL_VERSION.equals(userSupportedVersion)) { // metadata checksum files are calculated and cached as side-effect // of doRetrieveMetadata. final StorageFileItem mdItem; if (M2ArtifactRecognizer.isChecksum(request.getRequestPath())) { String path = request.getRequestPath(); if (request.getRequestPath().endsWith(".md5")) { path = path.substring(0, path.length() - 4); } else if (request.getRequestPath().endsWith(".sha1")) { path = path.substring(0, path.length() - 5); }/*w ww .jav a2 s. c o m*/ // we have to keep original reqest's flags: localOnly and remoteOnly are strange ones, so // we do a hack here // second, since we initiate a request for different path within a context of this request, // we need to be careful about it ResourceStoreRequest mdRequest = new ResourceStoreRequest(path, request.isRequestLocalOnly(), request.isRequestRemoteOnly()); mdRequest.getRequestContext().setParentContext(request.getRequestContext()); mdItem = (StorageFileItem) super.retrieveItem(false, mdRequest); } else { mdItem = (StorageFileItem) super.doRetrieveItem(request); } try { Metadata metadata; try (final InputStream inputStream = mdItem.getInputStream()) { metadata = MetadataBuilder.read(inputStream); } Version requiredVersion = getClientSupportedVersion(userAgent); Version metadataVersion = ModelVersionUtility.getModelVersion(metadata); if (requiredVersion == null || requiredVersion.equals(metadataVersion)) { return super.doRetrieveItem(request); } ModelVersionUtility.setModelVersion(metadata, requiredVersion); ByteArrayOutputStream mdOutput = new ByteArrayOutputStream(); MetadataBuilder.write(metadata, mdOutput); final byte[] content; if (M2ArtifactRecognizer.isChecksum(request.getRequestPath())) { String digest; if (request.getRequestPath().endsWith(".md5")) { digest = Hashing.md5().hashBytes(mdOutput.toByteArray()).toString(); } else { digest = Hashing.sha1().hashBytes(mdOutput.toByteArray()).toString(); } content = (digest + '\n').getBytes("UTF-8"); } else { content = mdOutput.toByteArray(); } String mimeType = getMimeSupport().guessMimeTypeFromPath(getMimeRulesSource(), request.getRequestPath()); ContentLocator contentLocator = new ByteArrayContentLocator(content, mimeType); DefaultStorageFileItem result = new DefaultStorageFileItem(this, request, true, false, contentLocator); result.setCreated(mdItem.getCreated()); result.setModified(System.currentTimeMillis()); return result; } catch (IOException e) { if (log.isDebugEnabled()) { log.error("Error parsing metadata, serving as retrieved", e); } else { log.error("Error parsing metadata, serving as retrieved: " + e.getMessage()); } return super.doRetrieveItem(request); } } } return super.doRetrieveItem(request); }
From source file:com.netflix.spinnaker.cats.redis.cache.RedisCache.java
/** * Compares the hash of serializedValue against an existing hash, if they do not match adds * serializedValue to keys and the new hash to updatedHashes. * * @param hashes the existing hash values * @param id the id of the item * @param serializedValue the serialized value * @param keys values to persist - if the hash does not match id and serializedValue are appended * @param updatedHashes hashes to persist - if the hash does not match adds an entry of id -> computed hash * @return true if the hash matched, false otherwise *///from w w w.ja v a 2 s .c o m private boolean hashCheck(Map<String, byte[]> hashes, String id, String serializedValue, List<String> keys, Map<byte[], byte[]> updatedHashes) { if (options.isHashingEnabled()) { final byte[] hash = Hashing.sha1().newHasher().putBytes(stringToBytes(serializedValue)).hash() .asBytes(); final byte[] existingHash = hashes.get(id); if (Arrays.equals(hash, existingHash)) { return true; } updatedHashes.put(stringToBytes(id), hash); } keys.add(id); keys.add(serializedValue); return false; }
From source file:com.facebook.buck.java.DefaultJavaLibrary.java
/** * Creates a Hasher containing the ABI keys of the dependencies. * @param rulesWithAbiToConsider a sorted set containing the dependencies whose ABI key will be * added to the hasher.//www . j a va2s.c om * @return a Hasher containing the ABI keys of the dependencies. */ private Hasher createHasherWithAbiKeyForDeps(SortedSet<HasJavaAbi> rulesWithAbiToConsider) { Hasher hasher = Hashing.sha1().newHasher(); for (HasJavaAbi ruleWithAbiToConsider : rulesWithAbiToConsider) { if (ruleWithAbiToConsider == this) { continue; } Sha1HashCode abiKey = ruleWithAbiToConsider.getAbiKey(); hasher.putUnencodedChars(abiKey.getHash()); } return hasher; }
From source file:com.pmeade.websocket.io.WebSocketServerInputStream.java
/** * Perform the initial WebSocket handshake. WebSockets connect with an * HTTP Request to upgrade the connection to a WebSocket connection. This * method ensures that the request is correctly formed, and provides * the appropriate response to the client. After this method is called, * further communication is performed solely with WebSocket frames. * @throws IOException if anything goes wrong with the underlying stream */// www . java 2 s. co m private void shakeHands() throws IOException { HttpRequest req = new HttpRequest(inputStream); String requestLine = req.get(HttpRequest.REQUEST_LINE); handshakeComplete = checkStartsWith(requestLine, "GET /") && checkContains(requestLine, "HTTP/") && req.get("Host") != null && checkContains(req.get("Upgrade"), "websocket") && checkContains(req.get("Connection"), "Upgrade") && "13".equals(req.get("Sec-WebSocket-Version")) && req.get("Sec-WebSocket-Key") != null; String nonce = req.get("Sec-WebSocket-Key"); if (handshakeComplete) { byte[] nonceBytes = BaseEncoding.base64().decode(nonce); if (nonceBytes.length != HANDSHAKE_NONCE_LENGTH) { handshakeComplete = false; } } // if we have met all the requirements if (handshakeComplete) { outputPeer.write(asUTF8("HTTP/1.1 101 Switching Protocols\r\n")); outputPeer.write(asUTF8("Upgrade: websocket\r\n")); outputPeer.write(asUTF8("Connection: upgrade\r\n")); outputPeer.write(asUTF8("Sec-WebSocket-Accept: ")); HashFunction hf = Hashing.sha1(); HashCode hc = hf.newHasher().putString(nonce, StandardCharsets.UTF_8) .putString(WEBSOCKET_ACCEPT_UUID, StandardCharsets.UTF_8).hash(); String acceptKey = BaseEncoding.base64().encode(hc.asBytes()); outputPeer.write(asUTF8(acceptKey)); outputPeer.write(asUTF8("\r\n\r\n")); } outputPeer.setHandshakeComplete(handshakeComplete); }
From source file:com.android.builder.internal.compiler.PreProcessCache.java
@Nullable protected Node createItemNode(@NonNull Document document, @NonNull T itemKey, @NonNull BaseItem item) throws IOException { if (!item.areOutputFilesPresent()) { return null; }//from ww w . j a v a2 s .c om Node itemNode = document.createElement(NODE_ITEM); Attr attr = document.createAttribute(ATTR_JAR); attr.setValue(item.getSourceFile().getPath()); itemNode.getAttributes().setNamedItem(attr); attr = document.createAttribute(ATTR_REVISION); attr.setValue(itemKey.getBuildToolsRevision().toString()); itemNode.getAttributes().setNamedItem(attr); HashCode hashCode = item.getSourceHash(); if (hashCode == null) { try { hashCode = Files.hash(item.getSourceFile(), Hashing.sha1()); } catch (IOException ex) { // If we can't compute the hash for whatever reason, simply skip this entry. return null; } } attr = document.createAttribute(ATTR_SHA1); attr.setValue(hashCode.toString()); itemNode.getAttributes().setNamedItem(attr); for (File dexFile : item.getOutputFiles()) { Node dexNode = document.createElement(NODE_DEX); itemNode.appendChild(dexNode); attr = document.createAttribute(ATTR_DEX); attr.setValue(dexFile.getPath()); dexNode.getAttributes().setNamedItem(attr); } return itemNode; }
From source file:de.brendamour.jpasskit.signing.PKSigningUtil.java
private static File createManifestJSONFile(final File tempPassDir, final ObjectMapper jsonObjectMapper) throws IOException, JsonGenerationException, JsonMappingException { Map<String, String> fileWithHashMap = new HashMap<String, String>(); HashFunction hashFunction = Hashing.sha1(); File[] filesInTempDir = tempPassDir.listFiles(); hashFilesInDirectory(filesInTempDir, fileWithHashMap, hashFunction, null); File manifestJSONFile = new File(tempPassDir.getAbsolutePath() + File.separator + MANIFEST_JSON_FILE_NAME); jsonObjectMapper.writeValue(manifestJSONFile, fileWithHashMap); return manifestJSONFile; }
From source file:org.apache.druid.metadata.IndexerSQLMetadataStorageCoordinator.java
@Nullable private SegmentIdentifier allocatePendingSegmentWithSegmentLineageCheck(final Handle handle, final String dataSource, final String sequenceName, @Nullable final String previousSegmentId, final Interval interval, final String maxVersion) throws IOException { final String previousSegmentIdNotNull = previousSegmentId == null ? "" : previousSegmentId; final CheckExistingSegmentIdResult result = checkAndGetExistingSegmentId( handle.createQuery(StringUtils.format( "SELECT payload FROM %s WHERE " + "dataSource = :dataSource AND " + "sequence_name = :sequence_name AND " + "sequence_prev_id = :sequence_prev_id", dbTables.getPendingSegmentsTable())), interval, sequenceName, previousSegmentIdNotNull, Pair.of("dataSource", dataSource), Pair.of("sequence_name", sequenceName), Pair.of("sequence_prev_id", previousSegmentIdNotNull)); if (result.found) { // The found existing segment identifier can be null if its interval doesn't match with the given interval return result.segmentIdentifier; }//from w ww. j a v a 2s. co m final SegmentIdentifier newIdentifier = createNewSegment(handle, dataSource, interval, maxVersion); if (newIdentifier == null) { return null; } // SELECT -> INSERT can fail due to races; callers must be prepared to retry. // Avoiding ON DUPLICATE KEY since it's not portable. // Avoiding try/catch since it may cause inadvertent transaction-splitting. // UNIQUE key for the row, ensuring sequences do not fork in two directions. // Using a single column instead of (sequence_name, sequence_prev_id) as some MySQL storage engines // have difficulty with large unique keys (see https://github.com/apache/incubator-druid/issues/2319) final String sequenceNamePrevIdSha1 = BaseEncoding.base16() .encode(Hashing.sha1().newHasher().putBytes(StringUtils.toUtf8(sequenceName)).putByte((byte) 0xff) .putBytes(StringUtils.toUtf8(previousSegmentIdNotNull)).hash().asBytes()); insertToMetastore(handle, newIdentifier, dataSource, interval, previousSegmentIdNotNull, sequenceName, sequenceNamePrevIdSha1); return newIdentifier; }
From source file:com.facebook.buck.parser.SerialDaemonicParserState.java
@SuppressWarnings({ "rawtypes", "unchecked" }) private TargetNode<?> createTargetNode(BuckEventBus eventBus, Cell cell, Path buildFile, BuildTarget target, Map<String, Object> rawNode, TargetNodeListener nodeListener) { 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); if (target.isFlavored()) { if (description instanceof Flavored) { if (!((Flavored) description).hasFlavors(ImmutableSet.copyOf(target.getFlavors()))) { throw new HumanReadableException("Unrecognized flavor in target %s while parsing %s%s.", target, UnflavoredBuildTarget.BUILD_TARGET_PREFIX, MorePaths .pathWithUnixSeparators(target.getBasePath().resolve(cell.getBuildFileName()))); }//from ww w . ja v a 2 s . co m } else { LOG.warn( "Target %s (type %s) must implement the Flavored interface " + "before we can check if it supports flavors: %s", target.getUnflavoredBuildTarget(), buildRuleType, target.getFlavors()); throw new HumanReadableException( "Target %s (type %s) does not currently support flavors (tried %s)", target.getUnflavoredBuildTarget(), buildRuleType, target.getFlavors()); } } Cell targetCell = cell.getCell(target); BuildRuleFactoryParams factoryParams = new BuildRuleFactoryParams(targetCell.getFilesystem(), target.withoutCell(), new FilesystemBackedBuildFileTree(cell.getFilesystem(), cell.getBuildFileName()), targetCell.isEnforcingBuckPackageBoundaries()); Object constructorArg = description.createUnpopulatedConstructorArg(); try { ImmutableSet.Builder<BuildTarget> declaredDeps = ImmutableSet.builder(); ImmutableSet.Builder<BuildTargetPattern> visibilityPatterns = ImmutableSet.builder(); try (SimplePerfEvent.Scope scope = SimplePerfEvent.scope(eventBus, PerfEventId.of("MarshalledConstructorArg"), "target", target)) { marshaller.populate(targetCell.getCellRoots(), targetCell.getFilesystem(), factoryParams, constructorArg, declaredDeps, visibilityPatterns, rawNode); } try (SimplePerfEvent.Scope scope = SimplePerfEvent.scope(eventBus, PerfEventId.of("CreatedTargetNode"), "target", target)) { Hasher hasher = Hashing.sha1().newHasher(); hasher.putString(BuckVersion.getVersion(), UTF_8); JsonObjectHashing.hashJsonObject(hasher, rawNode); targetsCornucopia.put(target.getUnflavoredBuildTarget(), target); TargetNode<?> node = new TargetNode(hasher.hash(), description, constructorArg, typeCoercerFactory, factoryParams, declaredDeps.build(), visibilityPatterns.build(), targetCell.getCellRoots()); nodeListener.onCreate(buildFile, node); return node; } } catch (NoSuchBuildTargetException | TargetNode.InvalidSourcePathInputException e) { throw new HumanReadableException(e); } catch (ConstructorArgMarshalException e) { throw new HumanReadableException("%s: %s", target, e.getMessage()); } catch (IOException e) { throw new HumanReadableException(e.getMessage(), e); } }
From source file:com.taobao.android.builder.tasks.app.databinding.AwbDataBindingMergeArtifactsTask.java
/** * Files exported from jars are exported into a certain folder so that we can rebuild them * when the related jar file changes.//from w ww . j a v a 2s . co m */ @NonNull private static String getJarFilePrefix(@NonNull File inputFile) { // get the filename String name = inputFile.getName(); // remove the extension int pos = name.lastIndexOf('.'); if (pos != -1) { name = name.substring(0, pos); } // add a hash of the original file path. String input = inputFile.getAbsolutePath(); HashFunction hashFunction = Hashing.sha1(); HashCode hashCode = hashFunction.hashString(input, Charsets.UTF_16LE); return name + "-" + hashCode.toString(); }
From source file:org.jooby.frontend.Frontend.java
private void onSyncPackageJson(Config conf, Path workDirectory, Throwing.Consumer<String> action) throws IOException { Path tmp = Paths.get(conf.getString("application.tmpdir"), "package.json"); Files.createDirectories(tmp); String sha1 = Hashing.sha1().hashBytes(Files.readAllBytes(workDirectory.resolve("package.json"))) .toString();/*from ww w . j a v a2s.co m*/ Path lastSha1 = tmp.resolve(sha1); if (!Files.exists(lastSha1) || !Files.exists(workDirectory.resolve("node_modules"))) { action.accept("install"); Files.walk(tmp).filter(f -> !f.equals(tmp)).forEach(throwingConsumer(Files::deleteIfExists)); Files.write(tmp.resolve(lastSha1), Arrays.asList("")); } }