-2

An MD5 encrypted password is $1$12345678$blahblahblah. The salt is the 8 digit key between the two $ signs after the one. How do i extract those 8?

So far I have char *salt = and i need to make it equal to the third-tenth index of the string.

Jakkie Chan
  • 317
  • 4
  • 14
  • 1
    http://linux.die.net/man/3/strncpy or http://linux.die.net/man/3/memcpy, after using http://linux.die.net/man/3/strtok to find the border characters. – AntonH May 22 '14 at 14:38

1 Answers1

0

Your question contains the word "extract" which is somewhat complicated because it is not clear if you want to copy those 8 characters into a new buffer or if you are looking to do something else.

I can imagine that perhaps you simply want to display these 8 characters to a console user. The following code accomplishes this (but probably requires a bit of explanation following):

char *salt = "$1$12345678$blahblahblah";
int from = 3;
int to = 10;
int len = to-from+1;
printf("%.*s\n", len, salt+from);

Printf() and its variants have some pretty powerful string generation/manipulation capabilities. In this case I used a "precision" specifier provided as a parameter for the 's' conversion. If a precision is given for a 's' conversion it is taken to limit the number of characters emitted. I used 'len' via the '*' character to provide this limit in a parametrized fashion. If you know that you are always going to want to emit 8 characters starting from the 3rd (in C we always count from 0 not from 1) then your code can be simplified to the more straightforward form given below:

char *salt = "$1$12345678$blahblahblah";
printf("%.8s\n", salt+3);

Finally, sprintf can be used to copy to another buffer. I'll give a simple form of this task below (note that I changed variable names for clarity; you are extracting the salt from a line of password text):

char *password = "$1$12345678$blahblahblah";
char salt[9]; /* Size of 9 required to hold terminating NULL byte */
sprintf(salt, "%.8s\n", password+3);

/* Now you can use salt for various purposes */
printf ("The salt is %s\n", salt);    
Jim Amidei
  • 106
  • 3