【stun系列04】hmac_sha1加密算法的C/C++代码
HMAC运算利用哈希算法,以一个密钥和一个消息为输入,生成一个消息摘要作为输出。HMACSHA1 接受任何大小的密钥,并产生长度为 160 位(20字节)的哈希序列。
linux平台代码:
依赖openssl库,先安装openssl依赖库:
yum install openssl openssl-devel -y
检查/usr/local/下是否有openssl文件夹
加密C代码:
test.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | #include <stdio.h> #include <string.h> #include <openssl/hmac.h> int main( int argc, void **argv) { //key value const char key[] = "745s295z8lv458ll46w2467ta460562n" ; //data in hex, /or you can init with text unsigned char data[] = { 0x00,0x01,0x00,0x48,0x21,0x12,0xa4,0x42,0x78,0x2b,0x66,0x6b,0x32,0x34,0x30,0x6b ,0x4e,0x51,0x6a,0x56,0x00,0x06,0x00,0x0d,0x36,0x37,0x76,0x32,0x37,0x30,0x37,0x35 ,0x3a,0x31,0x33,0x42,0x5a,0x00,0x00,0x00,0xc0,0x57,0x00,0x04,0x00,0x02,0x00,0x00 ,0x80,0x2a,0x00,0x08,0x70,0xfb,0xe5,0x05,0x91,0xbf,0x83,0x3c,0x00,0x24,0x00,0x04 ,0x6e,0x7e,0x1e,0xff }; unsigned char result[20] = {0}; int resultlen = 20; 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, sizeof (data)); HMAC_Final(&ctx, result, &resultlen); HMAC_CTX_cleanup(&ctx); //print the result in hex format for ( int i = 0; i < resultlen; i++) printf ( "%02x" , result[i]); printf ( "\n" ); return 0; } |
1 2 3 4 5 6 7 | /* 1.1.1m版本 HMAC_CTX *ctx = HMAC_CTX_new(); HMAC_Init_ex(ctx, passwd, passwdlen, EVP_sha1(), NULL); HMAC_Update(ctx, data, datalen); HMAC_Final(ctx, dest, destlen); HMAC_CTX_free(ctx); */ |
linux使用命令gcc编译,需要依赖libcrypto,编译命令如下:
1 | gcc -o test test .c -lssl -lcrypto |
编译后执行./test即可看到结果:
c7c49a60946fa024e5f6212ec63814fe2e76b26d
结果是否正确可以根据系列03或者其中的网址进行二次校验
windows平台代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | #include <stdio.h> #include <openssl/ossl_typ.h> #include <openssl/hmac.h> #include <string.h> void hmac_sha1( char * passwd, int passwdlen, unsigned char * data, short datalen, unsigned char * dest, unsigned int *destlen) { //use hmac sha1 to encode data with remote passwd. HMAC_CTX *ctx = HMAC_CTX_new(); HMAC_Init_ex(ctx, passwd, passwdlen, EVP_sha1(), NULL); HMAC_Update(ctx, data, datalen); HMAC_Final(ctx, dest, destlen); HMAC_CTX_free(ctx); } int main( int argc, char **argv) { unsigned char data[] = { 0x00,0x01,0x00,0x48,0x21,0x12,0xa4,0x42,0x78,0x2b,0x66,0x6b,0x32,0x34,0x30,0x6b ,0x4e,0x51,0x6a,0x56,0x00,0x06,0x00,0x0d,0x36,0x37,0x76,0x32,0x37,0x30,0x37,0x35 ,0x3a,0x31,0x33,0x42,0x5a,0x00,0x00,0x00,0xc0,0x57,0x00,0x04,0x00,0x02,0x00,0x00 ,0x80,0x2a,0x00,0x08,0x70,0xfb,0xe5,0x05,0x91,0xbf,0x83,0x3c,0x00,0x24,0x00,0x04 ,0x6e,0x7e,0x1e,0xff }; int datalen = sizeof (data); char passwd[] = "745s295z8lv458ll46w2467ta460562n" ; int passwdlen = strlen (passwd); unsigned char result[20] = { 0 }; unsigned int resultlen = 0; hmac_sha1(passwd, passwdlen, data, datalen, result, &resultlen); for ( int i = 0; i < resultlen; i++) printf ( "%02x" , result[i]); printf ( "\n" ); return 0; } |
windows需要安装openssl,需要编译x86时选择对应32位版本的openssl安装,否则会出错
将openssl的include和lib库路径填入工程,并添加libcrypto.lib库,编译即可。运行结果如下:
分类:
stun协议与代码实现
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现