< Summary

Information
Class: Renci.SshNet.Security.Cryptography.BlockCipher
Assembly: Renci.SshNet
File(s): \home\appveyor\projects\ssh-net\src\Renci.SshNet\Security\Cryptography\BlockCipher.cs
Line coverage
88%
Covered lines: 61
Uncovered lines: 8
Coverable lines: 69
Total lines: 171
Line coverage: 88.4%
Branch coverage
81%
Covered branches: 18
Total branches: 22
Branch coverage: 81.8%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
get_MinimumSize()100%1100%
get_BlockSize()100%1100%
.ctor(...)100%2100%
Encrypt(...)80%1085.71%
Decrypt(...)100%1100%
Decrypt(...)80%1085.18%

File(s)

\home\appveyor\projects\ssh-net\src\Renci.SshNet\Security\Cryptography\BlockCipher.cs

#LineLine coverage
 1using System;
 2using Renci.SshNet.Security.Cryptography.Ciphers;
 3
 4namespace Renci.SshNet.Security.Cryptography
 5{
 6    /// <summary>
 7    /// Base class for block cipher implementations.
 8    /// </summary>
 9    public abstract class BlockCipher : SymmetricCipher
 10    {
 11        private readonly CipherMode _mode;
 12
 13        private readonly CipherPadding _padding;
 14
 15        /// <summary>
 16        /// Gets the size of the block in bytes.
 17        /// </summary>
 18        /// <value>
 19        /// The size of the block in bytes.
 20        /// </value>
 21        private readonly byte _blockSize;
 22
 23        /// <summary>
 24        /// Gets the minimum data size.
 25        /// </summary>
 26        /// <value>
 27        /// The minimum data size.
 28        /// </value>
 29        public override byte MinimumSize
 30        {
 30353731            get { return BlockSize; }
 32        }
 33
 34        /// <summary>
 35        /// Gets the size of the block.
 36        /// </summary>
 37        /// <value>
 38        /// The size of the block.
 39        /// </value>
 40        public byte BlockSize
 41        {
 42            get
 55547043            {
 55547044                return _blockSize;
 55547045            }
 46        }
 47
 48        /// <summary>
 49        /// Initializes a new instance of the <see cref="BlockCipher"/> class.
 50        /// </summary>
 51        /// <param name="key">The key.</param>
 52        /// <param name="blockSize">Size of the block.</param>
 53        /// <param name="mode">Cipher mode.</param>
 54        /// <param name="padding">Cipher padding.</param>
 55        /// <exception cref="ArgumentNullException"><paramref name="key"/> is <see langword="null"/>.</exception>
 56        protected BlockCipher(byte[] key, byte blockSize, CipherMode mode, CipherPadding padding)
 551757            : base(key)
 551758        {
 551759            _blockSize = blockSize;
 551760            _mode = mode;
 551761            _padding = padding;
 62
 551763            _mode?.Init(this);
 551764        }
 65
 66        /// <summary>
 67        /// Encrypts the specified data.
 68        /// </summary>
 69        /// <param name="input">The data.</param>
 70        /// <param name="offset">The zero-based offset in <paramref name="input"/> at which to begin encrypting.</param>
 71        /// <param name="length">The number of bytes to encrypt from <paramref name="input"/>.</param>
 72        /// <returns>
 73        /// The encrypted data.
 74        /// </returns>
 75        public override byte[] Encrypt(byte[] input, int offset, int length)
 7776        {
 7777            if (length % _blockSize > 0)
 378            {
 379                if (_padding is null)
 080                {
 081                    throw new ArgumentException("data");
 82                }
 83
 384                var paddingLength = _blockSize - (length % _blockSize);
 385                input = _padding.Pad(input, offset, length, paddingLength);
 386                length += paddingLength;
 387                offset = 0;
 388            }
 89
 7790            var output = new byte[length];
 7791            var writtenBytes = 0;
 92
 104093            for (var i = 0; i < length / _blockSize; i++)
 44394            {
 44395                if (_mode is null)
 396                {
 397                    writtenBytes += EncryptBlock(input, offset + (i * _blockSize), _blockSize, output, i * _blockSize);
 398                }
 99                else
 440100                {
 440101                    writtenBytes += _mode.EncryptBlock(input, offset + (i * _blockSize), _blockSize, output, i * _blockS
 440102                }
 443103            }
 104
 77105            if (writtenBytes < length)
 0106            {
 0107                throw new InvalidOperationException("Encryption error.");
 108            }
 109
 77110            return output;
 77111        }
 112
 113        /// <summary>
 114        /// Decrypts the specified data.
 115        /// </summary>
 116        /// <param name="input">The data.</param>
 117        /// <returns>
 118        /// The decrypted data.
 119        /// </returns>
 120        public override byte[] Decrypt(byte[] input)
 52841121        {
 52841122            return Decrypt(input, 0, input.Length);
 52841123        }
 124
 125        /// <summary>
 126        /// Decrypts the specified input.
 127        /// </summary>
 128        /// <param name="input">The input.</param>
 129        /// <param name="offset">The zero-based offset in <paramref name="input"/> at which to begin decrypting.</param>
 130        /// <param name="length">The number of bytes to decrypt from <paramref name="input"/>.</param>
 131        /// <returns>
 132        /// The decrypted data.
 133        /// </returns>
 134        public override byte[] Decrypt(byte[] input, int offset, int length)
 128135        {
 128136            if (length % _blockSize > 0)
 6137            {
 6138                if (_padding is null)
 0139                {
 0140                    throw new ArgumentException("data");
 141                }
 142
 6143                input = _padding.Pad(_blockSize, input, offset, length);
 6144                offset = 0;
 6145                length = input.Length;
 6146            }
 147
 128148            var output = new byte[length];
 149
 128150            var writtenBytes = 0;
 5070151            for (var i = 0; i < length / _blockSize; i++)
 2407152            {
 2407153                if (_mode is null)
 3154                {
 3155                    writtenBytes += DecryptBlock(input, offset + (i * _blockSize), _blockSize, output, i * _blockSize);
 3156                }
 157                else
 2404158                {
 2404159                    writtenBytes += _mode.DecryptBlock(input, offset + (i * _blockSize), _blockSize, output, i * _blockS
 2404160                }
 2407161            }
 162
 128163            if (writtenBytes < length)
 0164            {
 0165                throw new InvalidOperationException("Encryption error.");
 166            }
 167
 128168            return output;
 128169        }
 170    }
 171}