Google Cloud Storage C++ Client 2.13.0
A C++ Client Library for Google Cloud Storage
Loading...
Searching...
No Matches
hashing_options.h
1// Copyright 2018 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_HASHING_OPTIONS_H
16#define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_HASHING_OPTIONS_H
17
18#include "google/cloud/storage/internal/complex_option.h"
19#include "google/cloud/storage/version.h"
20#include "absl/strings/string_view.h"
21#include <string>
22
23namespace google {
24namespace cloud {
25namespace storage {
26GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
27/**
28 * Provide a pre-computed MD5 hash value.
29 *
30 * The application may be able to obtain a MD5 hash in some out-of-band way.
31 * For example, if the object was downloaded from some other cloud storage
32 * service, or because the application already queried the GCS object metadata.
33 * In these cases, providing the value to the client library improves the
34 * end-to-end data integrity verification.
35 *
36 * @see
37 * https://sigops.org/s/conferences/hotos/2021/papers/hotos21-s01-hochschild.pdf
38 */
39struct MD5HashValue
40 : public internal::ComplexOption<MD5HashValue, std::string> {
41 using ComplexOption<MD5HashValue, std::string>::ComplexOption;
42 // GCC <= 7.0 does not use the inherited default constructor, redeclare it
43 // explicitly
44 MD5HashValue() = default;
45 static char const* name() { return "md5-hash-value"; }
46};
47
48/**
49 * Compute the MD5 Hash of a buffer in the format preferred by GCS.
50 */
51std::string ComputeMD5Hash(absl::string_view payload);
52
53/// @copydoc ComputeMD5Hash(absl::string_view)
54std::string ComputeMD5Hash(std::string const& payload);
55
56/// @copydoc ComputeMD5Hash(absl::string_view)
57inline std::string ComputeMD5Hash(char const* payload) {
58 return ComputeMD5Hash(payload == nullptr ? absl::string_view{}
59 : absl::string_view{payload});
60}
61
62/**
63 * Disable or enable MD5 Hashing computations.
64 *
65 * By default MD5 hashes are disabled. To enable them use the
66 * `EnableMD5Hash()` helper function.
67 *
68 * @warning MD5 hashes are disabled by default, as they are computationally
69 * expensive, and CRC32C checksums provide enough data integrity protection
70 * for most applications. Disabling CRC32C checksums while MD5 hashes remain
71 * disabled exposes your application to data corruption. We recommend that all
72 * uploads to GCS and downloads from GCS use CRC32C checksums.
73 */
74struct DisableMD5Hash : public internal::ComplexOption<DisableMD5Hash, bool> {
75 using ComplexOption<DisableMD5Hash, bool>::ComplexOption;
76 // GCC <= 7.0 does not use the inherited default constructor, redeclare it
77 // explicitly
79 static char const* name() { return "disable-md5-hash"; }
80};
81
82/**
83 * Enable MD5 hashes in upload and download operations.
84 *
85 * Use this function where the option `DisableMD5Hash` is expected to enable MD5
86 * hashes.
87 */
88inline DisableMD5Hash EnableMD5Hash() { return DisableMD5Hash(false); }
89
90/**
91 * Provide a pre-computed CRC32C checksum value.
92 *
93 * The application may be able to obtain a CRC32C checksum in some out-of-band
94 * way. For example, if the object was downloaded from some other cloud storage
95 * service, or because the application already queried the GCS object metadata.
96 * In these cases, providing the value to the client library improves the
97 * end-to-end data integrity verification.
98 *
99 * @see
100 * https://sigops.org/s/conferences/hotos/2021/papers/hotos21-s01-hochschild.pdf
101 */
103 : public internal::ComplexOption<Crc32cChecksumValue, std::string> {
104 using ComplexOption<Crc32cChecksumValue, std::string>::ComplexOption;
105 // GCC <= 7.0 does not use the inherited default constructor, redeclare it
106 // explicitly
107 Crc32cChecksumValue() = default;
108 static char const* name() { return "crc32c-checksum"; }
109};
110
111/**
112 * Compute the CRC32C checksum of a buffer in the format preferred by GCS.
113 */
114std::string ComputeCrc32cChecksum(absl::string_view payload);
115
116/// @copydoc ComputeCrc32cChecksum(absl::string_view payload)
117std::string ComputeCrc32cChecksum(std::string const& payload);
118
119/// @copydoc ComputeCrc32cChecksum(absl::string_view payload)
120inline std::string ComputeCrc32cChecksum(char const* payload) {
121 return ComputeCrc32cChecksum(payload == nullptr ? absl::string_view{}
122 : absl::string_view{payload});
123}
124
125/**
126 * Disable CRC32C checksum computations.
127 *
128 * By default the GCS client library computes CRC32C checksums in all upload and
129 * download operations. The application can use this option to disable the
130 * checksum computation.
131 *
132 * @warning MD5 hashes are disabled by default, as they are computationally
133 * expensive, and CRC32C checksums provide enough data integrity protection
134 * for most applications. Disabling CRC32C checksums while MD5 hashes remain
135 * disabled exposes your application to data corruption. We recommend that all
136 * uploads to GCS and downloads from GCS use CRC32C checksums.
137 */
139 : public internal::ComplexOption<DisableCrc32cChecksum, bool> {
140 using ComplexOption<DisableCrc32cChecksum, bool>::ComplexOption;
141 // GCC <= 7.0 does not use the inherited default constructor, redeclare it
142 // explicitly
143 DisableCrc32cChecksum() = default;
144 static char const* name() { return "disable-crc32c-checksum"; }
145};
146
147GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
148} // namespace storage
149} // namespace cloud
150} // namespace google
151
152#endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_STORAGE_HASHING_OPTIONS_H
Contains all the Google Cloud Storage C++ client APIs.
Definition: auto_finalize.h:24
std::string ComputeCrc32cChecksum(std::string const &payload)
Compute the CRC32C checksum of a buffer in the format preferred by GCS.
std::string ComputeMD5Hash(absl::string_view payload)
Compute the MD5 Hash of a buffer in the format preferred by GCS.
std::string ComputeCrc32cChecksum(absl::string_view payload)
Compute the CRC32C checksum of a buffer in the format preferred by GCS.
std::string ComputeMD5Hash(char const *payload)
Compute the MD5 Hash of a buffer in the format preferred by GCS.
Definition: hashing_options.h:57
DisableMD5Hash EnableMD5Hash()
Enable MD5 hashes in upload and download operations.
Definition: hashing_options.h:88
std::string ComputeMD5Hash(std::string const &payload)
Compute the MD5 Hash of a buffer in the format preferred by GCS.
std::string ComputeCrc32cChecksum(char const *payload)
Compute the CRC32C checksum of a buffer in the format preferred by GCS.
Definition: hashing_options.h:120
Provide a pre-computed CRC32C checksum value.
Definition: hashing_options.h:103
static char const * name()
Definition: hashing_options.h:108
Disable CRC32C checksum computations.
Definition: hashing_options.h:139
static char const * name()
Definition: hashing_options.h:144
Disable or enable MD5 Hashing computations.
Definition: hashing_options.h:74
static char const * name()
Definition: hashing_options.h:79
DisableMD5Hash()
Definition: hashing_options.h:78
Provide a pre-computed MD5 hash value.
Definition: hashing_options.h:40
static char const * name()
Definition: hashing_options.h:45