<PackageReference Include="BouncyCastle.Cryptography" Version="2.6.0" />

ParametersWithContext

using System; using System.Buffers; namespace Org.BouncyCastle.Crypto.Parameters { public class ParametersWithContext : ICipherParameters { private readonly ICipherParameters m_parameters; private readonly byte[] m_context; public int ContextLength => m_context.Length; public ICipherParameters Parameters => m_parameters; internal ReadOnlySpan<byte> Context => m_context; public static ParametersWithContext Create<TState>(ICipherParameters parameters, int contextLength, TState state, SpanAction<byte, TState> action) { if (action == null) throw new ArgumentNullException("action"); if (contextLength < 0) throw new ArgumentOutOfRangeException("contextLength"); ParametersWithContext parametersWithContext = new ParametersWithContext(parameters, contextLength); action(parametersWithContext.m_context, state); return parametersWithContext; } internal static ICipherParameters ApplyOptionalContext(ICipherParameters parameters, byte[] context) { if (context != null) return new ParametersWithContext(parameters, context); return parameters; } public ParametersWithContext(ICipherParameters parameters, byte[] context) { if (context == null) throw new ArgumentNullException("context"); m_parameters = parameters; m_context = (byte[])context.Clone(); } public ParametersWithContext(ICipherParameters parameters, byte[] context, int contextOff, int contextLen) { if (context == null) throw new ArgumentNullException("context"); m_parameters = parameters; m_context = new byte[contextLen]; Array.Copy(context, contextOff, m_context, 0, contextLen); } public ParametersWithContext(ICipherParameters parameters, ReadOnlySpan<byte> context) { m_parameters = parameters; m_context = context.ToArray(); } private ParametersWithContext(ICipherParameters parameters, int contextLength) { if (contextLength < 0) throw new ArgumentOutOfRangeException("contextLength"); m_parameters = parameters; m_context = new byte[contextLength]; } public void CopyContextTo(byte[] buf, int off, int len) { if (m_context.Length != len) throw new ArgumentOutOfRangeException("len"); Array.Copy(m_context, 0, buf, off, len); } public byte[] GetContext() { return (byte[])m_context.Clone(); } } }