val37
val37

Reputation: 67

How to store SHA1 output to a file using C

I have computed the SHA1 for a piece of data and I need to store it into a file to be read by another person later. The SHA1 gives the output as 190 bit's, but I can't store as bits into a file, fputc() function does it characters which means bytes.

So I want to either store it as bytes into the file or as its hexadecimal representation (preferably). In C, so far I can only get it to print the hash to the terminal as the hexadecimal representation (using a way I found in another question posted in this site), but I can't figure out how to store it to a file properly.

Please help!

Upvotes: 0

Views: 2102

Answers (2)

neenu
neenu

Reputation: 11

i used the following code for generating sha1 hash of text stored in message.txt file(text is: hello how are you).

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

#define BUFSIZE 1024*16

void do_fp(FILE *f);
void pt(unsigned char *md);
#ifndef _OSD_POSIX
int read(int, void *, unsigned int);
#endif

int main(int argc, char **argv)
{
    int i, err = 0;
    FILE *IN,*fp;

    if (argc == 1) {
      do_fp(stdin);
    }
 else {
printf("hi");        
for (i = 1; i < argc; i++) {
            IN = fopen(argv[i], "r");
            if (IN == NULL) {
                perror(argv[i]);
                err++;
                continue;
            }
            printf("SHA1(%s)= ", argv[i]);
            do_fp(IN);
            fclose(IN);
        }
    }
    exit(err);
}

void do_fp(FILE *f)
{
    SHA_CTX c;
    unsigned char md[SHA_DIGEST_LENGTH];
    int fd;
    int i;
    unsigned char buf[BUFSIZE];
    FILE *fp;
    fd = fileno(f);
    SHA1_Init(&c);
    for (;;) {
        i = read(fd, buf, BUFSIZE);
        if (i <= 0)
            break;
        SHA1_Update(&c, buf, (unsigned long)i);
    }
    SHA1_Final(&(md[0]), &c);
    fp = fopen("file.txt","wb");

   char *p=(void*)md; 
   i=SHA_DIGEST_LENGTH; 
   while (i--) 
   fprintf (fp, "%02x",*p++);
 pt(md);
}

void pt(unsigned char *md)
{
    int i;

    for (i = 0; i < SHA_DIGEST_LENGTH; i++)
        printf("%02x", md[i]);
    printf("\n");
}

i got followng as output. SHA1(message.txt)= 46e7b8310aa4d03b57e11022166d37beedf36537 however hash stored in file.txt is as follows. 46ffffffe7ffffffb8310affffffa4ffffffd03b57ffffffe11022166d37ffffffbeffffffedfffffff36537 please provide necessary guidance to achieve correct hash in file.txt. thank you in advance.

Upvotes: 1

Christophe
Christophe

Reputation: 73530

If you want to write the SHA1 digest as binary digest in the file, you can use:

fwrite ( &sha1_digest, sizeof(sha1_digest), 1, stream );

Where stream is a FILE* opened in binary mode.

If you want to save it as hexadecimal string, you can do a loop :

char *p=(void*)&sha1_digest; 
int i=sizeof(sha1_digest); 
while (i--) 
    fprintf (stream, "%02x",*p++); 

where tream is a FILE* opened in text or binary mode

I assumed that sha1_digest was a structure. If it's an array, you don't need the & and if the array is not fixed size, then you must provide the size of the digest (because in this case, sizeof() would return the sizeof a pointer isntead of the array.)

You are certainly aware, but SHA1 is no longer considered as a safe hash, if your use is somewhat related to security.

Upvotes: 2

Related Questions