Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,18 @@ public interface IPodLogEncryptionProvider

public class PodLogEncryptionProvider : IPodLogEncryptionProvider
{
readonly byte[] keyBytes;
readonly KeyParameter keyParameter;
readonly GcmBlockCipher cipher;
readonly int macSize;
const int NonceLength = 12;

private PodLogEncryptionProvider(byte[] keyBytes)
{
this.keyBytes = keyBytes;
keyParameter = new KeyParameter(keyBytes);
cipher = new GcmBlockCipher(new AesEngine());
macSize = 8 * cipher.GetBlockSize();
}

public static IPodLogEncryptionProvider Create(byte[] keyBytes) => new PodLogEncryptionProvider(keyBytes);

public string Decrypt(string encryptedLogMessage)
Expand All @@ -33,9 +37,7 @@ public string Decrypt(string encryptedLogMessage)
var nonceSpan = allEncryptedBytes.Slice(0, NonceLength);
var logMessageBytes = allEncryptedBytes.Slice(NonceLength);

var cipher = new GcmBlockCipher(new AesEngine());
var macSize = 8 * cipher.GetBlockSize();
cipher.Init(false, new AeadParameters(new KeyParameter(keyBytes), macSize, nonceSpan.ToArray()));
cipher.Init(false, new AeadParameters(keyParameter, macSize, nonceSpan.ToArray()));

var outputSize = cipher.GetOutputSize(logMessageBytes.Length);
var plainTextData = new byte[outputSize];
Expand All @@ -53,10 +55,7 @@ public string Encrypt(string plainText, byte[]? nonce = null)
//if no nonce is provided, generate one
nonce ??= GenerateNonce();

var cipher = new GcmBlockCipher(new AesEngine());
var macSize = 8 * cipher.GetBlockSize();
var parameters = new AeadParameters(new KeyParameter(keyBytes), macSize, nonce, null);
cipher.Init(true, parameters);
cipher.Init(true, new AeadParameters(keyParameter, macSize, nonce, null));

var cipherText = new byte[cipher.GetOutputSize(plainTextBytes.Length)];
var len = cipher.ProcessBytes(plainTextBytes, 0, plainTextBytes.Length, cipherText, 0);
Expand Down