public AssetBlob create(final InputStream inputStream, final Map<String, String> headers, final Iterable<HashAlgorithm> hashAlgorithms, final String contentType) { MultiHashingInputStream hashingStream = new MultiHashingInputStream(hashAlgorithms, inputStream); Blob streamedBlob = blobStore.create(hashingStream, headers); // pre-fetch to populate hashes return createPrefetchedAssetBlob( streamedBlob, hashingStream.hashes(), true, contentType); }
private String getHashCode(final QueryBuilder query) { return MD5.function().hashString(query.toString(), UTF_8).toString(); } }
private String calculateIntegrity(final Asset asset, final Blob blob, final String algorithm) { try { HashCode hash; if (algorithm.equalsIgnoreCase(SHA1.name())) { hash = hash(SHA1, blob.getInputStream()); } else { hash = hash(SHA512, blob.getInputStream()); } return algorithm + "-" + Base64.getEncoder().encodeToString(hash.asBytes()); } catch (IOException e) { log.error("Failed to calculate hash for asset {}", asset.name(), e); } return ""; } }
/** * Returns {@code true} when incoming hashes all match existing checksums. */ private boolean compareChecksums(final NestedAttributesMap checksums, final AssetBlob assetBlob) { for (Entry<HashAlgorithm, HashCode> entry : assetBlob.getHashes().entrySet()) { HashAlgorithm algorithm = entry.getKey(); HashCode checksum = entry.getValue(); if (!checksum.toString().equals(checksums.get(algorithm.name()))) { return false; } } return true; }
@TransactionalStoreMetadata protected void doCreateAssetFromBlob(final RestoreBlobData blobData, final T restoreData, final Blob blob) throws IOException { List<HashAlgorithm> hashTypes = getHashAlgorithms(); AssetBlob assetBlob = new AssetBlob(nodeAccess, blobStoreManager.get(blobData.getBlobStoreName()), blobStore -> blob, blobData.getProperty(HEADER_PREFIX + CONTENT_TYPE_HEADER), hash(hashTypes, blob.getInputStream()), true); createAssetFromBlob(assetBlob, restoreData); }
/** * Returns {@code true} when at least one incoming hash has the same algorithm as an existing checksum. */ private boolean checksumExists(final NestedAttributesMap checksums, final AssetBlob assetBlob) { if (!checksums.isEmpty()) { for (HashAlgorithm algorithm : assetBlob.getHashes().keySet()) { if (checksums.contains(algorithm.name())) { return true; } } } return false; }
@Override public void createIndex(final Repository repository) { checkNotNull(repository); final String safeIndexName = SHA1.function().hashUnencodedChars(repository.getName()).toString(); log.debug("Creating index for {}", repository); createIndex(repository, safeIndexName); }
@Override public TempBlob createTempBlob(final InputStream inputStream, final Iterable<HashAlgorithm> hashAlgorithms) { BlobStore blobStore = checkNotNull(blobStoreManager.get(config.blobStoreName)); MultiHashingInputStream hashingStream = new MultiHashingInputStream(hashAlgorithms, inputStream); Blob blob = blobStore.create(hashingStream, ImmutableMap.of( BlobStore.BLOB_NAME_HEADER, "temp", BlobStore.CREATED_BY_HEADER, createdBy(), BlobStore.CREATED_BY_IP_HEADER, createdByIp(), BlobStore.TEMPORARY_BLOB_HEADER, "")); return new TempBlob(blob, hashingStream.hashes(), true, blobStore); }
/** * Extract checksum of asset blob if checksum is present in asset attributes. */ @Nullable public HashCode getChecksum(final HashAlgorithm hashAlgorithm) { String hashCode = attributes().child(CHECKSUM).get(hashAlgorithm.name(), String.class); if (hashCode != null) { return HashCode.fromString(hashCode); } return null; }
OutputStream os = outputStream; for (HashType hashType : HashType.values()) { os = new HashingOutputStream(hashType.getHashAlgorithm().function(), os); hashingStreams.put(hashType.getHashAlgorithm(), (HashingOutputStream) os);
/** * Extract checksums of asset blob if checksums are present in asset attributes. */ public Map<HashAlgorithm, HashCode> getChecksums(final Iterable<HashAlgorithm> hashAlgorithms) { final NestedAttributesMap checksumAttributes = attributes().child(CHECKSUM); final Map<HashAlgorithm, HashCode> hashCodes = Maps.newHashMap(); for (HashAlgorithm algorithm : hashAlgorithms) { final HashCode hashCode = HashCode.fromString(checksumAttributes.require(algorithm.name(), String.class)); hashCodes.put(algorithm, hashCode); } return hashCodes; }
HashAlgorithm algorithm = entry.getKey(); HashCode checksum = entry.getValue(); checksums.set(algorithm.name(), checksum.toString());