首页 > 其他 > 详细

几个常用的散列算法

时间:2014-02-11 23:34:46      阅读:915      评论:0      收藏:0      [点我收藏+]

MD5:

bubuko.com,布布扣
#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;
}
bubuko.com,布布扣

SHA1:

bubuko.com,布布扣
#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;
}
bubuko.com,布布扣

 

SHA256:

bubuko.com,布布扣
#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;
}
bubuko.com,布布扣

 

SHA512:

bubuko.com,布布扣
#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;
}
bubuko.com,布布扣

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

几个常用的散列算法

原文:http://www.cnblogs.com/afxcn/p/3544643.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!