| 62 | std::string EncryptionType::separator() { return "||"; } |
| 63 | |
| 64 | Bytes encryptRaw(const Bytes& plaintext, const Bytes& key, const Bytes& nonce) { |
| 65 | if (key.size() != EncryptionType::keyLength()) { |
| 66 | throw std::invalid_argument{"Expected key of " + std::to_string(EncryptionType::keyLength()) + |
| 67 | " bytes, but got " + std::to_string(key.size()) + " bytes during encryption"}; |
| 68 | } |
| 69 | if (nonce.size() != EncryptionType::nonceLength()) { |
| 70 | throw std::invalid_argument{"Expected nonce of " + std::to_string(EncryptionType::nonceLength()) + |
| 71 | " bytes, but got " + std::to_string(nonce.size()) + " bytes during encryption"}; |
| 72 | } |
| 73 | |
| 74 | Bytes ciphertext_plus_mac(plaintext.size() + EncryptionType::macLength()); |
| 75 | crypto_secretbox_easy(ciphertext_plus_mac.data(), plaintext.data(), plaintext.size(), nonce.data(), key.data()); |
| 76 | return ciphertext_plus_mac; |
| 77 | } |
| 78 | |
| 79 | std::string encrypt(const std::string& plaintext, const Bytes& key) { |
| 80 | Bytes nonce = randomBytes(EncryptionType::nonceLength()); |
no test coverage detected