Very simple SHA1 test program written in C

· klm's blog


Original post is here: eklausmeier.goip.de

Here is a simple test program to call SHA1 hashing routine from OpenSSL.

 1#include <stdio.h>
 2#include <string.h>
 3#include <openssl/sha.h>
 4
 5int main (int argc, char *argv[]) {
 6        unsigned long i, n;
 7        unsigned char md[1024];
 8
 9        if (argc <= 1) return 0;
10
11        n = strlen(argv[1]);
12        SHA1((unsigned char*)argv[1],n,md);
13
14        for (i=0; i<SHA_DIGEST_LENGTH; ++i)
15                printf("%02x",md[i]);
16        puts("");
17
18        return 0;
19}

Compile with

1cc -Wall sha1tst.c -o sha1tst -lcrypto

It is important to give the -l flag after -o.

Some tests:

1$ ./sha1tst ABCabc
2135488ccc0c5e5a3d0ac437aac1821bba9347b3d
3$ printf "ABCabc" | sha1sum
4135488ccc0c5e5a3d0ac437aac1821bba9347b3d  -

In Ubuntu the openssl development libraries are in libssl-dev.