xref: /aosp_15_r20/external/tink/cc/examples/util/util.h (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
1*e7b1675dSTing-Kang Chang // Copyright 2022 Google LLC
2*e7b1675dSTing-Kang Chang //
3*e7b1675dSTing-Kang Chang // Licensed under the Apache License, Version 2.0 (the "License");
4*e7b1675dSTing-Kang Chang // you may not use this file except in compliance with the License.
5*e7b1675dSTing-Kang Chang // You may obtain a copy of the License at
6*e7b1675dSTing-Kang Chang //
7*e7b1675dSTing-Kang Chang //     http://www.apache.org/licenses/LICENSE-2.0
8*e7b1675dSTing-Kang Chang //
9*e7b1675dSTing-Kang Chang // Unless required by applicable law or agreed to in writing, software
10*e7b1675dSTing-Kang Chang // distributed under the License is distributed on an "AS IS" BASIS,
11*e7b1675dSTing-Kang Chang // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*e7b1675dSTing-Kang Chang // See the License for the specific language governing permissions and
13*e7b1675dSTing-Kang Chang // limitations under the License.
14*e7b1675dSTing-Kang Chang //
15*e7b1675dSTing-Kang Chang ///////////////////////////////////////////////////////////////////////////////
16*e7b1675dSTing-Kang Chang #ifndef TINK_EXAMPLES_UTIL_UTIL_H_
17*e7b1675dSTing-Kang Chang #define TINK_EXAMPLES_UTIL_UTIL_H_
18*e7b1675dSTing-Kang Chang 
19*e7b1675dSTing-Kang Chang #include <memory>
20*e7b1675dSTing-Kang Chang #include <string>
21*e7b1675dSTing-Kang Chang 
22*e7b1675dSTing-Kang Chang #include "tink/keyset_handle.h"
23*e7b1675dSTing-Kang Chang #include "tink/util/status.h"
24*e7b1675dSTing-Kang Chang #include "tink/util/statusor.h"
25*e7b1675dSTing-Kang Chang 
26*e7b1675dSTing-Kang Chang namespace tink_cc_examples {
27*e7b1675dSTing-Kang Chang 
28*e7b1675dSTing-Kang Chang // Reads a keyset from the given file `filename` which is expected to contain a
29*e7b1675dSTing-Kang Chang // JSON-formatted keyset.
30*e7b1675dSTing-Kang Chang crypto::tink::util::StatusOr<std::unique_ptr<crypto::tink::KeysetHandle>>
31*e7b1675dSTing-Kang Chang ReadJsonCleartextKeyset(const std::string& filename);
32*e7b1675dSTing-Kang Chang 
33*e7b1675dSTing-Kang Chang // Writes `keyset_handle` to the file `filename` formatted with JSON in
34*e7b1675dSTing-Kang Chang // cleartext.
35*e7b1675dSTing-Kang Chang crypto::tink::util::Status WriteJsonCleartextKeyset(
36*e7b1675dSTing-Kang Chang     const std::string& filename,
37*e7b1675dSTing-Kang Chang     const crypto::tink::KeysetHandle& keyset_handle);
38*e7b1675dSTing-Kang Chang 
39*e7b1675dSTing-Kang Chang // Reads `filename` and returns the read content as a string, or an error status
40*e7b1675dSTing-Kang Chang // if the file does not exist.
41*e7b1675dSTing-Kang Chang crypto::tink::util::StatusOr<std::string> ReadFile(const std::string& filename);
42*e7b1675dSTing-Kang Chang 
43*e7b1675dSTing-Kang Chang // Writes the given `data_to_write` to the specified file `filename`.
44*e7b1675dSTing-Kang Chang crypto::tink::util::Status WriteToFile(const std::string& data_to_write,
45*e7b1675dSTing-Kang Chang                                        const std::string& filename);
46*e7b1675dSTing-Kang Chang 
47*e7b1675dSTing-Kang Chang }  // namespace tink_cc_examples
48*e7b1675dSTing-Kang Chang 
49*e7b1675dSTing-Kang Chang #endif  // TINK_EXAMPLES_UTIL_UTIL_H_
50