Home > C/C++, Security > Using SHA1() function

Using SHA1() function

Hi. I’ ll show you how to convert any string into a SHA1 hash using the openssl library.
To use this library you must include in your codes this:

#include <openssl/sha.h>

Moreover you have to compile the source with the “-lssl” flag.
The following code takes in input a string (argv[1]) and generates an hash string through the SHA1() function. The value of “SHA_DIGEST_LENGTH” is 20 bytes, that is the dimension of SHA1 output.
This function stores the value computed in “temp” variable.
Then in the for loop, I copy the value of “temp” into “buf”, according to the standard format of SHA1 strings.

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

int main(int argn, char *argv[]) {

	int i = 0;
	unsigned char temp[SHA_DIGEST_LENGTH];
	char buf[SHA_DIGEST_LENGTH*2];

	if ( argn != 2 ) {
		printf("Usage: %s string\n", argv[0]);
		return -1;
	}

	memset(buf, 0x0, SHA_DIGEST_LENGTH*2);
	memset(temp, 0x0, SHA_DIGEST_LENGTH);

	SHA1((unsigned char *)argv[1], strlen(argv[1]), temp);

	for (i=0; i < SHA_DIGEST_LENGTH; i++) {
		sprintf((char*)&(buf[i*2]), "%02x", temp[i]);
	}

	printf("SHA1 of %s is %s\n", argv[1], buf);

	return 0;

}

We can compile and run it as follows:

$ gcc sha.c -lssl -o sha
$ ./sha asd
SHA1 of asd is f10e2821bbbea527ea02200352313bc059445190

If you want know something else of SHA1() function you can use the manpages:

man 3 sha1

Or look up in openssl website: link

About these ads
  1. Traktopel
    October 7, 2010 at 14:50

    Useful! Thanks! :D

  2. Hazem Elbaz
    March 22, 2012 at 02:30

    i did you example and run the command as you said, and i have this Error

    /tmp/ccqCjQoH.o: In function `main’:
    sha.c:(.text+0xc9): undefined reference to `SHA1′
    collect2: ld returned 1 exit status

    to solve this Error msg, i run the following command:

    $ gcc sha.c -Wall -lcrypto -o sha

    could you please explain what is the different between ” -lssl ” and ” -lcrypto “.

    i hope to reply to me.

  3. EJ
    September 16, 2012 at 16:25

    Thanks alot man! Been googling this for hours

  4. Alejandro
    November 10, 2012 at 13:37

    Sweet blog! I found it while browsing on Yahoo News.
    Do you have any suggestions on how to get listed in Yahoo
    News? I’ve been trying for a while but I never seem to get there! Thank you

  5. November 16, 2012 at 10:28

    Watkins Glen soon enough would be filled with 50,000 cheering NASCAR aficionados
    for the afternoon race. The winner, Denny Hamlin
    maintained a good lead over Tony Stewart and Kyle Busch to ultimately
    take the race. This could be anyone’s race, although last year’s Sprint Cup Series runner-up, Carl Edwards, wants
    it to be his.

  6. live chubby porn
    December 6, 2012 at 18:30

    I have read so many articles regarding the blogger lovers except this paragraph is in fact a good
    paragraph, keep it up.

  7. bbw
    January 11, 2013 at 13:58

    Nice answers in return of this matter with solid arguments and describing
    all regarding that.

  8. blake
    October 29, 2013 at 04:13

    Here is an implementation that does not require openssl: http://www.zedwood.com/article/cpp-sha1-function

  9. q
    April 15, 2014 at 12:39

    This code has an overflow: buf length should be SHA_DIGEST_LENGTH*2 + 1. You forgot the null char (string terminator).

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: