From 9d4dde60ecdcfabf38eaa98c483b15263e0b99e5 Mon Sep 17 00:00:00 2001 From: Peter Dettman Date: Wed, 9 Nov 2022 20:45:17 +0700 Subject: Add GetMaxResultLength method --- crypto/src/crmf/PKMacBuilder.cs | 16 ++++++-------- crypto/src/crypto/IBlockResult.cs | 12 ++++++----- crypto/src/crypto/SimpleBlockResult.cs | 25 ++++++++-------------- .../src/crypto/operators/DefaultSignatureResult.cs | 16 ++++++++------ 4 files changed, 32 insertions(+), 37 deletions(-) diff --git a/crypto/src/crmf/PKMacBuilder.cs b/crypto/src/crmf/PKMacBuilder.cs index ae9baa3d0..7261a9daf 100644 --- a/crypto/src/crmf/PKMacBuilder.cs +++ b/crypto/src/crmf/PKMacBuilder.cs @@ -60,7 +60,7 @@ namespace Org.BouncyCastle.Crmf } } - internal class DefaultPKMacResult + internal sealed class DefaultPKMacResult : IBlockResult { private readonly IMac mac; @@ -77,21 +77,19 @@ namespace Org.BouncyCastle.Crmf return res; } - public int Collect(byte[] sig, int sigOff) + public int Collect(byte[] buf, int off) { - byte[] signature = Collect(); - signature.CopyTo(sig, sigOff); - return signature.Length; + return mac.DoFinal(buf, off); } #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER - public int Collect(Span destination) + public int Collect(Span output) { - byte[] result = Collect(); - result.CopyTo(destination); - return result.Length; + return mac.DoFinal(output); } #endif + + public int GetMaxResultLength() => mac.GetMacSize(); } public class PKMacBuilder diff --git a/crypto/src/crypto/IBlockResult.cs b/crypto/src/crypto/IBlockResult.cs index 3ed96a7c1..f3b73e59f 100644 --- a/crypto/src/crypto/IBlockResult.cs +++ b/crypto/src/crypto/IBlockResult.cs @@ -18,17 +18,19 @@ namespace Org.BouncyCastle.Crypto /// Store the final result of the operation by copying it into the destination array. /// /// The number of bytes copied into destination. - /// The byte array to copy the result into. - /// The offset into destination to start copying the result at. - int Collect(byte[] destination, int offset); + /// The byte array to copy the result into. + /// The offset into destination to start copying the result at. + int Collect(byte[] buf, int off); #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER /// /// Store the final result of the operation by copying it into the destination span. /// /// The number of bytes copied into destination. - /// The span to copy the result into. - int Collect(Span destination); + /// The span to copy the result into. + int Collect(Span output); #endif + + int GetMaxResultLength(); } } diff --git a/crypto/src/crypto/SimpleBlockResult.cs b/crypto/src/crypto/SimpleBlockResult.cs index 35432c2b3..3f1a814e8 100644 --- a/crypto/src/crypto/SimpleBlockResult.cs +++ b/crypto/src/crypto/SimpleBlockResult.cs @@ -5,7 +5,7 @@ namespace Org.BouncyCastle.Crypto /// /// A simple block result object which just carries a byte array. /// - public class SimpleBlockResult + public sealed class SimpleBlockResult : IBlockResult { private readonly byte[] result; @@ -19,15 +19,6 @@ namespace Org.BouncyCastle.Crypto this.result = result; } - /// - /// Return the number of bytes in the result - /// - /// The length of the result in bytes. - public int Length - { - get { return result.Length; } - } - /// /// Return the final result of the operation. /// @@ -41,22 +32,24 @@ namespace Org.BouncyCastle.Crypto /// Store the final result of the operation by copying it into the destination array. /// /// The number of bytes copied into destination. - /// The byte array to copy the result into. - /// The offset into destination to start copying the result at. - public int Collect(byte[] destination, int offset) + /// The byte array to copy the result into. + /// The offset into destination to start copying the result at. + public int Collect(byte[] buf, int off) { - Array.Copy(result, 0, destination, offset, result.Length); + Array.Copy(result, 0, buf, off, result.Length); return result.Length; } #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER - public int Collect(Span destination) + public int Collect(Span output) { - result.CopyTo(destination); + result.CopyTo(output); return result.Length; } #endif + + public int GetMaxResultLength() => result.Length; } } diff --git a/crypto/src/crypto/operators/DefaultSignatureResult.cs b/crypto/src/crypto/operators/DefaultSignatureResult.cs index a236838d6..cbbc04d20 100644 --- a/crypto/src/crypto/operators/DefaultSignatureResult.cs +++ b/crypto/src/crypto/operators/DefaultSignatureResult.cs @@ -2,7 +2,7 @@ namespace Org.BouncyCastle.Crypto.Operators { - public class DefaultSignatureResult + public sealed class DefaultSignatureResult : IBlockResult { private readonly ISigner mSigner; @@ -17,20 +17,22 @@ namespace Org.BouncyCastle.Crypto.Operators return mSigner.GenerateSignature(); } - public int Collect(byte[] sig, int sigOff) + public int Collect(byte[] buf, int off) { byte[] signature = Collect(); - signature.CopyTo(sig, sigOff); + signature.CopyTo(buf, off); return signature.Length; } #if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER - public int Collect(Span destination) + public int Collect(Span output) { - byte[] result = Collect(); - result.CopyTo(destination); - return result.Length; + byte[] signature = Collect(); + signature.CopyTo(output); + return signature.Length; } #endif + + public int GetMaxResultLength() => mSigner.GetMaxSignatureSize(); } } -- cgit 1.4.1