LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-16-2015, 04:30 PM   #1
ron7000
Member
 
Registered: Nov 2007
Location: CT
Posts: 248

Rep: Reputation: 26
help incorporating sha256sum into C code


i am writing a quick C code that will read a text file that contains a list of files. my program will then be performing sha256sum on each file listing and writing that to an output file.
what i'd like to do is incorporate the source code of sha256sum from coreutils into my program, so that there can be no doubt about the integrity of the checksum... rather than referencing /usr/bin/sha256sum which may or may not be on a system.
I have the source from coreutils and i see under its lib folder the file sha256sum.c. however not sure what function to reference out of that file, or if there's another wrapper file i should be referencing?
 
Old 06-17-2015, 05:20 AM   #2
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Linux From Scratch, Slackware64, Partedmagic
Posts: 3,137

Rep: Reputation: 855Reputation: 855Reputation: 855Reputation: 855Reputation: 855Reputation: 855Reputation: 855
Try this:
Code:
#if 0

#©keithhedger Wed 17 Jun 10:39:19 BST 2015 kdhedger68713@gmail.com

gcc "$0" $(pkg-config --libs --cflags openssl)||exit 1
./a.out "$@"
retval=$?
rm ./a.out
exit $retval

#endif

#include <stdlib.h>
#include <stdio.h>
#include <openssl/sha.h>
#include <stddef.h>
#include <errno.h>
#include <string.h>

void sha256_hash_string (char hash[SHA256_DIGEST_LENGTH], char outputBuffer[65])
{
    int i = 0;

    for(i = 0; i < SHA256_DIGEST_LENGTH; i++)
    {
        sprintf(outputBuffer + (i * 2), "%02x", (unsigned char)hash[i]);
    }

    outputBuffer[64] = 0;
}

void sha256(char *string, char outputBuffer[65])
{
    unsigned char  hash[SHA256_DIGEST_LENGTH];
    int len;
    SHA256_CTX sha256;
    SHA256_Init(&sha256);

	len=strlen(string);
    SHA256_Update(&sha256, string,len);
    SHA256_Final(hash, &sha256);
    int i = 0;
    for(i = 0; i < SHA256_DIGEST_LENGTH; i++)
    {
        sprintf(outputBuffer + (i * 2), "%02x", (unsigned char)hash[i]);
    }
    outputBuffer[64] = 0;
}

int sha256_file(char *path, char outputBuffer[65])
{
    FILE *file = fopen(path, "rb");
    if(!file) return -534;

    unsigned char hash[SHA256_DIGEST_LENGTH];
    SHA256_CTX sha256;
    SHA256_Init(&sha256);
    const int bufSize = 32768;
    unsigned char *buffer = malloc(bufSize);
    int bytesRead = 0;
    if(!buffer) return ENOMEM;
    while((bytesRead = fread(buffer, 1, bufSize, file)))
    {
        SHA256_Update(&sha256, buffer, bytesRead);
    }
    SHA256_Final(hash, &sha256);

    sha256_hash_string(hash, outputBuffer);
    fclose(file);
    free(buffer);
    return 0;
}
int main(int argc, char **argv)
{
	char calc_hash[65];
	sha256_file(argv[1],calc_hash);
	printf("%s\n",calc_hash);
	return 0;
}
Just save the above file somewhere and make it executable ( chmod +x ) and run it like ( presumuing you are in the same directory as the file itself ) so:
Code:
keithhedger@LFSCerebro:/media/LinuxData/Development64/CScripts-> ./sha256.c ./sha256.c
e6fb775700e60446c8885303091e8f78aff4eb24297af412c5f55d181b898dfc
keithhedger@LFSCerebro:/media/LinuxData/Development64/CScripts-> sha256sum  ./sha256.c
e6fb775700e60446c8885303091e8f78aff4eb24297af412c5f55d181b898dfc  ./sha256.c
This will print the generated sha and verivy with the sha256sum command.
This as you can see from the gcc line links against openssl, this will be better than embeding all the code in your application as most (all?) linux distro are going to have openssl installed and when openssl is updated your code will benifit from the updated lib.
 
1 members found this post helpful.
Old 06-17-2015, 05:58 AM   #3
veerain
Senior Member
 
Registered: Mar 2005
Location: Earth bound to Helios
Distribution: Custom
Posts: 2,524

Rep: Reputation: 319Reputation: 319Reputation: 319Reputation: 319
Using openssl code ensures you don't make a mistake in implementing sha code.
 
Old 06-17-2015, 06:06 AM   #4
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Linux From Scratch, Slackware64, Partedmagic
Posts: 3,137

Rep: Reputation: 855Reputation: 855Reputation: 855Reputation: 855Reputation: 855Reputation: 855Reputation: 855
Yeah, what he said
 
Old 06-17-2015, 08:48 AM   #5
ron7000
Member
 
Registered: Nov 2007
Location: CT
Posts: 248

Original Poster
Rep: Reputation: 26
thanks Keith.
your code worked provided i strip out the stuff at the top between #if 0 and #endif, and compile my own executable... which is fine for me. it linked with -lssl and the 60 character hash output matches that from /usr/bin/sha256sum.

when i try to run it as you described, i get "Illegal variable name"

I compiled it using gcc sha256.c -O2 -o sha256_kh -lssl.
 
Old 06-17-2015, 09:01 AM   #6
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Linux From Scratch, Slackware64, Partedmagic
Posts: 3,137

Rep: Reputation: 855Reputation: 855Reputation: 855Reputation: 855Reputation: 855Reputation: 855Reputation: 855
Glad it worked for you, the auto compile bit at the top is just layiness on my part, works fine here might be your shell, not important.

Last edited by Keith Hedger; 06-17-2015 at 09:04 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] need help incorporating 3rd party enet and disk drivers into RedHat iso unix1adm Red Hat 2 06-12-2012 06:07 PM
Incorporating subroutine in main program fu3lc3ll$ Programming 4 05-24-2011 03:03 AM
[SOLVED] Perl SHA256SUM Clarification Sky.Crawler Programming 2 02-28-2011 08:33 PM
weird problems faced while incorporating ruby and python! wrapster Programming 2 10-05-2008 06:40 AM
Incorporating .NET code into a webpage Maverick1182 Programming 5 12-07-2006 06:27 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:19 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration