小鬼之家

流浪,游走于文明与原始之间. 关注底层技术,实现美好生活。

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

MD5:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/md5.h>

#pragma comment(lib,"libeay32.lib")

#define BLKSIZE 4096

#define OUTSIZE 16

int main(int argc, char *argv[])
{
    int bytes;
    int i;
    unsigned char output[OUTSIZE];
    MD5_CTX s;
    unsigned char data[BLKSIZE];
    FILE *fn;

    if(argc < 2){
        printf ("please input a filename.\n");
        return 0;
    }

    fn = fopen(argv[1], "rb");

    if (fn == NULL) {
        printf ("%s can't be opened.\n", argv[1]);
        return 0;
    }

    MD5_Init(&s);

    while ((bytes = fread(data, 1, BLKSIZE, fn)) != 0){
        MD5_Update(&s, data, bytes);
    }

    MD5_Final(output, &s);

    fclose(fn);

    for(i = 0; i < OUTSIZE; i++) {
        printf("%02x", output[i]);
    }

    return 0;
}

SHA1:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/sha.h>

#pragma comment(lib,"libeay32.lib")

#define BLKSIZE 4096
#define OUTSIZE 20

int main(int argc, char *argv[])
{
    int bytes;
    int i;
    unsigned char output[OUTSIZE];
    SHA_CTX s;
    unsigned char data[BLKSIZE];
    FILE *fn;

    if(argc < 2){
        printf ("please input a filename.\n");
        return 0;
    }

    fn = fopen(argv[1], "rb");

    if (fn == NULL) {
        printf ("%s can't be opened.\n", argv[1]);
        return 0;
    }

    SHA1_Init(&s);

    while ((bytes = fread(data, 1, BLKSIZE, fn)) != 0){
        SHA1_Update(&s, data, bytes);
    }

    SHA1_Final(output, &s);

    fclose(fn);

    for(i = 0; i < OUTSIZE; i++) {
        printf("%02x", output[i]);
    }

    return 0;
}

 

SHA256:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/sha.h>

#pragma comment(lib,"libeay32.lib")
#pragma comment(lib,"user32.lib")
#pragma comment(lib,"advapi32.lib")

#define BLKSIZE 4096
#define OUTSIZE 32

int main(int argc, char *argv[])
{
    int bytes;
    int i;
    unsigned char output[OUTSIZE];
    SHA256_CTX s;
    unsigned char data[BLKSIZE];
    FILE *fn;

    if(argc < 2){
        printf ("please input a filename.\n");
        return 0;
    }

    fn = fopen(argv[1], "rb");

    if (fn == NULL) {
        printf ("%s can't be opened.\n", argv[1]);
        return 0;
    }

    SHA256_Init(&s);

    while ((bytes = fread(data, 1, BLKSIZE, fn)) != 0){
        SHA256_Update(&s, data, bytes);
    }

    SHA256_Final(output, &s);

    fclose(fn);

    for(i = 0; i < OUTSIZE; i++) {
        printf("%02x", output[i]);
    }

    return 0;
}

 

SHA512:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/sha.h>

#pragma comment(lib,"libeay32.lib")
#pragma comment(lib,"user32.lib")
#pragma comment(lib,"advapi32.lib")

#define BLKSIZE 4096
#define OUTSIZE 64

int main(int argc, char *argv[])
{
    int bytes;
    int i;
    unsigned char output[OUTSIZE];
    SHA512_CTX s;
    unsigned char data[BLKSIZE];
    FILE *fn;

    if(argc < 2){
        printf ("please input a filename.\n");
        return 0;
    }

    fn = fopen(argv[1], "rb");

    if (fn == NULL) {
        printf ("%s can't be opened.\n", argv[1]);
        return 0;
    }

    SHA512_Init(&s);

    while ((bytes = fread(data, 1, BLKSIZE, fn)) != 0){
        SHA512_Update(&s, data, bytes);
    }

    SHA512_Final(output, &s);

    fclose(fn);

    for(i = 0; i < OUTSIZE; i++) {
        printf("%02x", output[i]);
    }

    return 0;
}

需要注意的是sha256和sha512需要用到user32.lib和advapi32.lib

posted on 2014-02-11 16:31  黄尚  阅读(3322)  评论(0编辑  收藏  举报