• 大小: 7KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-05-13
  • 语言: C/C++
  • 标签: HMAC  SHA256  C语言  

资源简介

本文档为HMAC-SHA256的C语言实现代码,本人亲测结果与标准加密结果一致,可供学习HMAC,SHA-256安全算法相关的人员参考

资源截图

代码片段和文件信息

/********************************************************************
  P-CODE:
    function hmac (key message) {
        if (length(key) > blocksize) {
            key = hash(key) // keys longer than blocksize are shortened
        }
        if (length(key) < blocksize) {
            // keys shorter than blocksize are zero-padded (where ∥ is concatenation)
            key = key ∥ [0x00 * (blocksize - length(key))] // Where * is repetition.
        }

        o_key_pad = [0x5c * blocksize] ⊕ key // Where blocksize is that of the underlying hash function
        i_key_pad = [0x36 * blocksize] ⊕ key // Where ⊕ is exclusive or (XOR)

        return hash(o_key_pad ∥ hash(i_key_pad ∥ message)) // Where ∥ is concatenation
    }
********************************************************************/

#include 
#include “hmac_sha256.h“
#include “sha256.h“

/* 常量 */
#define B 64                 // 明文分块长度 byte
#define I_PAD 0x36             // 内部填充
#define O_PAD 0x5C             // 外部填充
#define KEY_IOPAD_SIZE 64       // 填充字串长度 byte
#define SHA256_DIGEST_SIZE 32 // SHA-256摘要输出大小 32-byte

/* 带符号整数类型 */
typedef   signed          char int8_t;
typedef   signed short     int int16_t;
typedef   signed           int int32_t;

/* 无符号整数类型 */
typedef unsigned          char uint8_t;
typedef unsigned short     int uint16_t;
typedef unsigned           int uint32_t;


/********************************************************************
 * 将无符号 4-bit 二进制数据表示为十六进制形式.
 * 如 {1010} 将转换为 {‘A‘}.
 *
 * 参数说明:
 *  hb:         32-byte 摘要信息
 * 返回值:
 *  以 ASCII码 表示的十六进制数 [0-9A-F]
 ********************************************************************/
char bin2hex(unsigned char hb)
{
    hb = hb & 0xF;
    return (char)(hb < 10 ? ‘0‘ + hb : hb - 10 + ‘A‘);
}

/********************************************************************
 * 将 256-bit 摘要信息 转换为 64位 char数组 每位字符表示一个十六进制数.
 * 如 {0x5C 0x5C ...  0x5C} 将转化为 {‘5‘ ‘C‘ ‘5‘ ‘C‘ ...  ‘5‘ ‘C‘}.
 *
 * 参数说明:
 *  digest:     32-byte 摘要信息
 *  digest_hex: 出参 转换后的 64位 字符 每位表示一个十六进制数 以 ASCII码 形式存储
 ********************************************************************/
void digest2hex(unsigned char digest[HMAC_SHA256_DIGEST_SIZE] 
    char digest_hex[HMAC_SHA256_DIGEST_SIZE*2])
{
    for (int i = 0; i < HMAC_SHA256_DIGEST_SIZE; i++) {
        digest_hex[i * 2] = bin2hex(digest[i] >> 4);
        digest_hex[i * 2 + 1] = bin2hex(digest[i]);
    }
}

/********************************************************************
 * HMAC-SHA256 摘要生成算法:
 *  HMAC-SHA256(K M) == SHA256((K ^ opad) ∥ SHA256((K ^ ipad) ∥ M))
 *     K:      密钥
 *      M:      消息
 *     ipad:   内部填充字符串
 *     opad:   外部填充字符串
 *      ∥:      数据连接操作
 *
 * 函数参数:
 *      digest: 出参 输出 32-byte 加密结果
 *     msg:    要加密的数据
 *      msgLen: 数据长度
 *      key:    加密因子
 *      keyLen: 加密因子长度
 ********************************************************************/
void hmac_sha256(unsigned ch

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件       2283  2020-03-10 22:43  sha256.h

     文件       2503  2020-03-12 08:28  Test.cpp

     文件       5191  2020-03-12 05:43  hmac_sha256.c

     文件       1395  2020-03-12 05:44  hmac_sha256.h

     文件       9604  2020-03-12 03:30  sha256.c

----------- ---------  ---------- -----  ----

                20976                    5


评论

共有 条评论