1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
|
#define _XOPEN_SOURCE_
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
char *crypt(const char *clef,const char *salt);
/***************************************/
/* Cette fonction génère aléatoirement */
/* un salt pour hachage MD5 */
/* =================================== */
/* buf : tampon où stocker le salt */
/***************************************/
void generate_md5_salt(char buf[13])
{
int i;
//on initialse le générateur de nombre aléatoire
srand(time(NULL));
//un salt pour MD5 a le format : $1$<chaine de 8 caractères>$
//les huits caractères sont à choisir parmi [a-zA-Z0-9./]
buf[0]='$';
buf[1]='1';
buf[2]='$';
for (i=3; i < 11;i++)
{
do{
buf[i]=rand() % 128;
}while(
(buf[i] < 'a' || buf[i] > 'z')
&& (buf[i] < 'A' || buf[i] > 'Z')
&& (buf[i] < '0' || buf[i] > '9')
&& (buf[i] != '.')
&& (buf[i] != '/')
);
}
buf[11]='$';
buf[12]='\0';
}
int main ()
{
char salt[13];
char buff[256];
generate_md5_salt(salt);
printf("Entrez le texte à crypter :");
scanf("%s",buff);
printf("Le salt est %s\nLe texte crypte est %s\n",salt,crypt(buff,salt));
return 0; |
Partager