LinuxQuestions.org
Review your favorite Linux distribution.
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 08-23-2012, 10:08 AM   #1
yahoosam
Member
 
Registered: Jun 2012
Posts: 79

Rep: Reputation: Disabled
storing MD5 generated key in string variable


i stuck with something like..
i am having cipher-Key which is being generated through MD5 Algo & printing likewise below:
Code:
for (i = 0; i < 16; i++) {
                printf ("%02x", digest[i]);
        }
output:d41d8cd98f00b204e9800998ecf8427e
need to know how to store same output in string variable.
 
Old 08-23-2012, 10:19 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,626

Rep: Reputation: 1111Reputation: 1111Reputation: 1111Reputation: 1111Reputation: 1111Reputation: 1111Reputation: 1111Reputation: 1111Reputation: 1111
sprintf is your friend:
Code:
char buff [16*2+1], *p= buff;

for (i = 0; i < 16; i++) {
    p += sprintf (p, "%02x", digest[i]);
}
 
2 members found this post helpful.
Old 08-23-2012, 10:20 AM   #3
byannoni
Member
 
Registered: Aug 2012
Location: /home/byannoni
Distribution: Arch
Posts: 128

Rep: Reputation: 36
Code:
for (i = 0; i < 16; i++)
{
    sprintf( string_ptr, "%02x", digest[i]);
}
Edit:
NevemTeve's example is better

Last edited by byannoni; 08-23-2012 at 10:21 AM.
 
Old 08-24-2012, 03:05 AM   #4
yahoosam
Member
 
Registered: Jun 2012
Posts: 79

Original Poster
Rep: Reputation: Disabled
dear NevemTeve!!
can you please assist me two things..
Code:
char buff [16*2+1]
/*why this size : 16*2+1*/
and
p += sprintf (...);
/*please add your comment*/
thanks!
 
Old 08-24-2012, 08:45 AM   #5
byannoni
Member
 
Registered: Aug 2012
Location: /home/byannoni
Distribution: Arch
Posts: 128

Rep: Reputation: 36
Quote:
Originally Posted by yahoosam View Post
dear NevemTeve!!
can you please assist me two things..
Code:
char buff [16*2+1]
/*why this size : 16*2+1*/
and
p += sprintf (...);
/*please add your comment*/
thanks!
16 * 2 is 32 (the length of the MD5 sum)
32 + 1 leave a byte for the trailing null character.
 
Old 08-24-2012, 12:06 PM   #6
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,626

Rep: Reputation: 1111Reputation: 1111Reputation: 1111Reputation: 1111Reputation: 1111Reputation: 1111Reputation: 1111Reputation: 1111Reputation: 1111
ad 1: already answered: 32 visible character + terminating zero
ad 2: here is a comment: sprintf is a very useful function.
 
Old 08-26-2012, 06:14 PM   #7
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,398
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
The printf() family of functions return the number of bytes assigned to the output stream, string, or file (ie, the number of bytes printed). That fact is useful for code like:
Code:
p += sprintf (...);
when used in a loop, iteratively appending to the string.

--- rod.

Last edited by theNbomr; 08-26-2012 at 06:15 PM.
 
1 members found this post helpful.
Old 08-28-2012, 01:33 AM   #8
yahoosam
Member
 
Registered: Jun 2012
Posts: 79

Original Poster
Rep: Reputation: Disabled
ThanQ,vry much all of you!!
 
  


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
Need help with script writing: Storing cmd in variable, print variable, then exe cmds Arodef Programming 3 01-17-2012 01:26 AM
Storing table in variable DeepSeaNautilus Linux - Software 5 04-04-2009 02:45 PM
Storing part of a filename as a variable VTGuitarMan Programming 5 03-12-2008 07:47 AM
C: storing string which is more efficient. debiant Programming 22 09-01-2006 01:39 AM
storing and comparing a string Randall Programming 18 03-10-2003 11:05 PM

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

All times are GMT -5. The time now is 04:54 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration