]>
jfr.im git - solanum.git/blob - tools/mkpasswd.c
1 /* simple password generator by Nelson Minar (minar@reed.edu)
2 * copyright 1991, all rights reserved.
3 * You can use this code as long as my name stays with it.
5 * md5 patch by W. Campbell <wcampbel@botbay.net>
6 * Modernization, getopt, etc for the Hybrid IRCD team
9 * /dev/random for salt generation added by
10 * Aaron Sethman <androsyn@ratbox.org>
23 #define FLAG_MD5 0x00000001
24 #define FLAG_SALT 0x00000002
25 #define FLAG_PASS 0x00000004
26 #define FLAG_LENGTH 0x00000008
27 #define FLAG_BLOWFISH 0x00000010
28 #define FLAG_ROUNDS 0x00000020
29 #define FLAG_SHA256 0x00000040
30 #define FLAG_SHA512 0x00000080
33 static char *make_md5_salt(int);
34 static char *make_md5_salt_para(char *);
35 static char *make_sha256_salt(int);
36 static char *make_sha256_salt_para(char *);
37 static char *make_sha512_salt(int);
38 static char *make_sha512_salt_para(char *);
39 static char *make_bf_salt(int, int);
40 static char *make_bf_salt_para(int, char *);
41 static char *generate_random_salt(char *, int);
42 static char *generate_poor_salt(char *, int);
44 static void full_usage(void);
45 static void brief_usage(void);
47 static char saltChars
[] = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
48 /* 0 .. 63, ascii - 64 */
56 static char getpassbuf
[PASS_MAX
+ 1];
59 getpass(const char *prompt
)
64 memset(getpassbuf
, 0, sizeof(getpassbuf
));
65 fputs(prompt
, stderr
);
79 fputs("\r\n", stderr
);
87 main(int argc
, char *argv
[])
89 char *plaintext
= NULL
;
91 char *saltpara
= NULL
;
95 int length
= 0; /* Not Set */
96 int rounds
= 0; /* Not set, since blowfish needs 4 by default, a side effect
97 * of this being the encryption type parameter must be
98 * specified before the rounds parameter.
101 while((c
= getopt(argc
, argv
, "xymbr:h?l:s:p:")) != -1)
109 flag
|= FLAG_BLOWFISH
;
114 length
= atoi(optarg
);
118 rounds
= atoi(optarg
);
143 printf("Invalid Option: -%c\n", c
);
153 salt
= make_md5_salt_para(saltpara
);
155 salt
= make_md5_salt(length
);
157 else if(flag
& FLAG_BLOWFISH
)
162 salt
= make_bf_salt_para(rounds
, saltpara
);
164 salt
= make_bf_salt(rounds
, length
);
166 else if(flag
& FLAG_SHA256
)
171 salt
= make_sha256_salt_para(saltpara
);
173 salt
= make_sha256_salt(length
);
180 salt
= make_sha512_salt_para(saltpara
);
182 salt
= make_sha512_salt(length
);
189 fprintf(stderr
, "Please enter a valid password\n");
193 hashed
= rb_crypt(plaintext
, salt
);
197 hashed
= strdup(rb_crypt(getpass("plaintext: "), salt
));
198 plaintext
= getpass("again: ");
200 if (strcmp(rb_crypt(plaintext
, salt
), hashed
) != 0)
202 fprintf(stderr
, "Passwords do not match\n");
207 printf("%s\n", hashed
);
212 make_md5_salt_para(char *saltpara
)
214 static char salt
[21];
215 if(saltpara
&& (strlen(saltpara
) <= 16))
217 /* sprintf used because of portability requirements, the length
218 ** is checked above, so it should not be too much of a concern
220 sprintf(salt
, "$1$%s$", saltpara
);
223 printf("Invalid Salt, please use up to 16 random alphanumeric characters\n");
231 make_md5_salt(int length
)
233 static char salt
[21];
236 printf("MD5 salt length too long\n");
242 generate_random_salt(&salt
[3], length
);
243 salt
[length
+ 3] = '$';
244 salt
[length
+ 4] = '\0';
249 make_sha256_salt_para(char *saltpara
)
251 static char salt
[21];
252 if(saltpara
&& (strlen(saltpara
) <= 16))
254 /* sprintf used because of portability requirements, the length
255 ** is checked above, so it should not be too much of a concern
257 sprintf(salt
, "$5$%s$", saltpara
);
260 printf("Invalid Salt, please use up to 16 random alphanumeric characters\n");
268 make_sha512_salt_para(char *saltpara
)
270 static char salt
[21];
271 if(saltpara
&& (strlen(saltpara
) <= 16))
273 /* sprintf used because of portability requirements, the length
274 ** is checked above, so it should not be too much of a concern
276 sprintf(salt
, "$6$%s$", saltpara
);
279 printf("Invalid Salt, please use up to 16 random alphanumeric characters\n");
288 make_sha256_salt(int length
)
290 static char salt
[21];
293 printf("SHA256 salt length too long\n");
299 generate_random_salt(&salt
[3], length
);
300 salt
[length
+ 3] = '$';
301 salt
[length
+ 4] = '\0';
306 make_sha512_salt(int length
)
308 static char salt
[21];
311 printf("SHA512 salt length too long\n");
317 generate_random_salt(&salt
[3], length
);
318 salt
[length
+ 3] = '$';
319 salt
[length
+ 4] = '\0';
324 make_bf_salt_para(int rounds
, char *saltpara
)
326 static char salt
[31];
328 if(saltpara
&& (strlen(saltpara
) <= 22))
330 /* sprintf used because of portability requirements, the length
331 ** is checked above, so it should not be too much of a concern
333 sprintf(tbuf
, "%02d", rounds
);
334 sprintf(salt
, "$2a$%s$%s$", tbuf
, saltpara
);
337 printf("Invalid Salt, please use up to 22 random alphanumeric characters\n");
345 make_bf_salt(int rounds
, int length
)
347 static char salt
[31];
351 printf("Blowfish salt length too long\n");
354 sprintf(tbuf
, "%02d", rounds
);
355 sprintf(salt
, "$2a$%s$", tbuf
);
356 generate_random_salt(&salt
[7], length
);
357 salt
[length
+ 7] = '$';
358 salt
[length
+ 8] = '\0';
363 generate_poor_salt(char *salt
, int length
)
367 for(i
= 0; i
< length
; i
++)
369 salt
[i
] = saltChars
[rand() % 64];
375 generate_random_salt(char *salt
, int length
)
379 if((fd
= open("/dev/random", O_RDONLY
)) < 0)
381 return (generate_poor_salt(salt
, length
));
383 buf
= calloc(1, length
);
384 if(read(fd
, buf
, length
) != length
)
387 return (generate_poor_salt(salt
, length
));
390 for(i
= 0; i
< length
; i
++)
392 salt
[i
] = saltChars
[abs(buf
[i
]) % 64];
401 printf("mkpasswd [-m|-b|-x|-y] [-l saltlength] [-r rounds] [-s salt] [-p plaintext]\n");
402 printf("-x Generate a SHA256 password\n");
403 printf("-y Generate a SHA512 password\n");
404 printf("-m Generate an MD5 password\n");
405 printf("-b Generate a Blowfish password\n");
406 printf("-l Specify a length for a random MD5 or Blowfish salt\n");
407 printf("-r Specify a number of rounds for a Blowfish password\n");
408 printf(" Default 4, no more than 6 recommended\n");
409 printf("-s Specify a salt, up to 16 for MD5, SHA256, and SHA512\n");
410 printf(" up to 22 for Blowfish\n");
411 printf("-p Specify a plaintext password to use\n");
412 printf("Example: mkpasswd -m -s 3dr -p test\n");
419 printf("mkpasswd - password hash generator\n");
420 printf(" SHA512: mkpasswd [-y] [-l saltlength] [-s salt] [-p plaintext]\n");
421 printf(" SHA256: mkpasswd -x [-l saltlength] [-s salt] [-p plaintext]\n");
422 printf(" MD5: mkpasswd -m [-l saltlength] [-s salt] [-p plaintext]\n");
423 printf("Blowfish: mkpasswd -b [-r rounds] [-l saltlength] [-s salt]\n");
424 printf(" [-p plaintext]\n");
425 printf("Use -h for full usage\n");