diff options
Diffstat (limited to 'aesxx/include')
| -rw-r--r-- | aesxx/include/aesxx/box.hpp | 22 | ||||
| -rw-r--r-- | aesxx/include/aesxx/key.hpp | 49 |
2 files changed, 57 insertions, 14 deletions
diff --git a/aesxx/include/aesxx/box.hpp b/aesxx/include/aesxx/box.hpp index e625f38..aac2162 100644 --- a/aesxx/include/aesxx/box.hpp +++ b/aesxx/include/aesxx/box.hpp @@ -8,6 +8,7 @@ #include "algorithm.hpp" #include "block.hpp" #include "error.hpp" +#include "key.hpp" #include "mode.hpp" #include <aes/all.h> @@ -22,12 +23,6 @@ namespace aes { class Box { public: - using Key = AES_Key; - - static void parse_key(Key& dest, Algorithm algorithm, std::string_view src) { - aes_parse_key(algorithm, &dest, src.data(), ErrorDetailsThrowsInDestructor{}); - } - Box(Algorithm algorithm, const Key& key, Mode mode, @@ -35,7 +30,12 @@ public: bool verbose = false) : verbose{verbose} { aes_box_init( - &impl, algorithm, &key, mode, iv ? iv->ptr() : NULL, ErrorDetailsThrowsInDestructor{} + &impl, + algorithm, + key.ptr(), + mode, + iv ? iv->ptr() : NULL, + ErrorDetailsThrowsInDestructor{} ); dump_key(key); } @@ -113,15 +113,9 @@ public: } private: - static std::string format_key(const Key& src, Algorithm algorithm) { - AES_KeyString str; - aes_format_key(algorithm, &str, &src, ErrorDetailsThrowsInDestructor{}); - return reinterpret_cast<const char*>(&str); - } - void dump_key(const Key& src) const { if (verbose) - std::cout << std::format("Key : {}\n", format_key(src, get_algorithm())); + std::cout << std::format("Key : {}\n", src.to_string()); } Block get_iv() const { diff --git a/aesxx/include/aesxx/key.hpp b/aesxx/include/aesxx/key.hpp new file mode 100644 index 0000000..7036814 --- /dev/null +++ b/aesxx/include/aesxx/key.hpp @@ -0,0 +1,49 @@ +// Copyright (c) 2015 Egor Tensin <egor@tensin.name> +// This file is part of the "AES tools" project. +// For details, see https://github.com/egor-tensin/aes-tools. +// Distributed under the MIT License. + +#pragma once + +#include "algorithm.hpp" +#include "error.hpp" + +#include <aes/all.h> + +#include <string> +#include <string_view> + +namespace aes { + +class Key { +public: + using Impl = AES_Key; + + static Key parse(std::string_view src, Algorithm algorithm) { + Impl dest; + aes_parse_key(algorithm, &dest, src.data(), ErrorDetailsThrowsInDestructor{}); + return Key{dest, algorithm}; + } + + explicit Key(const Impl& impl, Algorithm algorithm) : impl{impl}, algorithm{algorithm} {} + + Impl* ptr() { + return &impl; + } + + const Impl* ptr() const { + return &impl; + } + + std::string to_string() const { + AES_KeyString str; + aes_format_key(algorithm, &str, &impl, ErrorDetailsThrowsInDestructor{}); + return reinterpret_cast<const char*>(&str); + } + +private: + Impl impl; + Algorithm algorithm; +}; + +} // namespace aes |
