Enigma  3.2.0
A Simple, Reliable and Efficient Encryption Tool
Meta.hpp
Go to the documentation of this file.
1 #pragma once
3 #include <Core/Core.hpp>
4 #include <Logger/Logger.hpp>
5 #include <Utility/SizeUtils.hpp>
6 #include "Endianness.hpp"
7 #include <bit>
8 #include <fstream>
9 #include <iostream>
10 #include <sha.h>
11 //#include <sha3.h>
12 
19 class Meta final {
20  ENIGMA_STATIC_CLASS(Meta);
21 
22  public:
23  using magic_t = std::uint64_t;
24  using size_type = std::size_t;
25  static constexpr const magic_t ENIGMA_MAGIC = 0x456e69676d61;
26  static constexpr const magic_t ENIGMA_CIPHER_CHUNK_MAGIC = 0x43456e69676d6143;
27  static constexpr const size_type ENIGMA_BUFFER_DEFAULT_SIZE = ENIGMA_KB_TO_BYTES(64); // 64KB chunk size is reasonable for memory usage and efficiency.
28 
29 #pragma pack(push, 1)
31  struct EnigmaFooter {
35  std::uint64_t version = ENIGMA_VERSION_MAJOR * 100'000 + ENIGMA_VERSION_MINOR * 1000 + ENIGMA_VERSION_PATCH;
39  std::array<byte, CryptoPP::SHA256::DIGESTSIZE> hash{};
41  std::vector<byte> iv{};
43  std::vector<byte> extra{};
44 
45 
46  [[nodiscard]] size_type sizeInBytes() const noexcept;
47 
49  [[nodiscard]] std::vector<byte> toBytes() const;
50 
51  [[nodiscard]] static EnigmaFooter fromBytes(const std::vector<byte>& bytes);
52  [[nodiscard]] static EnigmaFooter fromBytes(const byte *bytes, const std::size_t bytesSize);
53  [[nodiscard]] static EnigmaFooter fromBase64(const std::string& base64);
54  [[nodiscard]] static EnigmaFooter fromFile(const fs::path& filename);
55  };
56 
59  std::vector<byte> cipher;
60  std::vector<byte> extra;
61 
62  [[nodiscard]] size_type sizeInBytes() const noexcept;
63 
64  [[nodiscard]] std::vector<byte> toBytes() const;
65 
66  // readCipherChunks() will read this
67  };
68 #pragma pack(pop)
69 
70  // Sanity checks
71  // Ensure that size of magic_t is same on all platforms,
72  // so if you encrypt in Windows, you can decrypt in Linux..
73  static_assert(std::endian::native == std::endian::little, "Host must be little endian");
74  static_assert(sizeof(magic_t) == 8, "Magic type must be of 8 bytes");
75  static_assert(sizeof(size_type) == 8, "Size type must be of 8 bytes");
76 
78  static bool isEnigmaFile(const fs::path& filename);
79 
81  static bool isEnigmaCipher(const byte *cipher, const std::size_t cipherSize);
82  static bool isEnigmaCipher(const std::vector<byte>& cipher);
83 
85  static void readCipherChunks(const fs::path& filename, const std::function<bool(EnigmaCipherChunk&&)>& callback);
86 };
87 
#define NS_ENIGMA_BEGIN
Enable/Disable Assertions.
Definition: Macros.hpp:13
#define NS_ENIGMA_END
Definition: Macros.hpp:14
#define ENIGMA_KB_TO_BYTES(kb)
Definition: SizeUtils.hpp:51
Definition: Meta.hpp:19
static bool isEnigmaCipher(const std::vector< byte > &cipher)
std::size_t size_type
Definition: Meta.hpp:24
static bool isEnigmaCipher(const byte *cipher, const std::size_t cipherSize)
Returns true if the file is encrypted with enigma.
static void readCipherChunks(const fs::path &filename, const std::function< bool(EnigmaCipherChunk &&)> &callback)
Read cipher chunks from an Enigma encrypted file.
static constexpr const size_type ENIGMA_BUFFER_DEFAULT_SIZE
Definition: Meta.hpp:27
static constexpr const magic_t ENIGMA_CIPHER_CHUNK_MAGIC
Definition: Meta.hpp:26
std::uint64_t magic_t
Definition: Meta.hpp:23
static constexpr const magic_t ENIGMA_MAGIC
Definition: Meta.hpp:25
static bool isEnigmaFile(const fs::path &filename)
Returns true if the file is encrypted with enigma.
size_type sizeInBytes() const noexcept
std::vector< byte > extra
Definition: Meta.hpp:60
std::vector< byte > cipher
Definition: Meta.hpp:59