hmac-sha1加密算法C源码示例

HMAC: Hash-based Message Authentication Code,即基于Hash的消息鉴别码 

在各大开放平台大行其道的互联网开发潮流中,调用各平台的API接口过程中,无一例外都会用到计算签名值(sig值)。而在各种计算签名的方法中,经常被采用的就是HMAC-SHA1,现对HMAC-SHA1做一个简单的介绍:

HMAC,散列消息鉴别码,基于密钥的Hash算法认证协议。

实现原理为:

利用已经公开的Hash函数和私有的密钥,来生成固定长度的消息鉴别码;

SHA1、MD5等Hash算法是比较常用的不可逆Hash签名计算方法;

BASE64,将任意序列的8字节字符转换为人眼无法直接识别的符号编码的一种方法;

相关依赖库是openssl,安装方法如下:
apt-get install openssl   //Ubuntu 14.04
yum -y install openssl-devel    //centos 

下面提供两个源码示例:

例子一:

//gcc -g hmac_sha1_demo3.c -o hmac_sha1_demo3 -lcrypto -std=c99

#include <stdio.h>
#include <string.h>
#include <openssl/hmac.h>

int main()
{
    // The key to hash
    char key[] = "012345678";

    // The data that we're going to hash using HMAC
    char data[] = "hello world";

    unsigned char digest[EVP_MAX_MD_SIZE] = {'\0'};
    unsigned int digest_len = 0;

    // Using sha1 hash engine here.
    // You may use other hash engines. e.g EVP_md5(), EVP_sha224, EVP_sha512, etc
    HMAC(EVP_sha1(), key, strlen(key), (unsigned char*)data, strlen(data), digest, &digest_len);
    printf("%s, len %u\n", digest, digest_len);

    // Be careful of the length of string with the choosen hash engine. SHA1 produces a 20-byte hash value which rendered as 40 characters.
    // Change the length accordingly with your choosen hash engine
    char mdString[41] = {'\0'};
    for(int i = 0; i < 20; i++)
         sprintf(&mdString[i*2], "%02x", (unsigned int)digest[i]);

    printf("HMAC digest: %s\n", mdString);

    return 0;
}

例子二:

//gcc -g hmac_sha1_demo2.c -o hmac_sha1_demo2 -lcrypto --std=c99

#include <stdio.h>
#include <string.h>
#include <openssl/hmac.h>

int main() {
    // The secret key for hashing
    const char key[] = "012345678";

    // The data that we're going to hash
    char data[] = "hello world";

    // Be careful of the length of string with the choosen hash engine. SHA1 needed 20 characters.
    // Change the length accordingly with your choosen hash engine.
    unsigned char* result;
    unsigned int len = 20;

    result = (unsigned char*)malloc(sizeof(char) * len);

    HMAC_CTX ctx;
    HMAC_CTX_init(&ctx);

    // Using sha1 hash engine here.
    // You may use other hash engines. e.g EVP_md5(), EVP_sha224, EVP_sha512, etc
    HMAC_Init_ex(&ctx, key, strlen(key), EVP_sha1(), NULL);
    HMAC_Update(&ctx, (unsigned char*)&data, strlen(data));
    HMAC_Final(&ctx, result, &len);
    HMAC_CTX_cleanup(&ctx);

    printf("HMAC digest: ");

    for (int i = 0; i != len; i++)
        printf("%02x", (unsigned int)result[i]);

    printf("\n");

    free(result);

    return 0;
}
运行截图:


参考文献:

[1].http://www.askyb.com/cpp/openssl-hmac-hasing-example-in-cpp/ 

易语言的Base64_hmac_sha1加密算法是一个结合了HMAC(基于密钥的认证码)和SHA-1哈希函数的编码过程,通常用于数据的安全传输中。由于易语言是一门中文编程语言,它的源码可能会比较直观和简洁,但具体实现细节会涉及到易语言特有的语法。 以下是一个简化的示例,展示了如何在易语言中使用Base64编码和HMAC-SHA1进行加密: ```elescript -- 假设我们已经有了一个key和需要加密的data local key = "your_secret_key" local data = "your_data_to_encrypt" -- 加载Base64和HMAC-SHA1库(如果有内置的话) LoadLib("System.Security.Cryptography") -- 创建HMAC-SHA1对象 local hmac = NewObject("System.Security.Cryptography.HMACSHA1", key) -- 将数据转换为字节数组 local dataBytes = ToByteArray(data) -- 计算HMAC-SHA1哈希值 local hashBytes = hmac.ComputeHash(dataBytes) -- 将哈希值转换为Base64字符串 local hashBase64 = ToBase64String(hashBytes) -- 这就是加密后的结果 echo "Encrypted data: " & hashBase64 -- 当然,这只是一个简化版本,实际代码可能还需要处理错误和输入验证 -- -- 相关问题 -- -- 1. 易语言中的HMACSHA1类是如何工作的? -- 2. Base64编码在这里的作用是什么? -- 3. 如果我想要解密,我需要哪些信息? ``` 请注意,上述代码是假设易语言提供了一套支持这些加密操作的库。如果你直接用易语言写这个算法,代码可能会有所不同,因为易语言的API接口和标准库功能可能会有差异。若需查看完整的源码,你需要查阅易语言官方文档或相关的社区资源。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值