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

SHA-1算法

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[20]是输出的字节数据。是个长度为20的字节数组。

1.3、使用示例

step1、创建一个项目目录SHA1,并进入该目录

mkdir SHA1 && cd SHA1

step2、使用curl命令下载代码

curl -LO https://raw.githubusercontent.com/ARMmbed/mbedtls/master/include/mbedtls/sha1.h
curl -LO https://raw.githubusercontent.com/ARMmbed/mbedtls/master/library/sha1.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_SHA1_C

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

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

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

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

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

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

void performSHA1(char* input, bool isToUpper) {
    //SHA-1算法能够根据任意长度的数据计算出一个固定长度(160bit=20byte)的唯一数据
    unsigned char output[20] = {0};
    mbedtls_sha1((unsigned char*)input, strlen(input), output);
    //为了便于比较,通常会把SHA-1运算后得到的160bit数据再用base16编码。因为每4bit可以表示一个十六进制字符,用十六进制表示就是40个字符。
    char hex[41] = {0};
    base16(output, hex, isToUpper);
    printf("sha1(%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 {
            performSHA1(argv[1], 0);
        }
    } else {
        if (strcmp("-x", argv[1]) == 0) {
            performSHA1(argv[2], 0);
        } else if (strcmp("-X", argv[1]) == 0) {
            performSHA1(argv[2], 1);
        } else {
            showHelp();
        }
    }

    return 0;
}

step7、使用cc命令进行编译

cc -o sha1Test *.c

step8、运行sha1Test

./sha1Test -x abc
sha1(abc)=a9993e364706816aba3e25717850c26c9cd0d89d

./sha1Test -X abc
sha1(abc)=A9993E364706816ABA3E25717850C26C9CD0D89D

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

source code on GitHub