|
| 1 | +/* |
| 2 | + * Copyright 2026 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.cloud.storage; |
| 18 | + |
| 19 | +import com.google.api.client.http.HttpResponse; |
| 20 | +import com.google.api.core.InternalApi; |
| 21 | +import com.google.common.io.BaseEncoding; |
| 22 | +import com.google.common.primitives.Ints; |
| 23 | +import java.io.IOException; |
| 24 | +import java.io.OutputStream; |
| 25 | +import java.nio.ByteBuffer; |
| 26 | +import java.util.List; |
| 27 | +import java.util.Map; |
| 28 | +import java.util.function.Supplier; |
| 29 | + |
| 30 | +/** |
| 31 | + * Internal utility class to perform client-side CRC32C checksum validation on downloaded data |
| 32 | + * specifically for the {@code HttpStorageRpc} transport layer. |
| 33 | + * |
| 34 | + * <p>Since this class resides in the {@code com.google.cloud.storage} package, it has full, |
| 35 | + * package-private compile-time access to internal components (like {@link Hasher} and {@link |
| 36 | + * Crc32cValue}) without leaking GCS internal types into public client API surfaces. |
| 37 | + */ |
| 38 | +@InternalApi |
| 39 | +public final class HttpStorageRpcHasherHelper { |
| 40 | + |
| 41 | + public static final HttpStorageRpcHasherHelper INSTANCE = new HttpStorageRpcHasherHelper(); |
| 42 | + |
| 43 | + private final Hasher hasher; |
| 44 | + |
| 45 | + private HttpStorageRpcHasherHelper() { |
| 46 | + hasher = Hasher.defaultHasher(); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Returns a wrapping output stream that hashes the written content if validation is enabled, or |
| 51 | + * the original output stream otherwise. |
| 52 | + */ |
| 53 | + public OutputStream wrap(OutputStream out, boolean isChecksumValidationEnabled) { |
| 54 | + boolean isHasherEnabled = !(hasher instanceof Hasher.NoOpHasher); |
| 55 | + return (isChecksumValidationEnabled && isHasherEnabled) |
| 56 | + ? new Crc32cHashingOutputStream(out) |
| 57 | + : out; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Validates a raw byte array against GCS's expected base64-encoded value in response headers. |
| 62 | + * |
| 63 | + * @throws IOException if the checksums do not match. |
| 64 | + */ |
| 65 | + public void validate(HttpResponse response, byte[] content) throws IOException { |
| 66 | + Map<String, String> hashes = extractHashesFromHeader(response); |
| 67 | + String expectedCrc32cBase64 = hashes.get("crc32c"); |
| 68 | + if (expectedCrc32cBase64 != null) { |
| 69 | + validateCrc32c(expectedCrc32cBase64, content); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Validates the downloaded output stream against GCS's expected base64-encoded value in response |
| 75 | + * headers. |
| 76 | + * |
| 77 | + * @throws IOException if the checksums do not match. |
| 78 | + */ |
| 79 | + public void validate(HttpResponse response, OutputStream activeStream) throws IOException { |
| 80 | + if (activeStream instanceof Crc32cHashingOutputStream) { |
| 81 | + Crc32cHashingOutputStream targetStream = (Crc32cHashingOutputStream) activeStream; |
| 82 | + Map<String, String> hashes = extractHashesFromHeader(response); |
| 83 | + String expectedCrc32cBase64 = hashes.get("crc32c"); |
| 84 | + if (expectedCrc32cBase64 != null) { |
| 85 | + validateCrc32c(expectedCrc32cBase64, targetStream.hash()); |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Validates a calculated CRC32C value against GCS's expected base64-encoded value. |
| 92 | + * |
| 93 | + * @throws IOException if the checksums do not match. |
| 94 | + */ |
| 95 | + public void validateCrc32c(String expectedCrc32cBase64, int calculatedCrc32c) throws IOException { |
| 96 | + if (expectedCrc32cBase64 == null) { |
| 97 | + return; |
| 98 | + } |
| 99 | + byte[] decoded = BaseEncoding.base64().decode(expectedCrc32cBase64); |
| 100 | + int expectedVal = Ints.fromByteArray(decoded); |
| 101 | + |
| 102 | + Crc32cValue<?> expected = Crc32cValue.of(expectedVal, 0); |
| 103 | + Crc32cValue.Crc32cLengthKnown actual = Crc32cValue.of(calculatedCrc32c, 0); |
| 104 | + |
| 105 | + // Invoke standard package-private validate path natively |
| 106 | + System.out.println("validating checksum"); |
| 107 | + hasher.validate(expected, actual); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Validates a downloaded raw byte array against GCS's expected base64-encoded value. |
| 112 | + * |
| 113 | + * @throws IOException if the checksums do not match. |
| 114 | + */ |
| 115 | + public void validateCrc32c(String expectedCrc32cBase64, byte[] content) throws IOException { |
| 116 | + if (expectedCrc32cBase64 == null) { |
| 117 | + return; |
| 118 | + } |
| 119 | + byte[] decoded = BaseEncoding.base64().decode(expectedCrc32cBase64); |
| 120 | + int expectedVal = Ints.fromByteArray(decoded); |
| 121 | + |
| 122 | + Crc32cValue<?> expected = Crc32cValue.of(expectedVal, 0); |
| 123 | + System.out.println("validating checksum"); |
| 124 | + hasher.validate( |
| 125 | + expected, |
| 126 | + new Supplier<ByteBuffer>() { |
| 127 | + @Override |
| 128 | + public ByteBuffer get() { |
| 129 | + return ByteBuffer.wrap(content); |
| 130 | + } |
| 131 | + }); |
| 132 | + } |
| 133 | + |
| 134 | + @SuppressWarnings("UnstableApiUsage") |
| 135 | + private static class Crc32cHashingOutputStream extends java.io.FilterOutputStream { |
| 136 | + private final com.google.common.hash.Hasher hasher; |
| 137 | + |
| 138 | + Crc32cHashingOutputStream(OutputStream out) { |
| 139 | + super(out); |
| 140 | + this.hasher = com.google.common.hash.Hashing.crc32c().newHasher(); |
| 141 | + } |
| 142 | + |
| 143 | + @Override |
| 144 | + public void write(int b) throws IOException { |
| 145 | + out.write(b); |
| 146 | + hasher.putByte((byte) b); |
| 147 | + } |
| 148 | + |
| 149 | + @Override |
| 150 | + public void write(byte[] b, int off, int len) throws IOException { |
| 151 | + out.write(b, off, len); |
| 152 | + hasher.putBytes(b, off, len); |
| 153 | + } |
| 154 | + |
| 155 | + int hash() { |
| 156 | + return hasher.hash().asInt(); |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + private static Map<String, String> extractHashesFromHeader(HttpResponse response) { |
| 161 | + List<String> hashHeaders = response.getHeaders().getHeaderStringValues("x-goog-hash"); |
| 162 | + if (hashHeaders == null || hashHeaders.isEmpty()) { |
| 163 | + return java.util.Collections.emptyMap(); |
| 164 | + } |
| 165 | + |
| 166 | + return hashHeaders.stream() |
| 167 | + .flatMap(h -> java.util.Arrays.stream(h.split(","))) |
| 168 | + .map(String::trim) |
| 169 | + .filter(s -> !s.isEmpty()) |
| 170 | + .map(s -> s.split("=", 2)) |
| 171 | + .filter(a -> a.length == 2) |
| 172 | + .filter(a -> "crc32c".equalsIgnoreCase(a[0]) || "md5".equalsIgnoreCase(a[0])) |
| 173 | + .collect( |
| 174 | + java.util.stream.Collectors.toMap(a -> a[0].toLowerCase(), a -> a[1], (v1, v2) -> v1)); |
| 175 | + } |
| 176 | +} |
0 commit comments