⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sha2.c

📁 log4cxx 0.10 unix下编译包
💻 C
📖 第 1 页 / 共 3 页
字号:
        /* Now for the remaining rounds up to 79: */        do {                ROUND512(a,b,c,d,e,f,g,h);                ROUND512(h,a,b,c,d,e,f,g);                ROUND512(g,h,a,b,c,d,e,f);                ROUND512(f,g,h,a,b,c,d,e);                ROUND512(e,f,g,h,a,b,c,d);                ROUND512(d,e,f,g,h,a,b,c);                ROUND512(c,d,e,f,g,h,a,b);                ROUND512(b,c,d,e,f,g,h,a);        } while (j < 80);        /* Compute the current intermediate hash value */        context->state[0] += a;        context->state[1] += b;        context->state[2] += c;        context->state[3] += d;        context->state[4] += e;        context->state[5] += f;        context->state[6] += g;        context->state[7] += h;        /* Clean up */        a = b = c = d = e = f = g = h = T1 = 0;}#else /* SHA2_UNROLL_TRANSFORM */void apr__SHA512_Transform(SHA512_CTX* context, const sha2_word64* data) {        sha2_word64     a, b, c, d, e, f, g, h, s0, s1;        sha2_word64     T1, T2, *W512 = (sha2_word64*)context->buffer;        int             j;        /* Initialize registers with the prev. intermediate value */        a = context->state[0];        b = context->state[1];        c = context->state[2];        d = context->state[3];        e = context->state[4];        f = context->state[5];        g = context->state[6];        h = context->state[7];        j = 0;        do {#if !APR_IS_BIGENDIAN                /* Convert TO host byte order */                REVERSE64(*data++, W512[j]);                /* Apply the SHA-512 compression function to update a..h */                T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + W512[j];#else /* APR_IS_BIGENDIAN */                /* Apply the SHA-512 compression function to update a..h with copy */                T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] + (W512[j] = *data++);#endif /* APR_IS_BIGENDIAN */                T2 = Sigma0_512(a) + Maj(a, b, c);                h = g;                g = f;                f = e;                e = d + T1;                d = c;                c = b;                b = a;                a = T1 + T2;                j++;        } while (j < 16);        do {                /* Part of the message block expansion: */                s0 = W512[(j+1)&0x0f];                s0 = sigma0_512(s0);                s1 = W512[(j+14)&0x0f];                s1 =  sigma1_512(s1);                /* Apply the SHA-512 compression function to update a..h */                T1 = h + Sigma1_512(e) + Ch(e, f, g) + K512[j] +                     (W512[j&0x0f] += s1 + W512[(j+9)&0x0f] + s0);                T2 = Sigma0_512(a) + Maj(a, b, c);                h = g;                g = f;                f = e;                e = d + T1;                d = c;                c = b;                b = a;                a = T1 + T2;                j++;        } while (j < 80);        /* Compute the current intermediate hash value */        context->state[0] += a;        context->state[1] += b;        context->state[2] += c;        context->state[3] += d;        context->state[4] += e;        context->state[5] += f;        context->state[6] += g;        context->state[7] += h;        /* Clean up */        a = b = c = d = e = f = g = h = T1 = T2 = 0;}#endif /* SHA2_UNROLL_TRANSFORM */void apr__SHA512_Update(SHA512_CTX* context, const sha2_byte *data, size_t len) {        unsigned int    freespace, usedspace;        if (len == 0) {                /* Calling with no data is valid - we do nothing */                return;        }        /* Sanity check: */        assert(context != (SHA512_CTX*)0 && data != (sha2_byte*)0);        usedspace = (unsigned int)((context->bitcount[0] >> 3)                                  % SHA512_BLOCK_LENGTH);        if (usedspace > 0) {                /* Calculate how much free space is available in the buffer */                freespace = SHA512_BLOCK_LENGTH - usedspace;                if (len >= freespace) {                        /* Fill the buffer completely and process it */                        MEMCPY_BCOPY(&context->buffer[usedspace], data, freespace);                        ADDINC128(context->bitcount, freespace << 3);                        len -= freespace;                        data += freespace;                        apr__SHA512_Transform(context, (sha2_word64*)context->buffer);                } else {                        /* The buffer is not yet full */                        MEMCPY_BCOPY(&context->buffer[usedspace], data, len);                        ADDINC128(context->bitcount, len << 3);                        /* Clean up: */                        usedspace = freespace = 0;                        return;                }        }        while (len >= SHA512_BLOCK_LENGTH) {                /* Process as many complete blocks as we can */                apr__SHA512_Transform(context, (sha2_word64*)data);                ADDINC128(context->bitcount, SHA512_BLOCK_LENGTH << 3);                len -= SHA512_BLOCK_LENGTH;                data += SHA512_BLOCK_LENGTH;        }        if (len > 0) {                /* There's left-overs, so save 'em */                MEMCPY_BCOPY(context->buffer, data, len);                ADDINC128(context->bitcount, len << 3);        }        /* Clean up: */        usedspace = freespace = 0;}void apr__SHA512_Last(SHA512_CTX* context) {        unsigned int    usedspace;        usedspace = (unsigned int)((context->bitcount[0] >> 3)                                  % SHA512_BLOCK_LENGTH);#if !APR_IS_BIGENDIAN        /* Convert FROM host byte order */        REVERSE64(context->bitcount[0],context->bitcount[0]);        REVERSE64(context->bitcount[1],context->bitcount[1]);#endif        if (usedspace > 0) {                /* Begin padding with a 1 bit: */                context->buffer[usedspace++] = 0x80;                if (usedspace <= SHA512_SHORT_BLOCK_LENGTH) {                        /* Set-up for the last transform: */                        MEMSET_BZERO(&context->buffer[usedspace], SHA512_SHORT_BLOCK_LENGTH - usedspace);                } else {                        if (usedspace < SHA512_BLOCK_LENGTH) {                                MEMSET_BZERO(&context->buffer[usedspace], SHA512_BLOCK_LENGTH - usedspace);                        }                        /* Do second-to-last transform: */                        apr__SHA512_Transform(context, (sha2_word64*)context->buffer);                        /* And set-up for the last transform: */                        MEMSET_BZERO(context->buffer, SHA512_BLOCK_LENGTH - 2);                }        } else {                /* Prepare for final transform: */                MEMSET_BZERO(context->buffer, SHA512_SHORT_BLOCK_LENGTH);                /* Begin padding with a 1 bit: */                *context->buffer = 0x80;        }        /* Store the length of input data (in bits): */        *(sha2_word64*)&context->buffer[SHA512_SHORT_BLOCK_LENGTH] = context->bitcount[1];        *(sha2_word64*)&context->buffer[SHA512_SHORT_BLOCK_LENGTH+8] = context->bitcount[0];        /* Final transform: */        apr__SHA512_Transform(context, (sha2_word64*)context->buffer);}void apr__SHA512_Final(sha2_byte digest[], SHA512_CTX* context) {        sha2_word64     *d = (sha2_word64*)digest;        /* Sanity check: */        assert(context != (SHA512_CTX*)0);        /* If no digest buffer is passed, we don't bother doing this: */        if (digest != (sha2_byte*)0) {                apr__SHA512_Last(context);                /* Save the hash data for output: */#if !APR_IS_BIGENDIAN                {                        /* Convert TO host byte order */                        int     j;                        for (j = 0; j < 8; j++) {                                REVERSE64(context->state[j],context->state[j]);                                *d++ = context->state[j];                        }                }#else /* APR_IS_BIGENDIAN */                MEMCPY_BCOPY(d, context->state, SHA512_DIGEST_LENGTH);#endif /* APR_IS_BIGENDIAN */        }        /* Zero out state data */        MEMSET_BZERO(context, sizeof(context));}char *apr__SHA512_End(SHA512_CTX* context, char buffer[]) {        sha2_byte       digest[SHA512_DIGEST_LENGTH], *d = digest;        int             i;        /* Sanity check: */        assert(context != (SHA512_CTX*)0);        if (buffer != (char*)0) {                apr__SHA512_Final(digest, context);                for (i = 0; i < SHA512_DIGEST_LENGTH; i++) {                        *buffer++ = sha2_hex_digits[(*d & 0xf0) >> 4];                        *buffer++ = sha2_hex_digits[*d & 0x0f];                        d++;                }                *buffer = (char)0;        } else {                MEMSET_BZERO(context, sizeof(context));        }        MEMSET_BZERO(digest, SHA512_DIGEST_LENGTH);        return buffer;}char* apr__SHA512_Data(const sha2_byte* data, size_t len, char digest[SHA512_DIGEST_STRING_LENGTH]) {        SHA512_CTX      context;        apr__SHA512_Init(&context);        apr__SHA512_Update(&context, data, len);        return apr__SHA512_End(&context, digest);}/*** SHA-384: *********************************************************/void apr__SHA384_Init(SHA384_CTX* context) {        if (context == (SHA384_CTX*)0) {                return;        }        MEMCPY_BCOPY(context->state, sha384_initial_hash_value, SHA512_DIGEST_LENGTH);        MEMSET_BZERO(context->buffer, SHA384_BLOCK_LENGTH);        context->bitcount[0] = context->bitcount[1] = 0;}void apr__SHA384_Update(SHA384_CTX* context, const sha2_byte* data, size_t len) {        apr__SHA512_Update((SHA512_CTX*)context, data, len);}void apr__SHA384_Final(sha2_byte digest[], SHA384_CTX* context) {        sha2_word64     *d = (sha2_word64*)digest;        /* Sanity check: */        assert(context != (SHA384_CTX*)0);        /* If no digest buffer is passed, we don't bother doing this: */        if (digest != (sha2_byte*)0) {                apr__SHA512_Last((SHA512_CTX*)context);                /* Save the hash data for output: */#if !APR_IS_BIGENDIAN                {                        /* Convert TO host byte order */                        int     j;                        for (j = 0; j < 6; j++) {                                REVERSE64(context->state[j],context->state[j]);                                *d++ = context->state[j];                        }                }#else /* APR_IS_BIGENDIAN */                MEMCPY_BCOPY(d, context->state, SHA384_DIGEST_LENGTH);#endif /* APR_IS_BIGENDIAN */        }        /* Zero out state data */        MEMSET_BZERO(context, sizeof(context));}char *apr__SHA384_End(SHA384_CTX* context, char buffer[]) {        sha2_byte       digest[SHA384_DIGEST_LENGTH], *d = digest;        int             i;        /* Sanity check: */        assert(context != (SHA384_CTX*)0);        if (buffer != (char*)0) {                apr__SHA384_Final(digest, context);                for (i = 0; i < SHA384_DIGEST_LENGTH; i++) {                        *buffer++ = sha2_hex_digits[(*d & 0xf0) >> 4];                        *buffer++ = sha2_hex_digits[*d & 0x0f];                        d++;                }                *buffer = (char)0;        } else {                MEMSET_BZERO(context, sizeof(context));        }        MEMSET_BZERO(digest, SHA384_DIGEST_LENGTH);        return buffer;}char* apr__SHA384_Data(const sha2_byte* data, size_t len, char digest[SHA384_DIGEST_STRING_LENGTH]) {        SHA384_CTX      context;        apr__SHA384_Init(&context);        apr__SHA384_Update(&context, data, len);        return apr__SHA384_End(&context, digest);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -