Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

md5 encoding

Status
Not open for further replies.

jby1

Programmer
Apr 29, 2003
403
GB
Hi

I am looking for a function to provide me with md5 encoding. Is there anything in the standard C libraries to do this? (I haven't used C for some years so I am a little rusty!)

Cheers
 
Actually you should check the project, as it includes MD5 hash capabilities and more importantly has good documentation.

On the other hand, the code that Salem has pointed to is extremely well exampled, and could be easily modified to do MD5 password comparing suitable for Linux or LDAP password authentication.
 
Not to beat a dead horse, but since many people may actually want to do passwd file auth under Linux specifically - crypt() will handle the special case of salted MD5 as well as traditional crypt. Strangely my system's shadow file has both traditional and SMD5. Here's a code sample from I ended up using:

+++snip+++
char *userlogin; // From function call
char *userpassword; // From function call
char shadowpass[1024] = "";
char buffer[1024] = "";
char usrSalt[100] = "";
char *saltmine = NULL;
int saltlen = 0;

// ... Getting password from shadow removed ...

if (0 == strncmp(shadowpass, "$1$", 3))
{
saltmine = strchr(&shadowpass[3], '$');
saltlen = 1 + (saltmine - shadowpass);

strncpy(usrSalt, shadowpass, saltlen);
usrSalt[saltlen] = 0;
}
else
{
strncpy(usrSalt, shadowpass, 2);
usrSalt[2] = 0;
}
strncpy( buffer, crypt(usrpasswd, usrSalt), BUFSIZ);

// Zero if match: - strcmp(buffer, shadowpass)
+++snip+++

I hope this makes someone's life easier!
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top