精华内容
下载资源
问答
  • sha256 源码
    千次阅读
    2018-08-28 17:31:00

    目前可以实现SHA256

    #include <stdio.h>
    #include <stdlib.h>
    #include <stdint.h>
    #include <string.h>
    
    
    typedef unsigned char           uint_8;
    
    #define SHA256_ROTL(a,b) (((a>>(32-b))&(0x7fffffff>>(31-b)))|(a<<b))
    #define SHA256_SR(a,b) ((a>>b)&(0x7fffffff>>(b-1)))
    
    
    #define SHA256_Ch(x,y,z) ((x&y)^((~x)&z))
    #define SHA256_Maj(x,y,z) ((x&y)^(x&z)^(y&z))
    #define SHA256_E0(x) (SHA256_ROTL(x,30)^SHA256_ROTL(x,19)^SHA256_ROTL(x,10))
    #define SHA256_E1(x) (SHA256_ROTL(x,26)^SHA256_ROTL(x,21)^SHA256_ROTL(x,7))
    #define SHA256_O0(x) (SHA256_ROTL(x,25)^SHA256_ROTL(x,14)^SHA256_SR(x,3))
    #define SHA256_O1(x) (SHA256_ROTL(x,15)^SHA256_ROTL(x,13)^SHA256_SR(x,10))
    
    
    extern char* StrSHA256(const char* str, long long length, char* sha256){
        /*
        计算字符串SHA-256
        参数说明:
        str         字符串指针
        length      字符串长度
        sha256         用于保存SHA-256的字符串指针
        返回值为参数sha256
        */
        char *pp, *ppend;
        long l, i, W[64], T1, T2, A, B, C, D, E, F, G, H, H0, H1, H2, H3, H4, H5, H6, H7;
        H0 = 0x6a09e667, H1 = 0xbb67ae85, H2 = 0x3c6ef372, H3 = 0xa54ff53a;
        H4 = 0x510e527f, H5 = 0x9b05688c, H6 = 0x1f83d9ab, H7 = 0x5be0cd19;
        long K[64] = {
                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,
        };
        l = length + ((length % 64 >= 56) ? (128 - length % 64) : (64 - length % 64));
        if (!(pp = (char*)malloc((unsigned long)l))) return 0;
        for (i = 0; i < length; pp[i + 3 - 2 * (i % 4)] = str[i], i++);
        for (pp[i + 3 - 2 * (i % 4)] = 128, i++; i < l; pp[i + 3 - 2 * (i % 4)] = 0, i++);
        *((long*)(pp + l - 4)) = length << 3;
        *((long*)(pp + l - 8)) = length >> 29;
        for (ppend = pp + l; pp < ppend; pp += 64){
            for (i = 0; i < 16; W[i] = ((long*)pp)[i], i++);
            for (i = 16; i < 64; W[i] = (SHA256_O1(W[i - 2]) + W[i - 7] + SHA256_O0(W[i - 15]) + W[i - 16]), i++);
            A = H0, B = H1, C = H2, D = H3, E = H4, F = H5, G = H6, H = H7;
            for (i = 0; i < 64; i++){
                T1 = H + SHA256_E1(E) + SHA256_Ch(E, F, G) + K[i] + W[i];
                T2 = SHA256_E0(A) + SHA256_Maj(A, B, C);
                H = G, G = F, F = E, E = D + T1, D = C, C = B, B = A, A = T1 + T2;
            }
            H0 += A, H1 += B, H2 += C, H3 += D, H4 += E, H5 += F, H6 += G, H7 += H;
        }
        //free(pp - l);
        sprintf(sha256, "%08X%08X%08X%08X%08X%08X%08X%08X", H0, H1, H2, H3, H4, H5, H6, H7);
        return sha256;
    }
    
    int main(void){
        //char text[] = "00FC00800000000794060001204141";  //需要进行加密的数组
        uint_8 text[] = {0x00,0xFC,0x00,0x80,0x00,0x00,0x00,0x07,0x94,0x06,0x00,0x01,0x20,0x41,0x41};  //需要进行加密的数组
    
    
        char sha256[65];
    
        puts(StrSHA256(text,sizeof(text)-1,sha256));    // 函数返回值即sha256,直接输出也可以
        return 0;
    }
    

     

    更多相关内容
  • SHA256 C源码.zip

    2020-03-10 08:13:53
    sha256实现,逻辑清晰 依次执行SHA256Init、SHA256Update、SHA256Final 三个函数即可得到sha256结果,亲测!!
  • sha256,sha1,crc,md5源码C++

    2015-06-05 09:50:40
    能用的hash库,包括hash, crc32, md5, sha256, sha1, sha3, keccak, digest, hmac. C++实现,封装良好,.h, .cpp分离。实用,好用。
  • SHA256

    2021-02-12 04:34:01
    SHA256
  • 最近陆续造了一批哈希算法的轮子,包括MD家族(包括MD2/MD4/MD5), SHA1, SHA2家族(SHA224, SHA256, SHA384, SHA512),SHA3家族以及国密SM3算法。 原来打算将每一个算法都详细分析并实现,现在看来,这个工作短时间.

    相关文章:

    最近陆续造了一批哈希算法的轮子,包括MD家族(包括MD2/MD4/MD5), SHA1, SHA2家族(SHA224, SHA256, SHA384, SHA512),SHA3家族以及国密SM3算法。

    原来打算将每一个算法都详细分析并实现,现在看来,这个工作短时间可能无法完成,所以先将源码发上来。

    这部分实现的源码完全参考官方文档的算法描述,连变量名也尽可能和官方文档中的变量保持一致,方便学习。

    另外, 代码封装的SHA256和SHA224哈希调用接口参考了openssl官方的接口,完全兼容,无缝对接。会使用这里的接口,就会使用openssl的库函数接口,甚至连代码都不需要修改。

    除了实现的源码外,还另外附带了一个测试例子,这个测试例子不仅仅是用于测试哈希算法的实现是否正确,还可以提供了"-f"/"-s"等选项用于对任意文件和字符串进行哈希,因此作为一个工具使用,类似系统内置的md5sum/sha1sum。

    SHA224和SHA256实现源码

    1. 头文件sha256.c

    /*
     * @        file: sha256.h
     * @ description: header file for sha256.c
     * @      author: Gu Yongqiang
     * @        blog: https://blog.csdn.net/guyongqiangx
     */
    #ifndef __ROCKY_SHA256__H
    #define __ROCKY_SHA256__H
    
    #define ERR_OK           0
    #define ERR_ERR         -1  /* generic error */
    #define ERR_INV_PARAM   -2  /* invalid parameter */
    #define ERR_TOO_LONG    -3  /* too long */
    #define ERR_STATE_ERR   -4  /* state error */
    
    typedef unsigned char      uint8_t;
    typedef unsigned short     uint16_t;
    typedef unsigned int       uint32_t;
    typedef unsigned long long uint64_t;
    
    typedef struct sha256_context {
        /* message total length in bytes */
        uint64_t total;
    
        /* intermedia hash value for each block */
        struct {
            uint32_t a;
            uint32_t b;
            uint32_t c;
            uint32_t d;
            uint32_t e;
    		uint32_t f;
    		uint32_t g;
    		uint32_t h;
        }hash;
    
        /* last block */
        struct {
            uint32_t used;     /* used bytes */
            uint8_t  buf[64];  /* block data buffer */
        }last;
    }SHA256_CTX;
    
    /* https://www.openssl.org/docs/man1.1.1/man3/SHA256_Final.html */
    int SHA224_Init(SHA256_CTX *c);
    int SHA224_Update(SHA256_CTX *c, const void *data, size_t len);
    int SHA224_Final(unsigned char *md, SHA256_CTX *c);
    unsigned char *SHA224(const unsigned char *d, size_t n, unsigned char *md);
    
    int SHA256_Init(SHA256_CTX *c);
    int SHA256_Update(SHA256_CTX *c, const void *data, size_t len);
    int SHA256_Final(unsigned char *md, SHA256_CTX *c);
    unsigned char *SHA256(const unsigned char *d, size_t n, unsigned char *md);
    #endif
    

    2. 代码文件sha256.c

    /*
     * @        file: sha256.c
     * @ description: implementation for the SHA224/SHA256 Secure Hash Algorithm
     * @      author: Gu Yongqiang
     * @        blog: https://blog.csdn.net/guyongqiangx
     */
    #include <stdio.h>
    #include <string.h>
    
    #include "utils.h"
    #include "sha256.h"
    
    //#define DEBUG
    
    #ifdef DEBUG
    #define DBG(...) printf(__VA_ARGS__)
    #define DUMP_BLOCK_DATA 1
    #define DUMP_BLOCK_HASH 1
    #define DUMP_ROUND_DATA 1
    #else
    #define DBG(...)
    #define DUMP_BLOCK_DATA 0
    #define DUMP_BLOCK_HASH 0
    #define DUMP_ROUND_DATA 0
    #endif
    
    #define SHA256_BLOCK_SIZE           64  /* 512 bits = 64 Bytes */
    #define SHA256_LEN_SIZE             8   /* 64 bits = 8 bytes */
    #define SHA256_LEN_OFFSET           (SHA256_BLOCK_SIZE - SHA256_LEN_SIZE)
    
    #define SHA256_DIGEST_SIZE          32 /* 256 bits = 32 bytes */
    #define SHA224_DIGEST_SIZE          28 /* 224 bits = 28 bytes */
    
    #define SHA256_PADDING_PATTERN      0x80
    #define SHA256_ROUND_NUM            64
    
    #define HASH_BLOCK_SIZE             SHA256_BLOCK_SIZE
    #define HASH_LEN_SIZE               SHA256_LEN_SIZE
    #define HASH_LEN_OFFSET             SHA256_LEN_OFFSET
    
    #define HASH_DIGEST_SIZE            SHA256_DIGEST_SIZE      /* use sha256 digest size */
    
    #define HASH_PADDING_PATTERN        SHA256_PADDING_PATTERN
    #define HASH_ROUND_NUM              SHA256_ROUND_NUM
    
    /* SHA256 Constants */
    static const uint32_t K256[HASH_ROUND_NUM] = {
        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
    };
    
    /* ROTate Right (cirular right shift) */
    static uint32_t ROTR(uint32_t x, uint8_t shift)
    {
        return (x >> shift) | (x << (32 - shift));
    }
    
    /* Right SHift */
    static uint32_t SHR(uint32_t x, uint8_t shift)
    {
        return (x >> shift);
    }
    
    /* Ch ... choose */
    static uint32_t Ch(uint32_t x, uint32_t y, uint32_t z)
    {
        return (x & y) ^ (~x & z) ;
    }
    
    /* Maj ... majority */
    static uint32_t Maj(uint32_t x, uint32_t y, uint32_t z)
    {
        return (x & y) ^ (x & z) ^ (y & z);
    }
    
    /* SIGMA0 */
    static uint32_t SIGMA0(uint32_t x)
    {
        return ROTR(x, 2) ^ ROTR(x, 13) ^ ROTR(x, 22);
    }
    
    /* SIGMA1 */
    static uint32_t SIGMA1(uint32_t x)
    {
        return ROTR(x, 6) ^ ROTR(x, 11) ^ ROTR(x, 25);
    }
    
    /* sigma0, different from SIGMA0 */
    static uint32_t sigma0(uint32_t x)
    {
        return ROTR(x, 7) ^ ROTR(x, 18) ^ SHR(x, 3);
    }
    
    /* sigma1, different from SIGMA1 */
    static uint32_t sigma1(uint32_t x)
    {
        return ROTR(x, 17) ^ ROTR(x, 19) ^ SHR(x, 10);
    }
    
    int SHA256_Init(SHA256_CTX *c)
    {
        if (NULL == c)
        {
            return ERR_INV_PARAM;
        }
    
        memset(c, 0, sizeof(SHA256_CTX));
    
        /* Initial Value for SHA256 */
        c->hash.a = 0x6a09e667;
        c->hash.b = 0xbb67ae85;
        c->hash.c = 0x3c6ef372;
        c->hash.d = 0xa54ff53a;
        c->hash.e = 0x510e527f;
        c->hash.f = 0x9b05688c;
        c->hash.g = 0x1f83d9ab;
        c->hash.h = 0x5be0cd19;
    
        return ERR_OK;
    }
    
    static int SHA256_PrepareScheduleWord(const uint32_t *block, uint32_t *W)
    {
        uint32_t t;
    
        if ((NULL == block) || (NULL == W))
        {
            return ERR_INV_PARAM;
        }
    
        for (t=0; t<HASH_ROUND_NUM; t++)
        {
            if (t<=15)  /*  0 <= t <= 15 */
                W[t] = be32toh(block[t]);
            else        /* 16 <= t <= 79 */
                W[t] = sigma1(W[t-2]) + W[t-7] + sigma0(W[t-15]) + W[t-16];
        }
    
        return ERR_OK;
    }
    
    static int SHA256_ProcessBlock(SHA256_CTX *ctx, const void *block)
    {
        uint32_t t;
        uint32_t W[HASH_ROUND_NUM];
        uint32_t T1, T2;
        uint32_t a, b, c, d, e, f, g, h;
    
        if ((NULL == ctx) || (NULL == block))
        {
            return ERR_INV_PARAM;
        }
    
    #if (DUMP_BLOCK_DATA == 1)
        DBG("---------------------------------------------------------\n");
        DBG("   BLOCK: %llu\n", ctx->total/HASH_BLOCK_SIZE);
        DBG("    DATA:\n");
        print_buffer(block, HASH_BLOCK_SIZE, "    ");
    #endif
    
        /* prepare schedule word */
        SHA256_PrepareScheduleWord(block, W);
    
        a = ctx->hash.a;
        b = ctx->hash.b;
        c = ctx->hash.c;
        d = ctx->hash.d;
        e = ctx->hash.e;
        f = ctx->hash.f;
        g = ctx->hash.g;
        h = ctx->hash.h;
    
    #if (DUMP_BLOCK_HASH == 1)
        DBG("      IV: %08x %08x %08x %08x %08x %08x %08x %08x\n",
            ctx->hash.a, ctx->hash.b, ctx->hash.c, ctx->hash.d, ctx->hash.e, ctx->hash.f, ctx->hash.g, ctx->hash.h);
    #endif
    
        for (t=0; t<HASH_ROUND_NUM; t++)
        {
            T1 = h + SIGMA1(e) + Ch(e, f, g) + K256[t] + W[t];
            T2 = SIGMA0(a) + Maj(a, b, c);
            h = g;
            g = f;
            f = e;
            e = d + T1;
            d = c;
            c = b;
            b = a;
            a = T1 + T2;
    
    #if (DUMP_ROUND_DATA == 1)
            DBG("      %02d: T1=0x%08x, T2=0x%08x, W=0x%08x, \n"\
                "           a=0x%08x,  b=0x%08x, c=0x%08x, d=0x%08x, e=0x%08x, f=0x%08x, g=0x%08x, h=0x%08x\n", \
                    t, T1, T2, W[t], a, b, c, d, e, f, g, h);
    #endif
        }
    
        ctx->hash.a += a;
        ctx->hash.b += b;
        ctx->hash.c += c;
        ctx->hash.d += d;
        ctx->hash.e += e;
        ctx->hash.f += f;
        ctx->hash.g += g;
        ctx->hash.h += h;
    
    #if (DUMP_BLOCK_HASH == 1)
        DBG("    HASH: %08x %08x %08x %08x %08x %08x %08x %08x\n",
            ctx->hash.a, ctx->hash.b, ctx->hash.c, ctx->hash.d, ctx->hash.e, ctx->hash.f, ctx->hash.g, ctx->hash.h);
    #endif
    
        return ERR_OK;
    }
    
    
    int SHA256_Update(SHA256_CTX *c, const void *data, size_t len)
    {
        uint32_t copy_len = 0;
    
        if ((NULL == c) || (NULL == data))
        {
            return ERR_INV_PARAM;
        }
    
        /* has used data */
        if (c->last.used != 0)
        {
            /* less than 1 block in total, combine data */
            if (c->last.used + len < HASH_BLOCK_SIZE)
            {
                memcpy(&c->last.buf[c->last.used], data, len);
                c->last.used += len;
    
                return ERR_OK;
            }
            else /* more than 1 block */
            {
                /* process the block in context buffer */
                copy_len = HASH_BLOCK_SIZE - c->last.used;
                memcpy(&c->last.buf[c->last.used], data, copy_len);
                SHA256_ProcessBlock(c, &c->last.buf);
                c->total += HASH_BLOCK_SIZE;
    
                data = (uint8_t *)data + copy_len;
                len -= copy_len;
    
                /* reset context buffer */
                memset(&c->last.buf[0], 0, HASH_BLOCK_SIZE);
                c->last.used = 0;
            }
        }
    
        /* less than 1 block, copy to context buffer */
        if (len < HASH_BLOCK_SIZE)
        {
            memcpy(&c->last.buf[c->last.used], data, len);
            c->last.used += len;
    
            return ERR_OK;
        }
        else
        {
            /* process data blocks */
            while (len >= HASH_BLOCK_SIZE)
            {
                SHA256_ProcessBlock(c, data);
                c->total += HASH_BLOCK_SIZE;
    
                data = (uint8_t *)data + HASH_BLOCK_SIZE;
                len -= HASH_BLOCK_SIZE;
            }
    
            /* copy rest data to context buffer */
            memcpy(&c->last.buf[0], data, len);
            c->last.used = len;
        }
    
        return ERR_OK;
    }
    
    int SHA256_Final(unsigned char *md, SHA256_CTX *c)
    {
        uint32_t *temp;
        //uint64_t *buf;
    
        if ((NULL == c) || (NULL == md))
        {
            return ERR_INV_PARAM;
        }
    
        /* Last block should be less thant HASH_BLOCK_SIZE - HASH_LEN_SIZE */
        if (c->last.used >= (HASH_BLOCK_SIZE - HASH_LEN_SIZE))
        {
            c->total += c->last.used;
    
            /* one more block */
            c->last.buf[c->last.used] = HASH_PADDING_PATTERN;
            c->last.used++;
    
            memset(&c->last.buf[c->last.used], 0, HASH_BLOCK_SIZE - c->last.used);
            SHA256_ProcessBlock(c, &c->last.buf);
    
            memset(&c->last.buf[0], 0, HASH_BLOCK_SIZE - HASH_LEN_SIZE);
            c->last.used = 0;
    
            /* save length */
            //buf = (uint64_t *)&(c->last.buf[HASH_LEN_OFFSET]);
            //*buf = htobe64(c->total << 3);
            temp = (uint32_t *)&(c->last.buf[HASH_LEN_OFFSET]);
            temp[0] = htobe32((c->total << 3) >> 32 & 0xFFFFFFFF);
            temp[1] = htobe32((c->total << 3) & 0xFFFFFFFF);
    
            SHA256_ProcessBlock(c, &c->last.buf);
        }
        else /* 0 <= last.used < HASH_BLOCK_SIZE - HASH_LEN_SIZE */
        {
            c->total += c->last.used;
    
            /* one more block */
            c->last.buf[c->last.used] = HASH_PADDING_PATTERN;
            c->last.used++;
    
            /* padding 0s */
            memset(&c->last.buf[c->last.used], 0, HASH_BLOCK_SIZE - HASH_LEN_SIZE - c->last.used);
    
            /* save length */
            //buf = (uint64_t *)&(c->last.buf[HASH_LEN_OFFSET]);
            //*buf = htobe64(c->total << 3);
            temp = (uint32_t *)&(c->last.buf[HASH_LEN_OFFSET]);
            temp[0] = htobe32((c->total << 3) >> 32 & 0xFFFFFFFF);
            temp[1] = htobe32((c->total << 3) & 0xFFFFFFFF);
    
            SHA256_ProcessBlock(c, &c->last.buf);
        }
    
        temp = (uint32_t *)md;
        temp[0] = htobe32(c->hash.a);
        temp[1] = htobe32(c->hash.b);
        temp[2] = htobe32(c->hash.c);
        temp[3] = htobe32(c->hash.d);
        temp[4] = htobe32(c->hash.e);
        temp[5] = htobe32(c->hash.f);
        temp[6] = htobe32(c->hash.g);
        temp[7] = htobe32(c->hash.h);
    
        return ERR_OK;
    }
    
    unsigned char *SHA256(const unsigned char *d, size_t n, unsigned char *md)
    {
        SHA256_CTX c;
    
        if ((NULL == d) || (NULL == md))
        {
            return NULL;
        }
    
        SHA256_Init(&c);
        SHA256_Update(&c, d, n);
        SHA256_Final(md, &c);
    
        return md;
    }
    
    int SHA224_Init(SHA256_CTX *c)
    {
        if (NULL == c)
        {
            return ERR_INV_PARAM;
        }
    
        memset(c, 0, sizeof(SHA256_CTX));
    
        c->hash.a = 0xc1059ed8;
        c->hash.b = 0x367cd507;
        c->hash.c = 0x3070dd17;
        c->hash.d = 0xf70e5939;
        c->hash.e = 0xffc00b31;
        c->hash.f = 0x68581511;
        c->hash.g = 0x64f98fa7;
        c->hash.h = 0xbefa4fa4;
    
        return ERR_OK;
    }
    
    int SHA224_Update(SHA256_CTX *c, const void *data, size_t len)
    {
        return SHA256_Update(c, data, len);
    }
    
    int SHA224_Final(unsigned char *md, SHA256_CTX *c)
    {
        int rc = ERR_OK;
        unsigned char sha256_md[SHA256_DIGEST_SIZE];
    
        memset(&sha256_md, 0, sizeof(sha256_md));
    
        rc = SHA256_Final(sha256_md, c);
    
        memcpy(md, sha256_md, SHA224_DIGEST_SIZE);
    
        return rc;
    }
    
    unsigned char *SHA224(const unsigned char *d, size_t n, unsigned char *md)
    {
        SHA256_CTX c;
    
        if ((NULL == d) || (NULL == md))
        {
            return NULL;
        }
    
        SHA224_Init(&c);
        SHA224_Update(&c, d, n);
        SHA224_Final(md, &c);
    
        return md;
    }
    

    从上面的实现来看,SHA224和SHA256的主要区别在于:

    • SHA224_Init函数中,初始化变量不一致
    • SHA224_Final函数中,从基于SHA256得到的哈希中截取前面部分作为SHA224的哈希值

    SHA256源码的编译和测试

    我直接在Makefile中内置了一个test伪目标,编译时除了编译生成名为sha256的哈希工具外,还会直接调用内置的哈希测试。

    编译和运行如下:

    $ make
    gcc -Wall -g -O2 -c utils.c -o utils.o
    gcc -Wall -g -O2 -c sha256.c -o sha256.o
    gcc -Wall -g -O2 -c sha256test.c -o sha256test.o
    gcc -Wall -g -O2 utils.o sha256.o sha256test.o -o sha256
    
    Run Test...
    ./sha256 -a sha224 -x
    Internal hash tests for ./sha256(SHA224):
    ./sha256("")
      Expect: d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f
      Result: d14a028c2a3a2bc9476102bb288234c415a2b01f828ea62ac5b3e42f
    
    ./sha256("a")
      Expect: abd37534c7d9a2efb9465de931cd7055ffdb8879563ae98078d6d6d5
      Result: abd37534c7d9a2efb9465de931cd7055ffdb8879563ae98078d6d6d5
    
    ./sha256("abc")
      Expect: 23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7
      Result: 23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7
    
    ./sha256("message digest")
      Expect: 2cb21c83ae2f004de7e81c3c7019cbcb65b71ab656b22d6d0c39b8eb
      Result: 2cb21c83ae2f004de7e81c3c7019cbcb65b71ab656b22d6d0c39b8eb
    
    ./sha256("abcdefghijklmnopqrstuvwxyz")
      Expect: 45a5f72c39c5cff2522eb3429799e49e5f44b356ef926bcf390dccc2
      Result: 45a5f72c39c5cff2522eb3429799e49e5f44b356ef926bcf390dccc2
    
    ./sha256("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")
      Expect: bff72b4fcb7d75e5632900ac5f90d219e05e97a7bde72e740db393d9
      Result: bff72b4fcb7d75e5632900ac5f90d219e05e97a7bde72e740db393d9
    
    ./sha256("12345678901234567890123456789012345678901234567890123456789012345678901234567890")
      Expect: b50aecbe4e9bb0b57bc5f3ae760a8e01db24f203fb3cdcd13148046e
      Result: b50aecbe4e9bb0b57bc5f3ae760a8e01db24f203fb3cdcd13148046e
    
    ./sha256 -a sha256 -x
    Internal hash tests for ./sha256(SHA256):
    ./sha256("")
      Expect: e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
      Result: e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
    
    ./sha256("a")
      Expect: ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb
      Result: ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb
    
    ./sha256("abc")
      Expect: ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad
      Result: ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad
    
    ./sha256("message digest")
      Expect: f7846f55cf23e14eebeab5b4e1550cad5b509e3348fbc4efa3a1413d393cb650
      Result: f7846f55cf23e14eebeab5b4e1550cad5b509e3348fbc4efa3a1413d393cb650
    
    ./sha256("abcdefghijklmnopqrstuvwxyz")
      Expect: 71c480df93d6ae2f1efad1447c66c9525e316218cf51fc8d9ed832f2daf18b73
      Result: 71c480df93d6ae2f1efad1447c66c9525e316218cf51fc8d9ed832f2daf18b73
    
    ./sha256("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")
      Expect: db4bfcbd4da0cd85a60c3c37d3fbd8805c77f15fc6b1fdfe614ee0a7c8fdb4c0
      Result: db4bfcbd4da0cd85a60c3c37d3fbd8805c77f15fc6b1fdfe614ee0a7c8fdb4c0
    
    ./sha256("12345678901234567890123456789012345678901234567890123456789012345678901234567890")
      Expect: f371bc4a311f2b009eef952dd83ca80e2b60026c8e935592d0f9c308453c813e
      Result: f371bc4a311f2b009eef952dd83ca80e2b60026c8e935592d0f9c308453c813e
    

    最新版本的openssl工具已经支持sm3哈希算法,因此可以将sm3工具和openssl执行dgst计算的结果进行比较:

    $ sha256 -h
    Usage:
    Common options: [-x|-f file|-s string| -a sha224|sha256 | -h]
    Hash a string:
            sha256 -a sha224|sha256 -s string
    Hash a file:
            sha256 -a sha224|sha256 -f file [-k key]
    -a      Secure hash algorithm: "sha224", "sha256"
    -x      Internal string hash test
    -h      Display this message
    
    #
    # 计算SHA224哈希
    #
    
    # 使用"-f"和"-s"选项分别对文件和字符串计算sha224哈希值
    $ sha256 -a sha224 -f sha256.o
    sha256(sha256.o) = 3ab679330b579d31c032c29d648d873ee95b03e0be085640e468fba9
    $ sha256 -a sha224 -s "I Love China!"
    sha256("I Love China!") = ea5e741f52612d3897304d6a70c146a7fa21b965ed28739f091396e8
    
    # 使用开源的openssl工具计算相应的哈希进行对比
    $ openssl dgst -sha224 sha256.o
    SHA224(sha256.o)= 3ab679330b579d31c032c29d648d873ee95b03e0be085640e468fba9
    $ echo -n "I Love China!" | openssl dgst -sha224
    (stdin)= ea5e741f52612d3897304d6a70c146a7fa21b965ed28739f091396e8
    
    #
    # 计算SHA256哈希
    #
    
    # 使用"-f"和"-s"选项分别对文件和字符串计算sha256哈希值
    $ sha256 -a sha256 -f sha256.o
    sha256(sha256.o) = 08b4685632df74d8fc765fa70ccc4ab9763c9e4a6a69b3c5a53f73173122bac5
    $ sha256 -a sha256 -s "I Love China!"
    sha256("I Love China!") = 91c906339dbb1f46cfcb2a24dfe5bc445752a84fc04a8474b4260fd8bb679129
    
    # 使用开源的openssl工具计算相应的哈希进行对比
    $ openssl dgst -sha256 sha256.o
    SHA256(sha256.o)= 08b4685632df74d8fc765fa70ccc4ab9763c9e4a6a69b3c5a53f73173122bac5
    $ echo -n "I Love China!" | openssl dgst -sha256
    (stdin)= 91c906339dbb1f46cfcb2a24dfe5bc445752a84fc04a8474b4260fd8bb679129
    

    完整代码

    完整的代码文件列表如下:

    sha256$ ls -lh
    total 52K
    -rwxr--r-- 1 rocky rocky  649 Jun 20 15:47 Makefile
    -rwxrwxr-x 1 rocky rocky  12K Jun 20 17:06 sha256.c
    -rwxrwxr-x 1 rocky rocky 1.6K Jun 20 17:04 sha256.h
    -rwxrwxr-x 1 rocky rocky  12K Jun 20 16:49 sha256test.c
    -rwxr--r-- 1 rocky rocky 8.1K Jun 20 11:04 sha256test-new.c
    -rwxr--r-- 1 rocky rocky  758 Jun 20 17:11 utils.c
    -rwxr--r-- 1 rocky rocky 1.8K Jun 20 17:11 utils.h
    

    需要代码请访问:

    • https://github.com/guyongqiangx/cryptography/

    其它

    洛奇工作中常常会遇到自己不熟悉的问题,这些问题可能并不难,但因为不了解,找不到人帮忙而瞎折腾,往往导致浪费几天甚至更久的时间。

    所以我组建了几个微信讨论群(记得微信我说加哪个群,如何加微信见后面),欢迎一起讨论:

    • 一个密码编码学讨论组,主要讨论各种加解密,签名校验等算法,请说明加密码学讨论群。
    • 一个Android OTA的讨论组,请说明加Android OTA群。
    • 一个git和repo的讨论组,请说明加git和repo群。

    在工作之余,洛奇尽量写一些对大家有用的东西,如果洛奇的这篇文章让您有所收获,解决了您一直以来未能解决的问题,不妨赞赏一下洛奇,这也是对洛奇付出的最大鼓励。扫下面的二维码赞赏洛奇,金额随意:

    收钱码

    洛奇自己维护了一个公众号“洛奇看世界”,一个很佛系的公众号,不定期瞎逼逼。公号也提供个人联系方式,一些资源,说不定会有意外的收获,详细内容见公号提示。扫下方二维码关注公众号:

    公众号

    展开全文
  • 该楼层疑似违规已被系统折叠隐藏此楼查看此楼#include #include #define SHA256_ROTL(a,b) (((a>>(32-b))&(0x7fffffff>>(31-b)))|(a<#define SHA256_SR(a,b) ((a>>b)&(0x7fffffff>&...

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

    #include

    #include

    #define SHA256_ROTL(a,b) (((a>>(32-b))&(0x7fffffff>>(31-b)))|(a<

    #define SHA256_SR(a,b) ((a>>b)&(0x7fffffff>>(b-1)))

    #define SHA256_Ch(x,y,z) ((x&y)^((~x)&z))

    #define SHA256_Maj(x,y,z) ((x&y)^(x&z)^(y&z))

    #define SHA256_E0(x) (SHA256_ROTL(x,30)^SHA256_ROTL(x,19)^SHA256_ROTL(x,10))

    #define SHA256_E1(x) (SHA256_ROTL(x,26)^SHA256_ROTL(x,21)^SHA256_ROTL(x,7))

    #define SHA256_O0(x) (SHA256_ROTL(x,25)^SHA256_ROTL(x,14)^SHA256_SR(x,3))

    #define SHA256_O1(x) (SHA256_ROTL(x,15)^SHA256_ROTL(x,13)^SHA256_SR(x,10))

    extern char* StrSHA256(const char* str, long long length, char* sha256){

    /*

    计算字符串SHA-256

    参数说明:

    str 字符串指针

    length 字符串长度

    sha256 用于保存SHA-256的字符串指针

    返回值为参数sha256

    */

    char *pp, *ppend;

    long l, i, W[64], T1, T2, A, B, C, D, E, F, G, H, H0, H1, H2, H3, H4, H5, H6, H7;

    H0 = 0x6a09e667, H1 = 0xbb67ae85, H2 = 0x3c6ef372, H3 = 0xa54ff53a;

    H4 = 0x510e527f, H5 = 0x9b05688c, H6 = 0x1f83d9ab, H7 = 0x5be0cd19;

    long K[64] = {

    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,

    };

    l = length + ((length % 64 >= 56) ? (128 - length % 64) : (64 - length % 64));

    if (!(pp = (char*)malloc((unsigned long)l))) return 0;

    for (i = 0; i < length; pp[i + 3 - 2 * (i % 4)] = str[i], i++);

    for (pp[i + 3 - 2 * (i % 4)] = 128, i++; i < l; pp[i + 3 - 2 * (i % 4)] = 0, i++);

    *((long*)(pp + l - 4)) = length << 3;

    *((long*)(pp + l - 8)) = length >> 29;

    for (ppend = pp + l; pp < ppend; pp += 64){

    for (i = 0; i < 16; W[i] = ((long*)pp)[i], i++);

    for (i = 16; i < 64; W[i] = (SHA256_O1(W[i - 2]) + W[i - 7] + SHA256_O0(W[i - 15]) + W[i - 16]), i++);

    A = H0, B = H1, C = H2, D = H3, E = H4, F = H5, G = H6, H = H7;

    for (i = 0; i < 64; i++){

    T1 = H + SHA256_E1(E) + SHA256_Ch(E, F, G) + K[i] + W[i];

    T2 = SHA256_E0(A) + SHA256_Maj(A, B, C);

    H = G, G = F, F = E, E = D + T1, D = C, C = B, B = A, A = T1 + T2;

    }

    H0 += A, H1 += B, H2 += C, H3 += D, H4 += E, H5 += F, H6 += G, H7 += H;

    }

    free(pp - l);

    sprintf(sha256, "%08X%08X%08X%08X%08X%08X%08X%08X", H0, H1, H2, H3, H4, H5, H6, H7);

    return sha256;

    }

    extern char* FileSHA256(const char* file, char* sha256){

    /*

    计算文件SHA-256

    参数说明:

    file 文件路径字符串指针

    sha256 用于保存SHA-256的字符串指针

    返回值为参数sha256

    */

    FILE* fh;

    char* addlp, T[64];

    long addlsize, j, W[64], T1, T2, A, B, C, D, E, F, G, H, H0, H1, H2, H3, H4, H5, H6, H7;

    long long length, i, cpys;

    void *pp, *ppend;

    H0 = 0x6a09e667, H1 = 0xbb67ae85, H2 = 0x3c6ef372, H3 = 0xa54ff53a;

    H4 = 0x510e527f, H5 = 0x9b05688c, H6 = 0x1f83d9ab, H7 = 0x5be0cd19;

    long K[64] = {

    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,

    };

    fh = fopen(file, "rb");

    fseek(fh, 0, SEEK_END);

    length = ftello64(fh);

    addlsize = (56 - length % 64 > 0) ? (64) : (128);

    if (!(addlp = (char*)malloc(addlsize))) return 0;

    cpys = ((length - (56 - length % 64)) > 0) ? (length - length % 64) : (0);

    j = (long)(length - cpys);

    if (!(pp = (char*)malloc(j))) return 0;

    fseek(fh, -j, SEEK_END);

    fread(pp, 1, j, fh);

    for (i = 0; i < j; addlp[i + 3 - 2 * (i % 4)] = ((char*)pp)[i], i++);

    free(pp);

    for (addlp[i + 3 - 2 * (i % 4)] = 128, i++; i < addlsize; addlp[i + 3 - 2 * (i % 4)] = 0, i++);

    *((long*)(addlp + addlsize - 4)) = length << 3;

    *((long*)(addlp + addlsize - 8)) = length >> 29;

    for (rewind(fh); 64 == fread(W, 1, 64, fh);){

    for (i = 0; i < 64; T[i + 3 - 2 * (i % 4)] = ((char*)W)[i], i++);

    for (i = 0; i < 16; W[i] = ((long*)T)[i], i++);

    for (i = 16; i < 64; W[i] = (SHA256_O1(W[i - 2]) + W[i - 7] + SHA256_O0(W[i - 15]) + W[i - 16]), i++);

    A = H0, B = H1, C = H2, D = H3, E = H4, F = H5, G = H6, H = H7;

    for (i = 0; i < 64; i++){

    T1 = H + SHA256_E1(E) + SHA256_Ch(E, F, G) + K[i] + W[i];

    T2 = SHA256_E0(A) + SHA256_Maj(A, B, C);

    H = G, G = F, F = E, E = D + T1, D = C, C = B, B = A, A = T1 + T2;

    }

    H0 += A, H1 += B, H2 += C, H3 += D, H4 += E, H5 += F, H6 += G, H7 += H;

    }

    for (pp = addlp, ppend = addlp + addlsize; pp < ppend; pp = (long*)pp + 16){

    for (i = 0; i < 16; W[i] = ((long*)pp)[i], i++);

    for (i = 16; i < 64; W[i] = (SHA256_O1(W[i - 2]) + W[i - 7] + SHA256_O0(W[i - 15]) + W[i - 16]), i++);

    A = H0, B = H1, C = H2, D = H3, E = H4, F = H5, G = H6, H = H7;

    for (i = 0; i < 64; i++){

    T1 = H + SHA256_E1(E) + SHA256_Ch(E, F, G) + K[i] + W[i];

    T2 = SHA256_E0(A) + SHA256_Maj(A, B, C);

    H = G, G = F, F = E, E = D + T1, D = C, C = B, B = A, A = T1 + T2;

    }

    H0 += A, H1 += B, H2 += C, H3 += D, H4 += E, H5 += F, H6 += G, H7 += H;

    }

    free(addlp); fclose(fh);

    sprintf(sha256, "%08X%08X%08X%08X%08X%08X%08X%08X", H0, H1, H2, H3, H4, H5, H6, H7);

    return sha256;

    }

    int main(void){

    char text[] = "hik12345+";

    //char text[ ];

    //scanf("%s",&text);

    char sha256[65];

    StrSHA256(text,sizeof(text)-1,sha256); // sizeof()计算的结果包含了末尾的'\0'应减1

    puts(sha256);

    puts(StrSHA256(text,sizeof(text)-1,sha256)); // 函数返回值即sha256,直接输出也可以

    return 0;

    }

    展开全文
  • SHA256 加密 VB源码,实测通过,注意:目前不支持中文
  • 易语言写的HMAC_SHA1和HMAC_SHA256算法。使用了脚本对象
  • SHA256 和 HMAC-SHA256 的C语言实现,只有一个文件,无库依赖。使用方法见源码底部注释说明
  • 支付宝签名,SHA256WithRsa签名,SHA1WithRSA签名, delph所有源码,有需要的朋友绝对值这个分数!
  • sha 256,亲测可用,使用源码,未使用ssl库,使用VS2015软件, 可以作为作业参考或研究参考
  • C# SHA256 PKCS#7 生成验名、验签源码 中行支付.
  • SHA256哈希算法VB源码

    2018-12-15 21:39:42
    VB6.0语言,SHA256哈希算法源码,做成函数,可以直接调用!!
  • $ meteor add jparker:crypto-sha256 CryptoJS命名空间下的以下方法现在将在客户端和服务器上可用: CryptoJS.SHA256(message) 如果还安装了 ,也可以使用以下方法: CryptoJS.HmacSHA256(message, key) 用法 ...
  • C语言实现SHA256算法

    2021-05-16 02:53:42
    sha256.h#ifndef sha256_h#define sha256_h#include void sha256(const unsigned char *data, size_t len, unsigned char *out);#endif /* sha256_h */sha256.c// sha256.c#include #include #include #include "sha...

    sha256.h

    #ifndef sha256_h

    #define sha256_h

    #include

    void sha256(const unsigned char *data, size_t len, unsigned char *out);

    #endif /* sha256_h */

    sha256.c

    // sha256.c

    #include

    #include

    #include

    #include "sha256.h"

    #define rightrotate(w, n) ((w >> n) | (w) << (32-(n)))

    #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__

    #define copy_uint32(p, val) *((uint32_t *)p) = __builtin_bswap32((val))

    #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__

    #define copy_uint32(p, val) *((uint32_t *)p) = (val)

    #else

    #error "Unsupported target architecture endianess!"

    #endif

    static const uint32_t k[64] = {

    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

    };

    void sha256(const unsigned char *data, size_t len, unsigned char *out) {

    uint32_t h0 = 0x6a09e667;

    uint32_t h1 = 0xbb67ae85;

    uint32_t h2 = 0x3c6ef372;

    uint32_t h3 = 0xa54ff53a;

    uint32_t h4 = 0x510e527f;

    uint32_t h5 = 0x9b05688c;

    uint32_t h6 = 0x1f83d9ab;

    uint32_t h7 = 0x5be0cd19;

    int r = (int)(len * 8 % 512);

    int append = ((r < 448) ? (448 - r) : (448 + 512 - r)) / 8;

    size_t new_len = len + append + 8;

    unsigned char buf[new_len];

    bzero(buf + len, append);

    if (len > 0) {

    memcpy(buf, data, len);

    }

    buf[len] = (unsigned char)0x80;

    uint64_t bits_len = len * 8;

    for (int i = 0; i < 8; i++) {

    buf[len + append + i] = (bits_len >> ((7 - i) * 8)) & 0xff;

    }

    uint32_t w[64];

    bzero(w, 64);

    size_t chunk_len = new_len / 64;

    for (int idx = 0; idx < chunk_len; idx++) {

    uint32_t val = 0;

    for (int i = 0; i < 64; i++) {

    val = val | (*(buf + idx * 64 + i) << (8 * (3 - i)));

    if (i % 4 == 3) {

    w[i / 4] = val;

    val = 0;

    }

    }

    for (int i = 16; i < 64; i++) {

    uint32_t s0 = rightrotate(w[i - 15], 7) ^ rightrotate(w[i - 15], 18) ^ (w[i - 15] >> 3);

    uint32_t s1 = rightrotate(w[i - 2], 17) ^ rightrotate(w[i - 2], 19) ^ (w[i - 2] >> 10);

    w[i] = w[i - 16] + s0 + w[i - 7] + s1;

    }

    uint32_t a = h0;

    uint32_t b = h1;

    uint32_t c = h2;

    uint32_t d = h3;

    uint32_t e = h4;

    uint32_t f = h5;

    uint32_t g = h6;

    uint32_t h = h7;

    for (int i = 0; i < 64; i++) {

    uint32_t s_1 = rightrotate(e, 6) ^ rightrotate(e, 11) ^ rightrotate(e, 25);

    uint32_t ch = (e & f) ^ (~e & g);

    uint32_t temp1 = h + s_1 + ch + k[i] + w[i];

    uint32_t s_0 = rightrotate(a, 2) ^ rightrotate(a, 13) ^ rightrotate(a, 22);

    uint32_t maj = (a & b) ^ (a & c) ^ (b & c);

    uint32_t temp2 = s_0 + maj;

    h = g;

    g = f;

    f = e;

    e = d + temp1;

    d = c;

    c = b;

    b = a;

    a = temp1 + temp2;

    }

    h0 += a;

    h1 += b;

    h2 += c;

    h3 += d;

    h4 += e;

    h5 += f;

    h6 += g;

    h7 += h;

    }

    copy_uint32(out, h0);

    copy_uint32(out + 4, h1);

    copy_uint32(out + 8, h2);

    copy_uint32(out + 12, h3);

    copy_uint32(out + 16, h4);

    copy_uint32(out + 20, h5);

    copy_uint32(out + 24, h6);

    copy_uint32(out + 28, h7);

    }

    相关文章

    展开全文
  • VB6_SHA256

    2019-02-22 22:29:44
    VB6.0语言,SHA256哈希算法源码,做成函数,可以直接调用!! 区别于之前上传的“SHA256哈希算法VB源码”: “SHA256哈希算法VB源码”使用的是sha256算法详细规则计算的,较长; 本函数是直接调用SHA256的类算法,较...
  • TI SHA_256 源码及例程

    2014-07-25 16:38:11
    TI官方C语言SHA_256源码及应用例程,亲测可用,推荐下载~
  • Linux C SHA256 (内核态)

    千次阅读 2021-11-23 14:11:03
    Linux C/C++算法实现 SHA256
  • sha256加密,文件中包含sha256.js、sha256.java、包含jar包。工具类、调用方法。
  • 【安全算法之SHA256SHA256摘要运算的C语言源码实现概述头文件定义C语言版本的实现源码测试用例github仓库更多参考链接 概述 大家都知道摘要算法在安全领域,也是一个特别重要的存在,而SHA256是其中最常见的一种...
  • 这是小沃最近遇到的一个项目,由于百度云的所有接口都需要通过hmac sha256计算,所以小沃在网上找到了相关代码可以在单片机中实现,现在就分享给大家。SHA256.h#ifndefSHA256_H#defineSHA256_H#includetypedefstruct...
  • python实现SHA256

    千次阅读 2021-07-17 09:41:36
    from hashlib import sha256 import hmac def get_sign(key, data): #sha256加密有2种 # hsobj = sha256(key.encode("utf-8")) # hsobj.update(data.encode("utf-8")) # print(hsobj.hexdigest().upper()) data = ...
  • openssl——sha256算法源码

    千次阅读 2017-02-26 20:35:46
    openssl中关于sha256算法最关键的代码文件有sha.h, sha256.c,md32_common.h,crypto.h等等。 1、sha256算法最关键的文件sha256.c。 查看这个文件可以看到openssl如何计算sha256值:unsigned char *SHA256(const ...
  • 腾讯云·短信·TC3-HMAC-SHA256API3.0签名V3算法带发送例子
  • CryptoJS 完整源码

    2018-07-19 10:03:38
    包含components和rollups代码,还有crypto-js.js文件,使用require.js有用到
  • sha256sum c语言代码

    2014-09-28 16:21:15
    linux c语言代码 sha256sum
  • SHA512源码

    2017-06-22 22:52:01
    SHA512源码,通过编译测试
  • sha256算法实现代码

    2020-10-14 12:30:16
    sha256实现 依次执行 SHA256Init、 SHA256Update SHA256Final 三个函数即可得到sha256结果

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 42,278
精华内容 16,911
关键字:

sha256 源码