用C语言实现md5sum/sha256sum的功能

本文最后更新于2017年6月16日,已超过 1 年没有更新,如果文章内容失效,还请反馈给我,谢谢!

=Start=

缘由:

学习需要

正文:

参考解答:
MD5(支持文件和字符串)
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/md5.h>
int file_exist(char *filename) {
    if( access( filename, F_OK ) != -1 ) {
        return 1;   // file exists
    else {
        return 0;   // file doesn't exist
    }
}
void get_file_hash(char* filename, unsigned char* digest){
    MD5_CTX c;
    char buf[1024];
    ssize_t bytes;
    int fd = open(filename, O_RDONLY);
    if (fd == -1)
        exit(-1);
    MD5_Init(&c);
    bytes = read(fd, buf, 1024);
    while(bytes > 0) {
        MD5_Update(&c, buf, bytes);
        bytes=read(fd, buf, 1024);
    }
    MD5_Final(digest, &c);
    close(fd);
}
void get_str_hash(char* str, unsigned char* digest){
    MD5_CTX c;
    MD5_Init(&c);
    MD5_Update(&c, str, strlen(str));
    MD5_Final(digest, &c);
}
// Print the MD5 sum as hex-digits.
void print_md5_sum(unsigned char* md) {
    int i;
    for(i=0; i <MD5_DIGEST_LENGTH; i++) {
        printf("%02x", md[i]);
    }
}
int main(int argc, char *argv[]) {
    printf("MD5_DIGEST_LENGTH = %d\n", MD5_DIGEST_LENGTH);
    unsigned char md5_digest[MD5_DIGEST_LENGTH];
    int i = 0;
    for (i = 1; i < argc; i++) {
        printf("argv[%d] = '%s'\n", i, argv[i]);
        if (file_exist(argv[i])) {
            get_file_hash(argv[i], md5_digest);
            print_md5_sum(md5_digest);
            printf("\tFile('%s')\n\n", argv[i]);
        else {
            get_str_hash(argv[i], md5_digest);
            print_md5_sum(md5_digest);
            printf("\tString('%s')\n\n", argv[i]);
        }
    }
    return 0;
}
SHA256(支持文件和字符串)
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/sha.h>
int file_exist(char *filename) {
    if( access( filename, F_OK ) != -1 ) {
        return 1;   // file exists
    else {
        return 0;   // file doesn't exist
    }
}
void get_file_hash(char* filename, unsigned char* digest){
    SHA256_CTX c;
    char buf[1024];
    ssize_t bytes;
    int fd = open(filename, O_RDONLY);
    if (fd == -1)
        exit(-1);
    SHA256_Init(&c);
    bytes = read(fd, buf, 1024);
    while(bytes > 0) {
        SHA256_Update(&c, buf, bytes);
        bytes=read(fd, buf, 1024);
    }
    SHA256_Final(digest, &c);
    close(fd);
}
void get_str_hash(char* str, unsigned char* digest){
    SHA256_CTX c;
    SHA256_Init(&c);
    SHA256_Update(&c, str, strlen(str));
    SHA256_Final(digest, &c);
}
// Print the MD5/SHA1/SHA256 sum as hex-digits.
void print_hex_hash(unsigned char* digest, int length) {
    int i;
    for(i=0; i <length; i++) {
        printf("%02x", digest[i]);
    }
}
int main(int argc, char *argv[]) {
    printf("SHA256_DIGEST_LENGTH = %d\n", SHA256_DIGEST_LENGTH);
    unsigned char sha256_digest[SHA256_DIGEST_LENGTH];
    int i = 0;
    for (i = 1; i < argc; i++) {
        printf("argv[%d] = '%s'\n", i, argv[i]);
        if (file_exist(argv[i])) {
            get_file_hash(argv[i], sha256_digest);
            print_hex_hash(sha256_digest, SHA256_DIGEST_LENGTH);
            printf("\tFile('%s')\n\n", argv[i]);
        else {
            get_str_hash(argv[i], sha256_digest);
            print_hex_hash(sha256_digest, SHA256_DIGEST_LENGTH);
            printf("\tString('%s')\n\n", argv[i]);
        }
    }
    return 0;
}
参考链接:

md5sum of file in Linux C #基于openssl,从标准输入读取内容进行计算
https://stackoverflow.com/questions/3395690/md5sum-of-file-in-linux-c
http://www.unix.com/programming/134079-computing-md5sum-c.html

How to calculate the MD5 hash of a large file in C? #计算大文件的哈希值(分块读取)
https://stackoverflow.com/questions/10324611/how-to-calculate-the-md5-hash-of-a-large-file-in-c

使用 mmap 的方式将文件内容读入内存(适于小文件)
https://stackoverflow.com/questions/1220046/how-to-get-the-md5-hash-of-a-file-in-c

https://linux.die.net/man/3/md5_update
https://linux.die.net/man/3/sha
https://www.openssl.org/docs/man1.0.2/crypto/MD5.html
https://www.openssl.org/docs/man1.0.2/crypto/sha.html

各种语言的MD5摘要实现
http://userpages.umbc.edu/~mabzug1/cs/md5/md5.html
http://people.csail.mit.edu/rivest/Md5.c

计算字符串的MD5摘要
https://stackoverflow.com/questions/7627723/how-to-create-a-md5-hash-of-a-string-in-c

在C语言中检测文件是否存在的方法
https://stackoverflow.com/questions/230062/whats-the-best-way-to-check-if-a-file-exists-in-c-cross-platform

=END=

声明: 除非注明,ixyzero.com文章均为原创,转载请以链接形式标明本文地址,谢谢!
https://ixyzero.com/blog/archives/3353.html

3 thoughts on “用C语言实现md5sum/sha256sum的功能”

发表评论

电子邮件地址不会被公开。 必填项已用*标注