예제 #1
0
        /// <inheritdoc cref="DisposableBase.ThrowIfDisposed"/>
        /// <exception cref="InvalidOperationException">Already initialized.</exception>
        /// <exception cref="ArgumentException">Throws if the <paramref name="plainBytes" /> is empty.</exception>
        /// <exception cref="ArgumentNullException">Throws if the <paramref name="plainBytes" /> is null.</exception>
        /// <exception cref="CryptographicException">
        ///     Throws if the <paramref name="plainBytes" /> size does not conform to block
        ///     size defined in <see cref="BlockSizeInBytes" />.
        /// </exception>
        public async Task InitializeAsync(byte[] plainBytes)
        {
            if (plainBytes == null)
            {
                throw new ArgumentNullException(nameof(plainBytes));
            }
            if (plainBytes.Length == 0)
            {
                throw new ArgumentException("Value cannot be an empty collection.", nameof(plainBytes));
            }
            if (plainBytes.Length % BlockSizeInBytes != 0)
            {
                throw new CryptographicException(
                          $"Block size is {BlockSizeInBytes}, but plain bytes have {plainBytes.Length}. Maybe pad the bytes?");
            }
            if (IsInitialized)
            {
                throw new InvalidOperationException("Already initialized");
            }
            ThrowIfDisposed();
            var encryptedBytes = plainBytes;
            await _protector.ProtectAsync(encryptedBytes).ConfigureAwait(false);

            _encryptedBytes = encryptedBytes;
        }