< Summary

Information
Class: Renci.SshNet.Security.Org.BouncyCastle.Crypto.Digests.Sha256Digest
Assembly: Renci.SshNet
File(s): \home\appveyor\projects\ssh-net\src\Renci.SshNet\Security\BouncyCastle\crypto\digests\Sha256Digest.cs
Line coverage
83%
Covered lines: 141
Uncovered lines: 28
Coverable lines: 169
Total lines: 280
Line coverage: 83.4%
Branch coverage
87%
Covered branches: 7
Total branches: 8
Branch coverage: 87.5%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
.ctor()100%1100%
.ctor(...)100%10%
CopyIn(...)100%10%
get_AlgorithmName()100%10%
GetDigestSize()100%1100%
ProcessWord(...)100%2100%
ProcessLength(...)50%262.5%
DoFinal(...)100%1100%
Reset()100%1100%
initHs()100%1100%
ProcessBlock()100%4100%
Sum1Ch(...)100%1100%
Sum0Maj(...)100%1100%
Theta0(...)100%1100%
Theta1(...)100%1100%
.cctor()100%1100%
Copy()100%10%
Reset(...)100%10%

File(s)

\home\appveyor\projects\ssh-net\src\Renci.SshNet\Security\BouncyCastle\crypto\digests\Sha256Digest.cs

