幾個經常使用的散列算法

MD5:api

#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:spa

#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:code

#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:blog

#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.libmd5

相關文章
相關標籤/搜索