diff --git a/crypto/src/crypto/digests/NullDigest.cs b/crypto/src/crypto/digests/NullDigest.cs
index e598cb145..76b69afbf 100644
--- a/crypto/src/crypto/digests/NullDigest.cs
+++ b/crypto/src/crypto/digests/NullDigest.cs
@@ -1,6 +1,8 @@
using System;
using System.IO;
+using Org.BouncyCastle.Utilities.IO;
+
namespace Org.BouncyCastle.Crypto.Digests
{
public class NullDigest : IDigest
@@ -20,7 +22,7 @@ namespace Org.BouncyCastle.Crypto.Digests
public int GetDigestSize()
{
- return (int) bOut.Length;
+ return (int)bOut.Length;
}
public void Update(byte b)
@@ -33,15 +35,19 @@ namespace Org.BouncyCastle.Crypto.Digests
bOut.Write(inBytes, inOff, len);
}
- public int DoFinal(byte[] outBytes, int outOff)
+ public int DoFinal(byte[] outBytes, int outOff)
{
- byte[] res = bOut.ToArray();
- res.CopyTo(outBytes, outOff);
- Reset();
- return res.Length;
- }
-
- public void Reset()
+ try
+ {
+ return Streams.WriteBufTo(bOut, outBytes, outOff);
+ }
+ finally
+ {
+ Reset();
+ }
+ }
+
+ public void Reset()
{
bOut.SetLength(0);
}
diff --git a/crypto/src/crypto/signers/Ed25519Signer.cs b/crypto/src/crypto/signers/Ed25519Signer.cs
index 904450ed1..3776880db 100644
--- a/crypto/src/crypto/signers/Ed25519Signer.cs
+++ b/crypto/src/crypto/signers/Ed25519Signer.cs
@@ -5,6 +5,7 @@ using System.Runtime.CompilerServices;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Math.EC.Rfc8032;
using Org.BouncyCastle.Utilities;
+using Org.BouncyCastle.Utilities.IO;
namespace Org.BouncyCastle.Crypto.Signers
{
@@ -114,12 +115,12 @@ namespace Org.BouncyCastle.Crypto.Signers
[MethodImpl(MethodImplOptions.Synchronized)]
internal void Reset()
{
+ long count = Position;
#if PORTABLE
this.Position = 0L;
-
- // TODO Clear using Write method
+ Streams.WriteZeroes(this, count);
#else
- Array.Clear(GetBuffer(), 0, (int)Position);
+ Array.Clear(GetBuffer(), 0, (int)count);
#endif
this.Position = 0L;
}
diff --git a/crypto/src/crypto/signers/Ed25519ctxSigner.cs b/crypto/src/crypto/signers/Ed25519ctxSigner.cs
index e9c2eca44..9a7b2b904 100644
--- a/crypto/src/crypto/signers/Ed25519ctxSigner.cs
+++ b/crypto/src/crypto/signers/Ed25519ctxSigner.cs
@@ -5,6 +5,7 @@ using System.Runtime.CompilerServices;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Math.EC.Rfc8032;
using Org.BouncyCastle.Utilities;
+using Org.BouncyCastle.Utilities.IO;
namespace Org.BouncyCastle.Crypto.Signers
{
@@ -116,12 +117,12 @@ namespace Org.BouncyCastle.Crypto.Signers
[MethodImpl(MethodImplOptions.Synchronized)]
internal void Reset()
{
+ long count = Position;
#if PORTABLE
this.Position = 0L;
-
- // TODO Clear using Write method
+ Streams.WriteZeroes(this, count);
#else
- Array.Clear(GetBuffer(), 0, (int)Position);
+ Array.Clear(GetBuffer(), 0, (int)count);
#endif
this.Position = 0L;
}
diff --git a/crypto/src/crypto/signers/Ed448Signer.cs b/crypto/src/crypto/signers/Ed448Signer.cs
index c01d84b4d..44e26b94f 100644
--- a/crypto/src/crypto/signers/Ed448Signer.cs
+++ b/crypto/src/crypto/signers/Ed448Signer.cs
@@ -5,6 +5,7 @@ using System.Runtime.CompilerServices;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Math.EC.Rfc8032;
using Org.BouncyCastle.Utilities;
+using Org.BouncyCastle.Utilities.IO;
namespace Org.BouncyCastle.Crypto.Signers
{
@@ -116,12 +117,12 @@ namespace Org.BouncyCastle.Crypto.Signers
[MethodImpl(MethodImplOptions.Synchronized)]
internal void Reset()
{
+ long count = Position;
#if PORTABLE
this.Position = 0L;
-
- // TODO Clear using Write method
+ Streams.WriteZeroes(this, count);
#else
- Array.Clear(GetBuffer(), 0, (int)Position);
+ Array.Clear(GetBuffer(), 0, (int)count);
#endif
this.Position = 0L;
}
diff --git a/crypto/src/util/io/Streams.cs b/crypto/src/util/io/Streams.cs
index cc7fa924c..503a1b4f1 100644
--- a/crypto/src/util/io/Streams.cs
+++ b/crypto/src/util/io/Streams.cs
@@ -96,5 +96,30 @@ namespace Org.BouncyCastle.Utilities.IO
{
buf.WriteTo(output);
}
+
+ /// <exception cref="IOException"></exception>
+ public static int WriteBufTo(MemoryStream buf, byte[] output, int offset)
+ {
+#if PORTABLE
+ byte[] bytes = buf.ToArray();
+ bytes.CopyTo(output, offset);
+ return bytes.Length;
+#else
+ int size = (int)buf.Length;
+ buf.WriteTo(new MemoryStream(output, offset, size, true));
+ return size;
+#endif
+ }
+
+ public static void WriteZeroes(Stream outStr, long count)
+ {
+ byte[] zeroes = new byte[BufferSize];
+ while (count > BufferSize)
+ {
+ outStr.Write(zeroes, 0, BufferSize);
+ count -= BufferSize;
+ }
+ outStr.Write(zeroes, 0, (int)count);
+ }
}
}
|