Code/AutoRig/Dl/Sha256Pure.cs

Pure managed SHA-256 implementation. Provides a streaming Hasher that accepts Update calls and returns a hex digest with FinishHex, plus a convenience HashHex for one-shot hashing.

using System.Text;

namespace AutoRig.Dl;

/// <summary>
/// Pure managed SHA-256 (FIPS 180-4): System.Security.Cryptography is not on the
/// s&box whitelist, and download verification must run engine-side.
/// </summary>
public static class Sha256Pure
{
    static readonly uint[] K =
    {
        0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
        0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
        0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
        0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
        0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
        0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
        0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
        0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
    };

    /// <summary>Streaming hasher: feed blocks, then <see cref="FinishHex"/>.</summary>
    public sealed class Hasher
    {
        readonly uint[] _h =
        {
            0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
            0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19,
        };
        readonly byte[] _block = new byte[64];
        int _blockFill;
        ulong _totalBytes;

        public void Update( byte[] data, int offset, int count )
        {
            _totalBytes += (ulong)count;
            while ( count > 0 )
            {
                var take = Math.Min( 64 - _blockFill, count );
                Array.Copy( data, offset, _block, _blockFill, take );
                _blockFill += take;
                offset += take;
                count -= take;
                if ( _blockFill == 64 )
                {
                    Compress( _block, 0 );
                    _blockFill = 0;
                }
            }
        }

        public string FinishHex()
        {
            var bitLength = _totalBytes * 8;
            // Padding: 0x80, zeros, 8-byte big-endian bit length.
            Update( new byte[] { 0x80 }, 0, 1 );
            _totalBytes -= 1; // Update counted the pad byte
            while ( _blockFill != 56 )
            {
                Update( new byte[] { 0 }, 0, 1 );
                _totalBytes -= 1;
            }
            var lengthBytes = new byte[8];
            for ( var i = 0; i < 8; i++ )
                lengthBytes[7 - i] = (byte)(bitLength >> (i * 8));
            Update( lengthBytes, 0, 8 );

            var sb = new StringBuilder( 64 );
            foreach ( var word in _h )
                sb.Append( word.ToString( "x8" ) );
            return sb.ToString();
        }

        void Compress( byte[] data, int offset )
        {
            var w = new uint[64];
            for ( var i = 0; i < 16; i++ )
                w[i] = (uint)(data[offset + i * 4] << 24 | data[offset + i * 4 + 1] << 16
                    | data[offset + i * 4 + 2] << 8 | data[offset + i * 4 + 3]);
            for ( var i = 16; i < 64; i++ )
            {
                var s0 = Rotr( w[i - 15], 7 ) ^ Rotr( w[i - 15], 18 ) ^ (w[i - 15] >> 3);
                var s1 = Rotr( w[i - 2], 17 ) ^ Rotr( w[i - 2], 19 ) ^ (w[i - 2] >> 10);
                w[i] = w[i - 16] + s0 + w[i - 7] + s1;
            }

            uint a = _h[0], b = _h[1], c = _h[2], d = _h[3];
            uint e = _h[4], f = _h[5], g = _h[6], h = _h[7];
            for ( var i = 0; i < 64; i++ )
            {
                var s1 = Rotr( e, 6 ) ^ Rotr( e, 11 ) ^ Rotr( e, 25 );
                var ch = (e & f) ^ (~e & g);
                var temp1 = h + s1 + ch + K[i] + w[i];
                var s0 = Rotr( a, 2 ) ^ Rotr( a, 13 ) ^ Rotr( a, 22 );
                var maj = (a & b) ^ (a & c) ^ (b & c);
                var temp2 = s0 + maj;
                h = g; g = f; f = e; e = d + temp1;
                d = c; c = b; b = a; a = temp1 + temp2;
            }
            _h[0] += a; _h[1] += b; _h[2] += c; _h[3] += d;
            _h[4] += e; _h[5] += f; _h[6] += g; _h[7] += h;
        }

        static uint Rotr( uint x, int n ) => (x >> n) | (x << (32 - n));
    }

    /// <summary>Hex SHA-256 of a byte array.</summary>
    public static string HashHex( byte[] data )
    {
        var hasher = new Hasher();
        hasher.Update( data, 0, data.Length );
        return hasher.FinishHex();
    }
}