예제 #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="raw">[Plain]</param>
        /// <param name="key"></param>
        /// <param name="nonce"></param>
        /// <param name="aad"></param>
        /// <returns>[Cipher][Tag]</returns>
        protected override SmartBuffer EncryptChunk(ReadOnlyMemory <byte> raw, ReadOnlySpan <byte> key, ReadOnlySpan <byte> nonce, ReadOnlySpan <byte> aad = default)
        {
            var aead = this.CreateCipher(key, nonce, aad);

            using (MemoryStream src = MemoryStreamManager.GetStream(), work = MemoryStreamManager.GetStream())
            {
                raw.CopyToStream(src);
                src.Position = 0;

                var tag = aead.Encrypt(src, src, work);
                src.Position = 0;

                var rt = SmartBuffer.Rent((int)raw.Length + tag.Length);
                rt.SignificantLength = rt.Memory.CopyFromStream(src);

                tag.AsMemory().CopyTo(rt.FreeMemory);
                rt.SignificantLength += tag.Length;

                return(rt);
            }
        }