From 08118820fead0949657925456e9ef56efd3500f6 Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Mon, 16 Jan 2023 18:32:47 +0700 Subject: Avoid allocations --- crypto/src/crypto/engines/ElGamalEngine.cs | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/crypto/src/crypto/engines/ElGamalEngine.cs b/crypto/src/crypto/engines/ElGamalEngine.cs index 2e80302a6..8903f495e 100644 --- a/crypto/src/crypto/engines/ElGamalEngine.cs +++ b/crypto/src/crypto/engines/ElGamalEngine.cs @@ -3,6 +3,7 @@ using System; using Org.BouncyCastle.Crypto.Parameters; using Org.BouncyCastle.Math; using Org.BouncyCastle.Security; +using Org.BouncyCastle.Utilities; namespace Org.BouncyCastle.Crypto.Engines { @@ -157,14 +158,12 @@ namespace Org.BouncyCastle.Crypto.Engines output = new byte[this.GetOutputBlockSize()]; - // TODO Add methods to allow writing BigInteger to existing byte array? - byte[] out1 = gamma.ToByteArrayUnsigned(); - byte[] out2 = phi.ToByteArrayUnsigned(); - out1.CopyTo(output, output.Length / 2 - out1.Length); - out2.CopyTo(output, output.Length - out2.Length); - } + int mid = output.Length / 2; + BigIntegers.AsUnsignedByteArray(gamma, output, 0, mid); + BigIntegers.AsUnsignedByteArray(phi, output, mid, output.Length - mid); + } - return output; + return output; } } } -- cgit 1.4.1