예제 #1
0
        public void Encrypt(ReadOnlySpan <byte> nonce, ReadOnlySpan <byte> source,
                            Span <byte> destination, Span <byte> tag, ReadOnlySpan <byte> associatedData = default)
        {
            var input = ArrayPool <byte> .Shared.Rent(source.Length);

            var output = ArrayPool <byte> .Shared.Rent(destination.Length + tag.Length);

            try
            {
                _engine = new BufferedAeadCipher(new ChaCha20Poly1305());
                _engine.Init(true, new AeadParameters(_key, 128, nonce.ToArray(), associatedData.ToArray()));

                source.CopyTo(input);

                _engine.DoFinal(input, 0, source.Length, output, 0);

                output.AsSpan(0, destination.Length).CopyTo(destination);
                output.AsSpan(destination.Length, tag.Length).CopyTo(tag);
            }
            finally
            {
                ArrayPool <byte> .Shared.Return(input);

                ArrayPool <byte> .Shared.Return(output);
            }
        }
예제 #2
0
 public BcChaCha20Poly1305Crypto(byte[] key)
 {
     _key    = new KeyParameter(key);
     _engine = new BufferedAeadCipher(new ChaCha20Poly1305());
 }