From ca213ea7760160881aa61d6de43ef4f34a3fdb47 Mon Sep 17 00:00:00 2001 From: Emil Lerch Date: Thu, 1 Feb 2024 08:41:16 -0800 Subject: [PATCH] add decodeAndDecrypt function (inverse of encryptAndEncode) --- src/encryption.zig | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/encryption.zig b/src/encryption.zig index 648a538..77216b4 100644 --- a/src/encryption.zig +++ b/src/encryption.zig @@ -118,6 +118,17 @@ pub fn decrypt(allocator: std.mem.Allocator, key: [key_length]u8, ciphertext: [] return plaintext; } +/// Decrypts encoded data. Does the reverse of encryptAndEncode +/// Caller owns memory +pub fn decodeAndDecrypt(allocator: std.mem.Allocator, key: [key_length]u8, encoded_ciphertext: []const u8) ![]const u8 { + const Decoder = std.base64.standard.Decoder; + const ciphertext_len = try Decoder.calcSizeForSlice(encoded_ciphertext); + var ciphertext = try allocator.alloc(u8, ciphertext_len); + defer allocator.free(ciphertext); + try std.base64.standard.Decoder.decode(ciphertext, encoded_ciphertext); + return try decrypt(allocator, key, ciphertext); +} + // This is a pretty long running test... // test "can encrypt and decrypt data with simpler api" { // const allocator = std.testing.allocator;