Can some body tell me how to take SHA-1 of a given string in C. I am using ubuntu 9.04 with kernel 2.6.30.

Here is the code that compute a sha1 of a file, but when i try to execute it returns the whole string and I want only the sha1. sha1sum is linux utility.

#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>

int main()
{
	unsigned int sha1_sum;
	char *s;
	char ch;
		
	s = system("sha1sum -t a.txt");

	while(ch != ' ')
	{
		ch = s;

		printf("%c", ch);
		s++;

	}

	return 0;
}

system function returns an integer so you cant catch its out put in a string.prototype as follows: int system(const char *string);

> s = system("sha1sum -t a.txt"); system() doesn't return the output of an executed command.
It returns an integer value, which indicates whether it could execute the command successfully or not.

Edit:: Dream2Code posted before me.

> s = system("sha1sum -t a.txt"); system() doesn't return the output of an executed command.
It returns an integer value, which indicates whether it could execute the command successfully or not.

>>char *s;
>>s = system("sha1sum -t a.txt");

i pointed to the above part of the code.that's i highlighted the prototype.

In Fact system retuns the exit status of the commadn executed.
Thanks for you i/p tux4life.

OK, I have corrected this by a trick, The Code is given

#include <stdlib.h>
#include <stdio.h>

int main()
{
	FILE *fp;

        int i=0;

	char *s;
	char ch;
        char a[41];

	s = system("sha1sum -t a.txt > tmp.txt");
	fp = fopen("tmp.txt", "r");
	
	while(ch != ' ')
	{
		ch = fgetc(fp);
                a[i] = ch;
                i++;
	}
        puts(a);
	printf("\n");
	
		
	return 0;
}

Now works Perfectly and I am getting what I want.

OK, I have corrected this by a trick, The Code is given

#include <stdlib.h>
#include <stdio.h>

int main()
{
	FILE *fp;

        int i=0;

	char *s;
	char ch;
        char a[41];

	s = system("sha1sum -t a.txt > tmp.txt");
	fp = fopen("tmp.txt", "r");
	
	while(ch != ' ')
	{
		ch = fgetc(fp);
                a[i] = ch;
                i++;
	}
        puts(a);
	printf("\n");
	
		
	return 0;
}

Now works Perfectly and I am getting what I want.

dont use a char * to capture the return of system.
****system() will always return the exit status which is an integer.
use like:

int status;
status=system("command");
if(status!=0)
{
printf("system command not executed properly");
}

apart from this everything else is fine.

The return value of the system() function is platform dependent:
http://www.cplusplus.com/reference/clibrary/cstdlib/system/

>>function is platform dependent:
yes it is.But
You intrepreted the thing wrongly.
it will always return an integer but its intrepretation is system dependent.
means,
in some systems 0 means success some other 1 means success.like that.
>>returning an int value, whose interpretation is system-dependent.

it means it will always return an int , we have intrepret its success of failure according to systems.

>>function is platform dependent:
yes it is.But
You intrepreted the thing wrongly.
it will always return an integer but its intrepretation is system dependent.
means,
in some systems 0 means success some other 1 means success.like that.
>>returning an int value, whose interpretation is system-dependent.

it means it will always return an int , we have intrepret its success of failure according to systems.

Yes, but then your code snippet won't run on every system.
That's what I meant.

Yes, but then your code snippet won't run on every system.
That's what I meant.

cann't give you more reputation today [:)]

Comments
Because you do much effort to help people, you don't deserve that red dot.
This question has already been answered. Start a new discussion instead.