void mbedtls_sha256(
    const unsigned char *input,
    size_t inputLength,
    unsigned char output[32],
    int is224
)
1.0、参考
1.1、此函数的作用

SHA-224SHA-256算法

1.2、参数说明

const unsigned char *input是输入的字节数据C语言中没有其他语言中的byte类型,C语言中表达byte类型就是用unsigned charunsigned char的意思就是告诉编译器, 不要把我的最高位当成符号位,这样这8bit就都表示真实的数据了。

size_t的定义:

typedef long unsigned int __darwin_size_t;
typedef __darwin_size_t size_t;

size_t inputLengthconst unsigned char *input的大小,单位是字节

unsigned char output[32]是输出的字节数据。是个长度为32的字节数组。

int is224表示是否是SHA-224算法。

1.3、使用示例

step1、创建一个项目目录SHA-256,并进入该目录

mkdir SHA-256 && cd SHA-256

step2、使用curl命令下载代码

curl -LO https://raw.githubusercontent.com/ARMmbed/mbedtls/master/include/mbedtls/sha256.h
curl -LO https://raw.githubusercontent.com/ARMmbed/mbedtls/master/library/sha256.c

curl -LO https://raw.githubusercontent.com/ARMmbed/mbedtls/master/include/mbedtls/platform_util.h
curl -LO https://raw.githubusercontent.com/ARMmbed/mbedtls/master/library/platform_util.c

curl -LO https://raw.githubusercontent.com/ARMmbed/mbedtls/master/include/mbedtls/threading.h
curl -LO https://raw.githubusercontent.com/ARMmbed/mbedtls/master/library/threading.c

step3、编写一个config.h文件,其内容如下

#define MBEDTLS_SHA256_C

step4、将代码中的mbedtls/字符串去掉

sed -i    's@mbedtls/@@g' sha256.c platform_util.h platform_util.c threading.h threading.c 2> /dev/null ||
sed -i "" 's@mbedtls/@@g' sha256.c platform_util.h platform_util.c threading.h threading.c

step6、编写一个C语言源程序sha256Test.c,其内容如下

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include "sha256.h"

void showHelp() {
    printf("usage: sha256 [option] <str>\n");
    printf("option: -x lower case, default true\n");
    printf("        -X upper case\n");
    printf("example:sha256 -X sssssss\n");
    exit(1);
}

void base16(unsigned char input[32], char* output, bool isToUpper) {
    const char* format = isToUpper ? "%02X" : "%02x";
    char tmp[3] = {0};
    for(int i = 0; i < 32; i++) {
        sprintf(tmp, format, input[i]);
        strcat(output, tmp);
    }
}

void performSHA256(char* input, bool isToUpper) {
    //SHA-256算法能够根据任意长度的数据计算出一个固定长度(256bit=32byte)的唯一数据
    unsigned char output[32] = {0};
    mbedtls_sha256((unsigned char *)input, strlen(input), output, 0);
    //为了便于比较,通常会把SHA-256运算后得到的256bit数据再用base16编码。因为每4bit可以表示一个十六进制字符,用十六进制表示就是64个字符。
    char hex[65] = {0};
    base16(output, hex, isToUpper);
    printf("sha256(%s)=%s\n", input, hex);
}

int main(int argc, char* argv[]) {
    if (argc == 1) {
        showHelp();
    } else if (argc == 2) {
        if (strcmp("-h", argv[1]) == 0 
        || strcmp("--help", argv[1]) == 0
        || strcmp("-x", argv[1]) == 0
        || strcmp("-X", argv[1]) == 0) {
            showHelp();
        } else {
            performSHA256(argv[1], 0);
        }
    } else {
        if (strcmp("-x", argv[1]) == 0) {
            performSHA256(argv[2], 0);
        } else if (strcmp("-X", argv[1]) == 0) {
            performSHA256(argv[2], 1);
        } else {
            showHelp();
        }
    }

    return 0;
}

step7、使用cc命令进行编译

cc -o sha256Test *.c

step8、运行sha256Test

./sha256Test -x abc
sha256(abc)=ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad

./sha256Test -X abc
sha256(abc)=BA7816BF8F01CFEA414140DE5DAE2223B00361A396177A9CB410FF61F20015AD

step9、验证结果是否正确。在线工具中输入相同的字符,看看输出是否一样。

SHA-256 source code on GitHubSHA-224 source code on GitHub