алгоритм sha1 на C#

Discussion in 'С/С++, C#, Rust, Swift, Go, Java, Perl, Ruby' started by Fox_NICK, 22 Dec 2011.

  1. Fox_NICK

    Fox_NICK Elder - Старейшина

    Joined:
    10 Jan 2007
    Messages:
    45
    Likes Received:
    5
    Reputations:
    0
    Всем доброго времени суток!
    Так вот вопрос : может кто нибудь когда учился в универи реализовывал алгоритм хеша sha1 на C#
    если кто может поделиться какими исходниками на С# буду благодарен! :p
    Использовать Security.Cryptography нельзя! :)
     
  2. xophet

    xophet Member

    Joined:
    16 Apr 2011
    Messages:
    617
    Likes Received:
    49
    Reputations:
    5
    а посмотреть исходники и подшаманить под себя, предварительно разобравшись как работает? не вариант?
     
  3. Forte

    Forte Elder - Старейшина

    Joined:
    27 Sep 2010
    Messages:
    53
    Likes Received:
    24
    Reputations:
    32
    using System;
    using System.Text;
    using System.Security.Cryptography;

    namespace Apress.VisualCSharpRecipes.Chapter11
    {
    class Crypto
    {
    public static void Main(string[] args)
    {
    // Create a HashAlgorithm of the type specified by the first
    // command-line argument.
    HashAlgorithm hashAlg = null;
    if (args[0].CompareTo("SHA1Managed") == 0)
    {
    hashAlg = new SHA1Managed();
    }
    else
    {
    hashAlg = HashAlgorithm.Create(args[0]);
    }

    using (hashAlg)
    {
    // Convert the password string, provided as the second
    // command-line argument, to an array of bytes.
    byte[] pwordData = Encoding.Default.GetBytes(args[1]);

    // Generate the hash code of the password.
    byte[] hash = hashAlg.ComputeHash(pwordData);

    // Display the hash code of the password to the console.
    Console.WriteLine(BitConverter.ToString(hash));

    // Wait to continue.
    Console.WriteLine("\n Press Enter to exit.");
    Console.ReadLine();
    }
    }
    }
    }
     
  4. Kaimi

    Kaimi Well-Known Member

    Joined:
    23 Aug 2007
    Messages:
    1,732
    Likes Received:
    811
    Reputations:
    231
    Диссонанс
     
    _________________________
  5. Fox_NICK

    Fox_NICK Elder - Старейшина

    Joined:
    10 Jan 2007
    Messages:
    45
    Likes Received:
    5
    Reputations:
    0
    как сделать на C# инициализацию вот этого
    h0 = 0x67452301
    h1 = 0xEFCDAB89
    h2 = 0x98BADCFE
    h3 = 0x10325476
    h4 = 0xC3D2E1F0

    вижу C# в первый раз)
     
  6. Forte

    Forte Elder - Старейшина

    Joined:
    27 Sep 2010
    Messages:
    53
    Likes Received:
    24
    Reputations:
    32
    Const h0 = 0x67452301
    Const h1 = 0xEFCDAB89
    Const h2 = 0x98BADCFE
    Const h3 = 0x10325476
    Const h4 = 0xC3D2E1F0


    Private Const h0 = 0x67452301
    Private Const h1 = 0xEFCDAB89
    Private Const h2 = 0x98BADCFE
    Private Const h3 = 0x10325476
    Private Const h4 = 0xC3D2E1F0
     
    #6 Forte, 23 Dec 2011
    Last edited: 23 Dec 2011
  7. Fox_NICK

    Fox_NICK Elder - Старейшина

    Joined:
    10 Jan 2007
    Messages:
    45
    Likes Received:
    5
    Reputations:
    0
    не...
    вот по идеи:)
    Code:
                UInt32 h0 = 0x67452301, h1 = 0xEFCDAB89, h2 = 0x98BADCFE, h3 = 0x10325476, h4 = 0xC3D2E1F0, a, b, c, d, e;
                UInt32[] digest=new UInt32[5];
                Console.WriteLine("Ввод");
                string s=Console.ReadLine();
                UInt64 SIZE = Convert.ToUInt64(s.Length);
                uint[] W = new uint[80];
    
    
    Вот как бы дописать 1 бит к строке, если бы кто подсказал :rolleyes:
     
  8. FEV

    FEV Member

    Joined:
    23 Sep 2009
    Messages:
    15
    Likes Received:
    7
    Reputations:
    0
    кури:

    Code:
    
    [ComVisible(true)]
    public class SHA1Managed : SHA1
    {
        // Fields
        private byte[] _buffer;
        private long _count;
        private uint[] _expandedBuffer;
        private uint[] _stateSHA1;
    
        // Methods
        public SHA1Managed()
        {
            if (CryptoConfig.AllowOnlyFipsAlgorithms)
            {
                throw new InvalidOperationException(Environment.GetResourceString("Cryptography_NonCompliantFIPSAlgorithm"));
            }
            this._stateSHA1 = new uint[5];
            this._buffer = new byte[0x40];
            this._expandedBuffer = new uint[80];
            this.InitializeState();
        }
    
        private byte[] _EndHash()
        {
            byte[] block = new byte[20];
            int num = 0x40 - ((int) (this._count & 0x3fL));
            if (num <= 8)
            {
                num += 0x40;
            }
            byte[] partIn = new byte[num];
            partIn[0] = 0x80;
            long num2 = this._count * 8L;
            partIn[num - 8] = (byte) ((num2 >> 0x38) & 0xffL);
            partIn[num - 7] = (byte) ((num2 >> 0x30) & 0xffL);
            partIn[num - 6] = (byte) ((num2 >> 40) & 0xffL);
            partIn[num - 5] = (byte) ((num2 >> 0x20) & 0xffL);
            partIn[num - 4] = (byte) ((num2 >> 0x18) & 0xffL);
            partIn[num - 3] = (byte) ((num2 >> 0x10) & 0xffL);
            partIn[num - 2] = (byte) ((num2 >> 8) & 0xffL);
            partIn[num - 1] = (byte) (num2 & 0xffL);
            this._HashData(partIn, 0, partIn.Length);
            Utils.DWORDToBigEndian(block, this._stateSHA1, 5);
            base.HashValue = block;
            return block;
        }
    
        [SecuritySafeCritical]
        private unsafe void _HashData(byte[] partIn, int ibStart, int cbSize)
        {
            int byteCount = cbSize;
            int srcOffsetBytes = ibStart;
            int dstOffsetBytes = (int) (this._count & 0x3fL);
            this._count += byteCount;
            fixed (uint* numRef = this._stateSHA1)
            {
                fixed (byte* numRef2 = this._buffer)
                {
                    fixed (uint* numRef3 = this._expandedBuffer)
                    {
                        if ((dstOffsetBytes > 0) && ((dstOffsetBytes + byteCount) >= 0x40))
                        {
                            Buffer.InternalBlockCopy(partIn, srcOffsetBytes, this._buffer, dstOffsetBytes, 0x40 - dstOffsetBytes);
                            srcOffsetBytes += 0x40 - dstOffsetBytes;
                            byteCount -= 0x40 - dstOffsetBytes;
                            SHATransform(numRef3, numRef, numRef2);
                            dstOffsetBytes = 0;
                        }
                        while (byteCount >= 0x40)
                        {
                            Buffer.InternalBlockCopy(partIn, srcOffsetBytes, this._buffer, 0, 0x40);
                            srcOffsetBytes += 0x40;
                            byteCount -= 0x40;
                            SHATransform(numRef3, numRef, numRef2);
                        }
                        if (byteCount > 0)
                        {
                            Buffer.InternalBlockCopy(partIn, srcOffsetBytes, this._buffer, dstOffsetBytes, byteCount);
                        }
                    }
                }
            }
        }
    
        [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
        protected override void HashCore(byte[] rgb, int ibStart, int cbSize)
        {
            this._HashData(rgb, ibStart, cbSize);
        }
    
        [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
        protected override byte[] HashFinal()
        {
            return this._EndHash();
        }
    
        public override void Initialize()
        {
            this.InitializeState();
            Array.Clear(this._buffer, 0, this._buffer.Length);
            Array.Clear(this._expandedBuffer, 0, this._expandedBuffer.Length);
        }
    
        private void InitializeState()
        {
            this._count = 0L;
            this._stateSHA1[0] = 0x67452301;
            this._stateSHA1[1] = 0xefcdab89;
            this._stateSHA1[2] = 0x98badcfe;
            this._stateSHA1[3] = 0x10325476;
            this._stateSHA1[4] = 0xc3d2e1f0;
        }
    
        [SecurityCritical]
        private static unsafe void SHAExpand(uint* x)
        {
            for (int i = 0x10; i < 80; i++)
            {
                uint num2 = ((x[i - 3] ^ x[i - 8]) ^ x[i - 14]) ^ x[i - 0x10];
                x[i] = (num2 << 1) | (num2 >> 0x1f);
            }
        }
    
        [SecurityCritical]
        private static unsafe void SHATransform(uint* expandedBuffer, uint* state, byte* block)
        {
            uint num = state[0];
            uint num2 = state[1];
            uint num3 = state[2];
            uint num4 = state[3];
            uint num5 = state[4];
            Utils.DWORDFromBigEndian(expandedBuffer, 0x10, block);
            SHAExpand(expandedBuffer);
            int index = 0;
            while (index < 20)
            {
                num5 += ((((num << 5) | (num >> 0x1b)) + (num4 ^ (num2 & (num3 ^ num4)))) + expandedBuffer[index]) + 0x5a827999;
                num2 = (num2 << 30) | (num2 >> 2);
                num4 += ((((num5 << 5) | (num5 >> 0x1b)) + (num3 ^ (num & (num2 ^ num3)))) + expandedBuffer[index + 1]) + 0x5a827999;
                num = (num << 30) | (num >> 2);
                num3 += ((((num4 << 5) | (num4 >> 0x1b)) + (num2 ^ (num5 & (num ^ num2)))) + expandedBuffer[index + 2]) + 0x5a827999;
                num5 = (num5 << 30) | (num5 >> 2);
                num2 += ((((num3 << 5) | (num3 >> 0x1b)) + (num ^ (num4 & (num5 ^ num)))) + expandedBuffer[index + 3]) + 0x5a827999;
                num4 = (num4 << 30) | (num4 >> 2);
                num += ((((num2 << 5) | (num2 >> 0x1b)) + (num5 ^ (num3 & (num4 ^ num5)))) + expandedBuffer[index + 4]) + 0x5a827999;
                num3 = (num3 << 30) | (num3 >> 2);
                index += 5;
            }
            while (index < 40)
            {
                num5 += ((((num << 5) | (num >> 0x1b)) + ((num2 ^ num3) ^ num4)) + expandedBuffer[index]) + 0x6ed9eba1;
                num2 = (num2 << 30) | (num2 >> 2);
                num4 += ((((num5 << 5) | (num5 >> 0x1b)) + ((num ^ num2) ^ num3)) + expandedBuffer[index + 1]) + 0x6ed9eba1;
                num = (num << 30) | (num >> 2);
                num3 += ((((num4 << 5) | (num4 >> 0x1b)) + ((num5 ^ num) ^ num2)) + expandedBuffer[index + 2]) + 0x6ed9eba1;
                num5 = (num5 << 30) | (num5 >> 2);
                num2 += ((((num3 << 5) | (num3 >> 0x1b)) + ((num4 ^ num5) ^ num)) + expandedBuffer[index + 3]) + 0x6ed9eba1;
                num4 = (num4 << 30) | (num4 >> 2);
                num += ((((num2 << 5) | (num2 >> 0x1b)) + ((num3 ^ num4) ^ num5)) + expandedBuffer[index + 4]) + 0x6ed9eba1;
                num3 = (num3 << 30) | (num3 >> 2);
                index += 5;
            }
            while (index < 60)
            {
                num5 += ((((num << 5) | (num >> 0x1b)) + ((num2 & num3) | (num4 & (num2 | num3)))) + expandedBuffer[index]) + 0x8f1bbcdc;
                num2 = (num2 << 30) | (num2 >> 2);
                num4 += ((((num5 << 5) | (num5 >> 0x1b)) + ((num & num2) | (num3 & (num | num2)))) + expandedBuffer[index + 1]) + 0x8f1bbcdc;
                num = (num << 30) | (num >> 2);
                num3 += ((((num4 << 5) | (num4 >> 0x1b)) + ((num5 & num) | (num2 & (num5 | num)))) + expandedBuffer[index + 2]) + 0x8f1bbcdc;
                num5 = (num5 << 30) | (num5 >> 2);
                num2 += ((((num3 << 5) | (num3 >> 0x1b)) + ((num4 & num5) | (num & (num4 | num5)))) + expandedBuffer[index + 3]) + 0x8f1bbcdc;
                num4 = (num4 << 30) | (num4 >> 2);
                num += ((((num2 << 5) | (num2 >> 0x1b)) + ((num3 & num4) | (num5 & (num3 | num4)))) + expandedBuffer[index + 4]) + 0x8f1bbcdc;
                num3 = (num3 << 30) | (num3 >> 2);
                index += 5;
            }
            while (index < 80)
            {
                num5 += ((((num << 5) | (num >> 0x1b)) + ((num2 ^ num3) ^ num4)) + expandedBuffer[index]) + 0xca62c1d6;
                num2 = (num2 << 30) | (num2 >> 2);
                num4 += ((((num5 << 5) | (num5 >> 0x1b)) + ((num ^ num2) ^ num3)) + expandedBuffer[index + 1]) + 0xca62c1d6;
                num = (num << 30) | (num >> 2);
                num3 += ((((num4 << 5) | (num4 >> 0x1b)) + ((num5 ^ num) ^ num2)) + expandedBuffer[index + 2]) + 0xca62c1d6;
                num5 = (num5 << 30) | (num5 >> 2);
                num2 += ((((num3 << 5) | (num3 >> 0x1b)) + ((num4 ^ num5) ^ num)) + expandedBuffer[index + 3]) + 0xca62c1d6;
                num4 = (num4 << 30) | (num4 >> 2);
                num += ((((num2 << 5) | (num2 >> 0x1b)) + ((num3 ^ num4) ^ num5)) + expandedBuffer[index + 4]) + 0xca62c1d6;
                num3 = (num3 << 30) | (num3 >> 2);
                index += 5;
            }
            state[0] += num;
            uint* numPtr1 = state + 1;
            numPtr1[0] += num2;
            uint* numPtr2 = state + 2;
            numPtr2[0] += num3;
            uint* numPtr3 = state + 3;
            numPtr3[0] += num4;
            uint* numPtr4 = state + 4;
            numPtr4[0] += num5;
        }
    }
    
     
    Collapse Methods