| 19 | } |
| 20 | |
| 21 | bool DataEncryptor::encrypt(const Byte* data, size_t length, ByteBuffer& out) const { |
| 22 | snap::utils::crypto::AesEncryptor::Iv iv; |
| 23 | RAND_bytes(reinterpret_cast<uint8_t*>(iv.data()), iv.size()); |
| 24 | |
| 25 | snap::utils::crypto::AesEncryptor encryptor(_key, iv); |
| 26 | |
| 27 | auto encryptResult = encryptor.encrypt(toSpan(data, length)); |
| 28 | if (!encryptResult) { |
| 29 | return false; |
| 30 | } |
| 31 | |
| 32 | // Insert the IV at the front |
| 33 | out.append(iv.begin(), iv.end()); |
| 34 | |
| 35 | // Append the data after |
| 36 | out.append(encryptResult.value().data(), encryptResult.value().data() + encryptResult.value().size()); |
| 37 | |
| 38 | return true; |
| 39 | } |
| 40 | |
| 41 | bool DataEncryptor::decrypt(const Byte* data, size_t length, ByteBuffer& out) const { |
| 42 | snap::utils::crypto::AesEncryptor::Iv iv; |