< Summary

Information
Class: Renci.SshNet.Security.Cryptography.CipherDigitalSignature
Assembly: Renci.SshNet
File(s): \home\appveyor\projects\ssh-net\src\Renci.SshNet\Security\Cryptography\CipherDigitalSignature.cs
Line coverage
92%
Covered lines: 26
Uncovered lines: 2
Coverable lines: 28
Total lines: 90
Line coverage: 92.8%
Branch coverage
50%
Covered branches: 1
Total branches: 2
Branch coverage: 50%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
.ctor(...)50%275%
Verify(...)100%1100%
Sign(...)100%1100%
DerEncode(...)100%1100%

File(s)

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

#LineLine coverage
 1using System;
 2using Renci.SshNet.Common;
 3
 4namespace Renci.SshNet.Security.Cryptography
 5{
 6    /// <summary>
 7    /// Implements digital signature where where asymmetric cipher is used.
 8    /// </summary>
 9    public abstract class CipherDigitalSignature : DigitalSignature
 10    {
 11        private readonly AsymmetricCipher _cipher;
 12        private readonly ObjectIdentifier _oid;
 13
 14        /// <summary>
 15        /// Initializes a new instance of the <see cref="CipherDigitalSignature"/> class.
 16        /// </summary>
 17        /// <param name="oid">The object identifier.</param>
 18        /// <param name="cipher">The cipher.</param>
 276819        protected CipherDigitalSignature(ObjectIdentifier oid, AsymmetricCipher cipher)
 276820        {
 276821            if (cipher is null)
 022            {
 023                throw new ArgumentNullException(nameof(cipher));
 24            }
 25
 276826            _cipher = cipher;
 276827            _oid = oid;
 276828        }
 29
 30        /// <summary>
 31        /// Verifies the signature.
 32        /// </summary>
 33        /// <param name="input">The input.</param>
 34        /// <param name="signature">The signature.</param>
 35        /// <returns>
 36        /// <see langword="true"/> if signature was successfully verified; otherwise <see langword="false"/>.
 37        /// </returns>
 38        public override bool Verify(byte[] input, byte[] signature)
 121039        {
 121040            var encryptedSignature = _cipher.Decrypt(signature);
 121041            var hashData = Hash(input);
 121042            var expected = DerEncode(hashData);
 121043            return expected.IsEqualTo(encryptedSignature);
 121044        }
 45
 46        /// <summary>
 47        /// Creates the signature.
 48        /// </summary>
 49        /// <param name="input">The input.</param>
 50        /// <returns>
 51        /// Signed input data.
 52        /// </returns>
 53        public override byte[] Sign(byte[] input)
 69654        {
 55            // Calculate hash value
 69656            var hashData = Hash(input);
 57
 58            // Calculate DER string
 69659            var derEncodedHash = DerEncode(hashData);
 60
 69661            return _cipher.Encrypt(derEncodedHash).TrimLeadingZeros();
 69662        }
 63
 64        /// <summary>
 65        /// Hashes the specified input.
 66        /// </summary>
 67        /// <param name="input">The input.</param>
 68        /// <returns>Hashed data.</returns>
 69        protected abstract byte[] Hash(byte[] input);
 70
 71        /// <summary>
 72        /// Encodes hash using DER.
 73        /// </summary>
 74        /// <param name="hashData">The hash data.</param>
 75        /// <returns>
 76        /// DER Encoded byte array.
 77        /// </returns>
 78        protected byte[] DerEncode(byte[] hashData)
 190679        {
 190680            var alg = new DerData();
 190681            alg.Write(_oid);
 190682            alg.WriteNull();
 83
 190684            var data = new DerData();
 190685            data.Write(alg);
 190686            data.Write(hashData);
 190687            return data.Encode();
 190688        }
 89    }
 90}