#LineLine coverage
 1using System;
 2
 3using Renci.SshNet.Security.Org.BouncyCastle.Crypto.Utilities;
 4using Renci.SshNet.Security.Org.BouncyCastle.Utilities;
 5
 6namespace Renci.SshNet.Security.Org.BouncyCastle.Crypto.Digests
 7{
 8    internal class Sha256Digest
 9    : GeneralDigest
 10    {
 11        private const int DigestLength = 32;
 12
 13        private uint H1, H2, H3, H4, H5, H6, H7, H8;
 1014        private uint[] X = new uint[64];
 15        private int xOff;
 16
 1017        public Sha256Digest()
 1018        {
 1019      initHs();
 1020        }
 21
 22        /**
 23        * Copy constructor.  This will copy the state of the provided
 24        * message digest.
 25        */
 026        public Sha256Digest(Sha256Digest t) : base(t)
 027        {
 028      CopyIn(t);
 029    }
 30
 31    private void CopyIn(Sha256Digest t)
 032    {
 033      base.CopyIn(t);
 34
 035            H1 = t.H1;
 036            H2 = t.H2;
 037            H3 = t.H3;
 038            H4 = t.H4;
 039            H5 = t.H5;
 040            H6 = t.H6;
 041            H7 = t.H7;
 042            H8 = t.H8;
 43
 044            Array.Copy(t.X, 0, X, 0, t.X.Length);
 045            xOff = t.xOff;
 046        }
 47
 48        public override string AlgorithmName
 49    {
 050      get { return "SHA-256"; }
 51    }
 52
 53    public override int GetDigestSize()
 3054    {
 3055      return DigestLength;
 3056    }
 57
 58    internal override void ProcessWord(
 59            byte[]  input,
 60            int     inOff)
 62261    {
 62262      X[xOff] = Pack.BE_To_UInt32(input, inOff);
 63
 62264      if (++xOff == 16)
 2865            {
 2866                ProcessBlock();
 2867            }
 62268        }
 69
 70    internal override void ProcessLength(
 71            long bitLength)
 3872        {
 3873            if (xOff > 14)
 074            {
 075                ProcessBlock();
 076            }
 77
 3878            X[14] = (uint)((ulong)bitLength >> 32);
 3879            X[15] = (uint)((ulong)bitLength);
 3880        }
 81
 82        public override int DoFinal(
 83            byte[]  output,
 84            int     outOff)
 3885        {
 3886            Finish();
 87
 3888            Pack.UInt32_To_BE((uint)H1, output, outOff);
 3889            Pack.UInt32_To_BE((uint)H2, output, outOff + 4);
 3890            Pack.UInt32_To_BE((uint)H3, output, outOff + 8);
 3891            Pack.UInt32_To_BE((uint)H4, output, outOff + 12);
 3892            Pack.UInt32_To_BE((uint)H5, output, outOff + 16);
 3893            Pack.UInt32_To_BE((uint)H6, output, outOff + 20);
 3894            Pack.UInt32_To_BE((uint)H7, output, outOff + 24);
 3895            Pack.UInt32_To_BE((uint)H8, output, outOff + 28);
 96
 3897            Reset();
 98
 3899            return DigestLength;
 38100        }
 101
 102        public override void Reset()
 38103        {
 38104            base.Reset();
 105
 38106      initHs();
 107
 38108            xOff = 0;
 38109      Array.Clear(X, 0, X.Length);
 38110        }
 111
 112    private void initHs()
 48113    {
 114            /* SHA-256 initial hash value
 115            * The first 32 bits of the fractional parts of the square roots
 116            * of the first eight prime numbers
 117            */
 48118            H1 = 0x6a09e667;
 48119            H2 = 0xbb67ae85;
 48120            H3 = 0x3c6ef372;
 48121            H4 = 0xa54ff53a;
 48122            H5 = 0x510e527f;
 48123            H6 = 0x9b05688c;
 48124            H7 = 0x1f83d9ab;
 48125            H8 = 0x5be0cd19;
 48126    }
 127
 128        internal override void ProcessBlock()
 66129        {
 130            //
 131            // expand 16 word block into 64 word blocks.
 132            //
 6468133            for (int ti = 16; ti <= 63; ti++)
 3168134            {
 3168135                X[ti] = Theta1(X[ti - 2]) + X[ti - 7] + Theta0(X[ti - 15]) + X[ti - 16];
 3168136            }
 137
 138            //
 139            // set up working variables.
 140            //
 66141            uint a = H1;
 66142            uint b = H2;
 66143            uint c = H3;
 66144            uint d = H4;
 66145            uint e = H5;
 66146            uint f = H6;
 66147            uint g = H7;
 66148            uint h = H8;
 149
 66150      int t = 0;
 1188151      for(int i = 0; i < 8; ++i)
 528152      {
 153        // t = 8 * i
 528154        h += Sum1Ch(e, f, g) + K[t] + X[t];
 528155        d += h;
 528156        h += Sum0Maj(a, b, c);
 528157        ++t;
 158
 159        // t = 8 * i + 1
 528160        g += Sum1Ch(d, e, f) + K[t] + X[t];
 528161        c += g;
 528162        g += Sum0Maj(h, a, b);
 528163        ++t;
 164
 165        // t = 8 * i + 2
 528166        f += Sum1Ch(c, d, e) + K[t] + X[t];
 528167        b += f;
 528168        f += Sum0Maj(g, h, a);
 528169        ++t;
 170
 171        // t = 8 * i + 3
 528172        e += Sum1Ch(b, c, d) + K[t] + X[t];
 528173        a += e;
 528174        e += Sum0Maj(f, g, h);
 528175        ++t;
 176
 177        // t = 8 * i + 4
 528178        d += Sum1Ch(a, b, c) + K[t] + X[t];
 528179        h += d;
 528180        d += Sum0Maj(e, f, g);
 528181        ++t;
 182
 183        // t = 8 * i + 5
 528184        c += Sum1Ch(h, a, b) + K[t] + X[t];
 528185        g += c;
 528186        c += Sum0Maj(d, e, f);
 528187        ++t;
 188
 189        // t = 8 * i + 6
 528190        b += Sum1Ch(g, h, a) + K[t] + X[t];
 528191        f += b;
 528192        b += Sum0Maj(c, d, e);
 528193        ++t;
 194
 195        // t = 8 * i + 7
 528196        a += Sum1Ch(f, g, h) + K[t] + X[t];
 528197        e += a;
 528198        a += Sum0Maj(b, c, d);
 528199        ++t;
 528200      }
 201
 66202      H1 += a;
 66203            H2 += b;
 66204            H3 += c;
 66205            H4 += d;
 66206            H5 += e;
 66207            H6 += f;
 66208            H7 += g;
 66209            H8 += h;
 210
 211            //
 212            // reset the offset and clean out the word buffer.
 213            //
 66214            xOff = 0;
 66215      Array.Clear(X, 0, 16);
 66216        }
 217
 218    private static uint Sum1Ch(
 219            uint    x,
 220            uint    y,
 221            uint    z)
 4224222    {
 4224223          return (((x >> 6) | (x << 26)) ^ ((x >> 11) | (x << 21)) ^ ((x >> 25) | (x << 7)))
 4224224        + ((x & y) ^ ((~x) & z));
 4224225    }
 226
 227    private static uint Sum0Maj(
 228            uint  x,
 229            uint    y,
 230            uint    z)
 4224231    {
 4224232          return (((x >> 2) | (x << 30)) ^ ((x >> 13) | (x << 19)) ^ ((x >> 22) | (x << 10)))
 4224233        + ((x & y) ^ (x & z) ^ (y & z));
 4224234    }
 235
 236        private static uint Theta0(
 237            uint x)
 3168238        {
 3168239          return ((x >> 7) | (x << 25)) ^ ((x >> 18) | (x << 14)) ^ (x >> 3);
 3168240        }
 241
 242        private static uint Theta1(
 243            uint x)
 3168244        {
 3168245          return ((x >> 17) | (x << 15)) ^ ((x >> 19) | (x << 13)) ^ (x >> 10);
 3168246        }
 247
 1248        private static readonly uint[] K = {
 1249            0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
 1250      0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
 1251            0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
 1252            0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
 1253            0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
 1254            0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
 1255            0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
 1256            0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
 1257            0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
 1258            0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
 1259            0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
 1260            0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
 1261            0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
 1262            0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
 1263            0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
 1264            0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
 1265        };
 266
 267    public override IMemoable Copy()
 0268    {
 0269      return new Sha256Digest(this);
 0270    }
 271
 272    public override void Reset(IMemoable other)
 0273    {
 0274      Sha256Digest d = (Sha256Digest)other;
 275
 0276      CopyIn(d);
 0277    }
 278
 279    }
 280}