Beispiel #1
0
        public static void MY_SHA0_final(MY_SHA0_CTX *ctx)
        {
            UCHAR *p   = ctx->buf;
            UINT64 cnt = ctx->count * 8;
            int    i;
            var    x = new byte[1] {
                0x80
            };

            MY_SHA0_update(ctx, x);
            var zero = new byte[1] {
                0x00
            };

            while ((ctx->count & 63) != 56)
            {
                MY_SHA0_update(ctx, zero);
            }
            Span <byte> t = new byte[1];

            for (i = 0; i < 8; ++i)
            {
                t[0] = (UCHAR)(cnt >> ((7 - i) * 8));
                MY_SHA0_update(ctx, t);
            }
            for (i = 0; i < 5; i++)
            {
                UINT tmp = ctx->state[i];
                *    p++ = (BYTE)(tmp >> 24);
                *    p++ = (BYTE)(tmp >> 16);
                *    p++ = (BYTE)(tmp >> 8);
                *    p++ = (BYTE)(tmp >> 0);
            }
        }
Beispiel #2
0
        public static void MY_SHA0_Transform(MY_SHA0_CTX *ctx)
        {
            Span <UINT> W = stackalloc UINT[80];
            UINT        A, B, C, D, E;
            UCHAR *     p = ctx->buf;
            int         t;

            for (t = 0; t < 16; ++t)
            {
                UINT tmp = (UINT)(*p++) << 24;
                tmp |= (UINT)(*p++) << 16;
                tmp |= (UINT)(*p++) << 8;
                tmp |= (UINT)(*p++);
                W[t] = tmp;
            }
            for (; t < 80; t++)
            {
                //W[t] = rol(1,W[t-3] ^ W[t-8] ^ W[t-14] ^ W[t-16]);
                W[t] = (W[t - 3] ^ W[t - 8] ^ W[t - 14] ^ W[t - 16]);
            }
            A = ctx->state[0];
            B = ctx->state[1];
            C = ctx->state[2];
            D = ctx->state[3];
            E = ctx->state[4];
            for (t = 0; t < 80; t++)
            {
                UINT tmp = rol(5, A) + E + W[t];
                if (t < 20)
                {
                    tmp += (D ^ (B & (C ^ D))) + 0x5A827999;
                }
                else if (t < 40)
                {
                    tmp += (B ^ C ^ D) + 0x6ED9EBA1;
                }
                else if (t < 60)
                {
                    tmp += ((B & C) | (D & (B | C))) + 0x8F1BBCDC;
                }
                else
                {
                    tmp += (B ^ C ^ D) + 0xCA62C1D6;
                }
                E = D;
                D = C;
                C = rol(30, B);
                B = A;
                A = tmp;
            }
            ctx->state[0] += A;
            ctx->state[1] += B;
            ctx->state[2] += C;
            ctx->state[3] += D;
            ctx->state[4] += E;
        }
Beispiel #3
0
        public static void MY_SHA0_update(MY_SHA0_CTX *ctx, ReadOnlySpan <byte> data)
        {
            UINT len = (UINT)data.Length;
            int  i   = (int)(ctx->count & 63);

            fixed(UCHAR *ptr = data)
            {
                UCHAR *p = ptr;

                ctx->count += len;
                while (len-- != 0)
                {
                    ctx->buf[i++] = *p++;
                    if (i == 64)
                    {
                        MY_SHA0_Transform(ctx);
                        i = 0;
                    }
                }
            }
        }