Skip to content

Commit

Permalink
Update C API examples
Browse files Browse the repository at this point in the history
  • Loading branch information
sorairolake committed Feb 1, 2024
1 parent 2f758c0 commit 66af808
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 36 deletions.
41 changes: 25 additions & 16 deletions crates/capi/examples/decrypt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <fstream>
#include <iostream>
#include <iterator>
#include <optional>
#include <string>
#include <vector>

Expand All @@ -25,11 +26,11 @@
int main(int argc, char *argv[]) {
CLI::App app{
"An example of decrypting from the abcrypt encrypted data format"};
std::optional<std::string> output_filename;
app.add_option("-o,--output", output_filename, "Output the result to a file");
app.set_version_flag("-V,--version", VERSION, "Print version");
std::string input_filename;
app.add_option("<INFILE>", input_filename, "Input file")->required();
std::string output_filename;
app.add_option("<OUTFILE>", output_filename, "Output file")->required();
app.add_option("FILE", input_filename, "Input file")->required();
CLI11_PARSE(app, argc, argv);

std::ifstream input_file(input_filename);
Expand Down Expand Up @@ -64,34 +65,42 @@ int main(int argc, char *argv[]) {
if (error_code != ABCRYPT_ERROR_CODE_OK) {
std::vector<std::uint8_t> buf(abcrypt_error_message_out_len(error_code));
abcrypt_error_message(error_code, buf.data(), buf.size());
std::string error_message(std::cbegin(buf), std::cend(buf));
std::string error_message(buf.cbegin(), buf.cend());
switch (error_code) {
case ABCRYPT_ERROR_CODE_INVALID_HEADER_MAC:
std::clog << fmt::format("Error: passphrase is incorrect: {}",
error_message)
<< std::endl;
break;
case ABCRYPT_ERROR_CODE_INVALID_MAC:
std::clog << fmt::format("Error: {} is corrupted: {}", input_filename,
std::clog << fmt::format("Error: the encrypted data is corrupted: {}",
error_message)
<< std::endl;
break;
default:
std::clog << fmt::format("Error: the header in {} is invalid: {}",
input_filename, error_message)
<< std::endl;
std::clog
<< fmt::format(
"Error: the header in the encrypted data is invalid: {}",
error_message)
<< std::endl;
break;
}
return EXIT_FAILURE;
}

std::ofstream output_file(output_filename);
if (!input_file) {
std::clog << fmt::format("Error: could not open {}: {}", output_filename,
std::strerror(errno))
<< std::endl;
return EXIT_FAILURE;
if (output_filename) {
auto ofn = output_filename.value();
std::ofstream output_file(ofn);
if (!output_file) {
std::clog << fmt::format("Error: could not open {}: {}", ofn,
std::strerror(errno))
<< std::endl;
return EXIT_FAILURE;
}
std::copy(plaintext.cbegin(), plaintext.cend(),
std::ostreambuf_iterator<char>(output_file));
} else {
std::copy(plaintext.cbegin(), plaintext.cend(),
std::ostreambuf_iterator<char>(std::cout));
}
std::ostreambuf_iterator<char> output_file_iter(output_file);
std::copy(std::cbegin(plaintext), std::cend(plaintext), output_file_iter);
}
12 changes: 6 additions & 6 deletions crates/capi/examples/encrypt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ int main(int argc, char *argv[]) {
->capture_default_str();
app.set_version_flag("-V,--version", VERSION, "Print version");
std::string input_filename;
app.add_option("<INFILE>", input_filename, "Input file")->required();
app.add_option("INFILE", input_filename, "Input file")->required();
std::string output_filename;
app.add_option("<OUTFILE>", output_filename, "Output file")->required();
app.add_option("OUTFILE", output_filename, "Output file")->required();
CLI11_PARSE(app, argc, argv);

std::ifstream input_file(input_filename);
Expand Down Expand Up @@ -83,18 +83,18 @@ int main(int argc, char *argv[]) {
if (error_code != ABCRYPT_ERROR_CODE_OK) {
std::vector<std::uint8_t> buf(abcrypt_error_message_out_len(error_code));
abcrypt_error_message(error_code, buf.data(), buf.size());
std::string error_message(std::cbegin(buf), std::cend(buf));
std::string error_message(buf.cbegin(), buf.cend());
std::clog << fmt::format("Error: {}", error_message) << std::endl;
return EXIT_FAILURE;
}

std::ofstream output_file(output_filename);
if (!input_file) {
if (!output_file) {
std::clog << fmt::format("Error: could not open {}: {}", output_filename,
std::strerror(errno))
<< std::endl;
return EXIT_FAILURE;
}
std::ostreambuf_iterator<char> output_file_iter(output_file);
std::copy(std::cbegin(ciphertext), std::cend(ciphertext), output_file_iter);
std::copy(ciphertext.cbegin(), ciphertext.cend(),
std::ostreambuf_iterator<char>(output_file));
}
35 changes: 21 additions & 14 deletions crates/capi/examples/info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <fstream>
#include <iostream>
#include <iterator>
#include <optional>
#include <string>
#include <vector>

Expand All @@ -23,31 +24,37 @@
int main(int argc, char *argv[]) {
CLI::App app{"An example of reading the Argon2 parameters"};
app.set_version_flag("-V,--version", VERSION, "Print version");
std::string input_filename;
app.add_option("<FILE>", input_filename, "Input file")->required();
std::optional<std::string> input_filename;
app.add_option("FILE", input_filename, "Input file");
CLI11_PARSE(app, argc, argv);

std::ifstream input_file(input_filename);
if (!input_file) {
std::clog << fmt::format("Error: could not open {}: {}", input_filename,
std::strerror(errno))
<< std::endl;
return EXIT_FAILURE;
std::vector<std::uint8_t> contents;
if (input_filename) {
auto ifn = input_filename.value();
std::ifstream input_file(ifn);
if (!input_file) {
std::clog << fmt::format("Error: could not open {}: {}", ifn,
std::strerror(errno))
<< std::endl;
return EXIT_FAILURE;
}
contents = {(std::istreambuf_iterator<char>(input_file)),
std::istreambuf_iterator<char>()};
} else {
contents = {(std::istreambuf_iterator<char>(std::cin)),
std::istreambuf_iterator<char>()};
}
std::vector<std::uint8_t> contents{
(std::istreambuf_iterator<char>(input_file)),
std::istreambuf_iterator<char>()};

auto params = abcrypt_params_new();
auto error_code =
abcrypt_params_read(contents.data(), contents.size(), params);
if (error_code != ABCRYPT_ERROR_CODE_OK) {
std::vector<std::uint8_t> buf(abcrypt_error_message_out_len(error_code));
abcrypt_error_message(error_code, buf.data(), buf.size());
std::string error_message(std::cbegin(buf), std::cend(buf));
std::string error_message(buf.cbegin(), buf.cend());
std::clog << fmt::format(
"Error: {} is not a valid Argon2 encrypted file: {}",
input_filename, error_message)
"Error: data is not a valid abcrypt encrypted file: {}",
error_message)
<< std::endl;
abcrypt_params_free(params);
return EXIT_FAILURE;
Expand Down
2 changes: 2 additions & 0 deletions crates/capi/examples/version.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@

#pragma once

namespace {
inline constexpr auto VERSION = "0.2.5";
}

0 comments on commit 66af808

Please sign in to comment.