Changeset View
Changeset View
Standalone View
Standalone View
src/examples/key-export-example.cpp
- This file was added.
| /* | |||||
| * This file is part of libkazv. | |||||
| * SPDX-FileCopyrightText: 2025 tusooa <tusooa@kazv.moe> | |||||
| * SPDX-License-Identifier: AGPL-3.0-or-later | |||||
| */ | |||||
| #include <libkazv-config.hpp> | |||||
| #include "key-export.hpp" | |||||
| #include <iostream> | |||||
| #include <fstream> | |||||
| using namespace Kazv; | |||||
| std::string readWhole(std::ifstream &stream) | |||||
| { | |||||
| // https://en.cppreference.com/w/cpp/io/basic_istream/read.html | |||||
| auto size = stream.tellg(); | |||||
| std::string res(size, '\0'); | |||||
| stream.seekg(0); | |||||
| stream.read(res.data(), size); | |||||
| return res; | |||||
| } | |||||
| int main(int argc, char *argv[]) | |||||
| { | |||||
| if (argc < 2) { | |||||
| std::cerr << "Usage: " << argv[0] << " FILE" << std::endl; | |||||
| return 1; | |||||
| } | |||||
| auto stream = std::ifstream(argv[1], std::ios_base::ate | std::ios_base::binary); | |||||
| if (!stream) { | |||||
| std::cerr << "Cannot open file for reading." << std::endl; | |||||
| return 1; | |||||
| } | |||||
| auto content = readWhole(stream); | |||||
| std::cout << "Enter password:" << std::endl; | |||||
| auto password = std::string(); | |||||
| std::getline(std::cin, password); | |||||
| auto decrypted = decryptKeyExport(content, password); | |||||
| if (decrypted.has_value()) { | |||||
| std::cout << decrypted.value().dump() << std::endl; | |||||
| return 0; | |||||
| } else { | |||||
| std::cerr << "Error: " << decrypted.reason() << std::endl; | |||||
| return 1; | |||||
| } | |||||
| } | |||||