xref: /aosp_15_r20/external/tink/cc/examples/util/util.cc (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
1 // Copyright 2022 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 //     http://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 ///////////////////////////////////////////////////////////////////////////////
16 #include "util/util.h"
17 
18 #include <fstream>
19 #include <iostream>
20 #include <memory>
21 #include <ostream>
22 #include <sstream>
23 #include <string>
24 #include <utility>
25 
26 #include "absl/memory/memory.h"
27 #include "tink/cleartext_keyset_handle.h"
28 #include "tink/json_keyset_reader.h"
29 #include "tink/json_keyset_writer.h"
30 #include "tink/keyset_handle.h"
31 #include "tink/keyset_reader.h"
32 #include "tink/keyset_writer.h"
33 #include "tink/util/status.h"
34 #include "tink/util/statusor.h"
35 
36 namespace tink_cc_examples {
37 namespace {
38 
39 using ::crypto::tink::JsonKeysetReader;
40 using ::crypto::tink::JsonKeysetWriter;
41 using ::crypto::tink::KeysetHandle;
42 using ::crypto::tink::KeysetReader;
43 using ::crypto::tink::util::Status;
44 using ::crypto::tink::util::StatusOr;
45 
46 // Creates a KeysetReader that reads a JSON-formatted keyset
47 // from the given file.
GetJsonKeysetReader(const std::string & filename)48 StatusOr<std::unique_ptr<KeysetReader>> GetJsonKeysetReader(
49     const std::string& filename) {
50   auto input_stream = absl::make_unique<std::ifstream>();
51   input_stream->open(filename, std::ifstream::in);
52   return JsonKeysetReader::New(std::move(input_stream));
53 }
54 
GetJsonKeysetWriter(const std::string & filename)55 StatusOr<std::unique_ptr<JsonKeysetWriter>> GetJsonKeysetWriter(
56     const std::string& filename) {
57   auto output_stream = absl::make_unique<std::ofstream>();
58   output_stream->open(filename, std::ofstream::out);
59   return JsonKeysetWriter::New(std::move(output_stream));
60 }
61 
62 }  // namespace
63 
ReadJsonCleartextKeyset(const std::string & filename)64 StatusOr<std::unique_ptr<KeysetHandle>> ReadJsonCleartextKeyset(
65     const std::string& filename) {
66   StatusOr<std::unique_ptr<KeysetReader>> keyset_reader =
67       GetJsonKeysetReader(filename);
68   if (!keyset_reader.ok()) return keyset_reader.status();
69   return crypto::tink::CleartextKeysetHandle::Read(*std::move(keyset_reader));
70 }
71 
WriteJsonCleartextKeyset(const std::string & filename,const KeysetHandle & keyset_handle)72 Status WriteJsonCleartextKeyset(const std::string& filename,
73                                 const KeysetHandle& keyset_handle) {
74   StatusOr<std::unique_ptr<JsonKeysetWriter>> keyset_writer =
75       GetJsonKeysetWriter(filename);
76   if (!keyset_writer.ok()) return keyset_writer.status();
77   return crypto::tink::CleartextKeysetHandle::Write(keyset_writer->get(),
78                                                     keyset_handle);
79 }
80 
ReadFile(const std::string & filename)81 StatusOr<std::string> ReadFile(const std::string& filename) {
82   std::ifstream input_stream;
83   input_stream.open(filename, std::ifstream::in);
84   if (!input_stream.is_open()) {
85     return Status(absl::StatusCode::kInternal,
86                   absl::StrCat("Error opening input file ", filename));
87   }
88   std::stringstream input;
89   input << input_stream.rdbuf();
90   return input.str();
91 }
92 
WriteToFile(const std::string & data_to_write,const std::string & filename)93 Status WriteToFile(const std::string& data_to_write,
94                    const std::string& filename) {
95   std::ofstream output_stream(filename,
96                               std::ofstream::out | std::ofstream::binary);
97   if (!output_stream.is_open()) {
98     return Status(absl::StatusCode::kInternal,
99                   absl::StrCat("Error opening output file ", filename));
100   }
101   output_stream << data_to_write;
102   return crypto::tink::util::OkStatus();
103 }
104 
105 }  // namespace tink_cc_examples
106