diff options
Diffstat (limited to 'crypto/src/crmf/PKMacBuilder.cs')
-rw-r--r-- | crypto/src/crmf/PKMacBuilder.cs | 78 |
1 files changed, 12 insertions, 66 deletions
diff --git a/crypto/src/crmf/PKMacBuilder.cs b/crypto/src/crmf/PKMacBuilder.cs index 6db80325d..f59ba8f35 100644 --- a/crypto/src/crmf/PKMacBuilder.cs +++ b/crypto/src/crmf/PKMacBuilder.cs @@ -8,90 +8,36 @@ using Org.BouncyCastle.Asn1.Oiw; using Org.BouncyCastle.Asn1.X509; using Org.BouncyCastle.Crypto; using Org.BouncyCastle.Crypto.IO; +using Org.BouncyCastle.Crypto.Operators; using Org.BouncyCastle.Crypto.Parameters; using Org.BouncyCastle.Security; using Org.BouncyCastle.Utilities; namespace Org.BouncyCastle.Crmf { - internal class PKMacStreamCalculator - : IStreamCalculator<DefaultPKMacResult> - { - private readonly MacSink _stream; - - public PKMacStreamCalculator(IMac mac) - { - _stream = new MacSink(mac); - } - - public Stream Stream - { - get { return _stream; } - } - - public DefaultPKMacResult GetResult() - { - return new DefaultPKMacResult(_stream.Mac); - } - } - - internal class PKMacFactory + internal sealed class PKMacFactory : IMacFactory { - protected readonly PbmParameter parameters; - private readonly byte[] key; + private readonly KeyParameter m_key; + private readonly PbmParameter m_parameters; public PKMacFactory(byte[] key, PbmParameter parameters) { - this.key = Arrays.Clone(key); - this.parameters = parameters; + m_key = new KeyParameter(key); + m_parameters = parameters; } - public virtual object AlgorithmDetails - { - get { return new AlgorithmIdentifier(CmpObjectIdentifiers.passwordBasedMac, parameters); } - } + public object AlgorithmDetails => + new AlgorithmIdentifier(CmpObjectIdentifiers.passwordBasedMac, m_parameters); - public virtual IStreamCalculator<IBlockResult> CreateCalculator() + public IStreamCalculator<IBlockResult> CreateCalculator() { - IMac mac = MacUtilities.GetMac(parameters.Mac.Algorithm); - mac.Init(new KeyParameter(key)); - return new PKMacStreamCalculator(mac); + IMac mac = MacUtilities.GetMac(m_parameters.Mac.Algorithm); + mac.Init(m_key); + return new DefaultMacCalculator(mac); } } - internal sealed class DefaultPKMacResult - : IBlockResult - { - private readonly IMac mac; - - public DefaultPKMacResult(IMac mac) - { - this.mac = mac; - } - - public byte[] Collect() - { - byte[] res = new byte[mac.GetMacSize()]; - mac.DoFinal(res, 0); - return res; - } - - public int Collect(byte[] buf, int off) - { - return mac.DoFinal(buf, off); - } - -#if NETCOREAPP2_1_OR_GREATER || NETSTANDARD2_1_OR_GREATER - public int Collect(Span<byte> output) - { - return mac.DoFinal(output); - } -#endif - - public int GetMaxResultLength() => mac.GetMacSize(); - } - public class PKMacBuilder { private AlgorithmIdentifier owf; |