mbedtls学习(2)单项散列函数

单项散列函数又称安全散列函数或者哈希函数,是根据消息内容算出散列值,散列值又称为消息摘要。
单项散列函数实现有下面2种方式

  • MD算法家族
    -SHA算法家族
    在这里插入图片描述

mbedtls所支持的单项散列算法在接口文件如下

  • MD2 include/mbedtls/md2.h
  • MD4 include/mbedtls/md4.h
  • MD5 include/mbedtls/md5.h
  • SHA1 include/mbedtls/sha1.h
  • SHA224 include/mbedtls/sha256.h
  • SHA245 include/mbedtls/sha256.h
  • SHA384 include/mbedtls/sha512.h
  • SHA512 include/mbedtls/sha512.h

include/mbedtls/md.h 是md通用接口文件,把是md算法和sha算法的抽象

下面使用md通用接口采用SHA256计算摘要
注意要打开如下宏

  • MBEDTLS_MD_C 使能md通用接口
  • MBEDTLS_SHA256_C 使能SHA256算法
#include <stdio.h>
#include <string.h>
#include <stdint.h>

#include “mbedtls/md.h”
#include “mbedtls/platform.h”

static void dump_buf(char info, uint8_t buf, uint32_t len)
{
mbedtls_printf(“%s”, info);
for (int i = 0; i < len; i++) {
mbedtls_printf(“%s%02X%s”, i % 16 0 ? “\n\t”:" ",
buf[i], i len - 1 ? “\n”:“”);
}
mbedtls_printf(“\n”);
}

int main(void)
{
uint8_t digest[32];
char msg1 = “abc”;
char msg2 = “def”;
mbedtls_md_context_t ctx;
const mbedtls_md_info_t *info;

<span class="token function">mbedtls_md_init</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>ctx<span class="token punctuation">)</span><span class="token punctuation">;</span>
info <span class="token operator">=</span> <span class="token function">mbedtls_md_info_from_type</span><span class="token punctuation">(</span>MBEDTLS_MD_SHA256<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token function">mbedtls_md_setup</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>ctx<span class="token punctuation">,</span> info<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">mbedtls_printf</span><span class="token punctuation">(</span><span class="token string">"\n  md info setup, name: %s, digest size: %d\n"</span><span class="token punctuation">,</span> 
               <span class="token function">mbedtls_md_get_name</span><span class="token punctuation">(</span>info<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token function">mbedtls_md_get_size</span><span class="token punctuation">(</span>info<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token function">mbedtls_md_starts</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>ctx<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">mbedtls_md_update</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>ctx<span class="token punctuation">,</span> msg1<span class="token punctuation">,</span> <span class="token function">strlen</span><span class="token punctuation">(</span>msg1<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">mbedtls_md_update</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>ctx<span class="token punctuation">,</span> msg2<span class="token punctuation">,</span> <span class="token function">strlen</span><span class="token punctuation">(</span>msg2<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token function">mbedtls_md_finish</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>ctx<span class="token punctuation">,</span> digest<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token function">dump_buf</span><span class="token punctuation">(</span><span class="token string">"\n  md sha-256 digest:"</span><span class="token punctuation">,</span> digest<span class="token punctuation">,</span> <span class="token keyword">sizeof</span><span class="token punctuation">(</span>digest<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token function">mbedtls_md_free</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>ctx<span class="token punctuation">)</span><span class="token punctuation">;</span> 
<span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>

}

  • 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
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42

运行结果
在这里插入图片描述
sha256sum验证
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值