]>
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>
21 #define FLAG_MD5 0x00000001
22 #define FLAG_SALT 0x00000002
23 #define FLAG_PASS 0x00000004
24 #define FLAG_LENGTH 0x00000008
25 #define FLAG_BLOWFISH 0x00000010
26 #define FLAG_ROUNDS 0x00000020
27 #define FLAG_SHA256 0x00000040
28 #define FLAG_SHA512 0x00000080
31 static char *make_md5_salt(int);
32 static char *make_md5_salt_para(char *);
33 static char *make_sha256_salt(int);
34 static char *make_sha256_salt_para(char *);
35 static char *make_sha512_salt(int);
36 static char *make_sha512_salt_para(char *);
37 static char *make_bf_salt(int, int);
38 static char *make_bf_salt_para(int, char *);
39 static char *generate_random_salt(char *, int);
40 static char *generate_poor_salt(char *, int);
42 static void full_usage(void) __attribute__((noreturn
));
43 static void brief_usage(void) __attribute__((noreturn
));
45 static char saltChars
[] = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
46 /* 0 .. 63, ascii - 64 */
49 main(int argc
, char *argv
[])
51 char *plaintext
= NULL
;
53 char *saltpara
= NULL
;
55 char *hashed
, *hashed2
;
57 int length
= 0; /* Not Set */
58 int rounds
= 0; /* Not set, since blowfish needs 4 by default, a side effect
59 * of this being the encryption type parameter must be
60 * specified before the rounds parameter.
63 while((c
= getopt(argc
, argv
, "xymbr:h?l:s:p:")) != -1)
71 flag
|= FLAG_BLOWFISH
;
76 length
= atoi(optarg
);
80 rounds
= atoi(optarg
);
105 printf("Invalid Option: -%c\n", c
);
115 salt
= make_md5_salt_para(saltpara
);
117 salt
= make_md5_salt(length
);
119 else if(flag
& FLAG_BLOWFISH
)
124 salt
= make_bf_salt_para(rounds
, saltpara
);
126 salt
= make_bf_salt(rounds
, length
);
128 else if(flag
& FLAG_SHA256
)
133 salt
= make_sha256_salt_para(saltpara
);
135 salt
= make_sha256_salt(length
);
142 salt
= make_sha512_salt_para(saltpara
);
144 salt
= make_sha512_salt(length
);
151 fprintf(stderr
, "Please enter a valid password\n");
155 hashed
= rb_crypt(plaintext
, salt
);
159 plaintext
= getpass("plaintext: ");
160 hashed
= rb_crypt(plaintext
, salt
);
163 fprintf(stderr
, "rb_crypt() failed\n");
166 hashed
= strdup(hashed
);
168 plaintext
= getpass("again: ");
169 hashed2
= rb_crypt(plaintext
, salt
);
172 fprintf(stderr
, "rb_crypt() failed\n");
176 if (strcmp(hashed
, hashed2
) != 0)
178 fprintf(stderr
, "Passwords do not match\n");
183 printf("%s\n", hashed
);
188 make_md5_salt_para(char *saltpara
)
190 static char salt
[21];
191 if(saltpara
&& (strlen(saltpara
) <= 16))
193 /* sprintf used because of portability requirements, the length
194 ** is checked above, so it should not be too much of a concern
196 sprintf(salt
, "$1$%s$", saltpara
);
199 printf("Invalid Salt, please use up to 16 random alphanumeric characters\n");
207 make_md5_salt(int length
)
209 static char salt
[21];
212 printf("MD5 salt length too long\n");
218 generate_random_salt(&salt
[3], length
);
219 salt
[length
+ 3] = '$';
220 salt
[length
+ 4] = '\0';
225 make_sha256_salt_para(char *saltpara
)
227 static char salt
[21];
228 if(saltpara
&& (strlen(saltpara
) <= 16))
230 /* sprintf used because of portability requirements, the length
231 ** is checked above, so it should not be too much of a concern
233 sprintf(salt
, "$5$%s$", saltpara
);
236 printf("Invalid Salt, please use up to 16 random alphanumeric characters\n");
244 make_sha512_salt_para(char *saltpara
)
246 static char salt
[21];
247 if(saltpara
&& (strlen(saltpara
) <= 16))
249 /* sprintf used because of portability requirements, the length
250 ** is checked above, so it should not be too much of a concern
252 sprintf(salt
, "$6$%s$", saltpara
);
255 printf("Invalid Salt, please use up to 16 random alphanumeric characters\n");
264 make_sha256_salt(int length
)
266 static char salt
[21];
269 printf("SHA256 salt length too long\n");
275 generate_random_salt(&salt
[3], length
);
276 salt
[length
+ 3] = '$';
277 salt
[length
+ 4] = '\0';
282 make_sha512_salt(int length
)
284 static char salt
[21];
287 printf("SHA512 salt length too long\n");
293 generate_random_salt(&salt
[3], length
);
294 salt
[length
+ 3] = '$';
295 salt
[length
+ 4] = '\0';
300 make_bf_salt_para(int rounds
, char *saltpara
)
302 static char salt
[31];
304 if(saltpara
&& (strlen(saltpara
) <= 22))
306 /* sprintf used because of portability requirements, the length
307 ** is checked above, so it should not be too much of a concern
309 sprintf(tbuf
, "%02d", rounds
);
310 sprintf(salt
, "$2a$%s$%s$", tbuf
, saltpara
);
313 printf("Invalid Salt, please use up to 22 random alphanumeric characters\n");
321 make_bf_salt(int rounds
, int length
)
323 static char salt
[31];
327 printf("Blowfish salt length too long\n");
330 sprintf(tbuf
, "%02d", rounds
);
331 sprintf(salt
, "$2a$%s$", tbuf
);
332 generate_random_salt(&salt
[7], length
);
333 salt
[length
+ 7] = '$';
334 salt
[length
+ 8] = '\0';
339 generate_poor_salt(char *salt
, int length
)
343 for(i
= 0; i
< length
; i
++)
345 salt
[i
] = saltChars
[rand() % 64];
351 generate_random_salt(char *salt
, int length
)
355 if((fd
= open("/dev/random", O_RDONLY
)) < 0)
357 return (generate_poor_salt(salt
, length
));
359 buf
= calloc(1, length
);
360 if(read(fd
, buf
, length
) != length
)
363 return (generate_poor_salt(salt
, length
));
366 for(i
= 0; i
< length
; i
++)
368 salt
[i
] = saltChars
[abs(buf
[i
]) % 64];
377 printf("mkpasswd [-m|-b|-x|-y] [-l saltlength] [-r rounds] [-s salt] [-p plaintext]\n");
378 printf("-x Generate a SHA256 password\n");
379 printf("-y Generate a SHA512 password\n");
380 printf("-m Generate an MD5 password\n");
381 printf("-b Generate a Blowfish password\n");
382 printf("-l Specify a length for a random MD5 or Blowfish salt\n");
383 printf("-r Specify a number of rounds for a Blowfish password\n");
384 printf(" Default 4, no more than 6 recommended\n");
385 printf("-s Specify a salt, up to 16 for MD5, SHA256, and SHA512\n");
386 printf(" up to 22 for Blowfish\n");
387 printf("-p Specify a plaintext password to use\n");
388 printf("Example: mkpasswd -m -s 3dr -p test\n");
395 printf("mkpasswd - password hash generator\n");
396 printf(" SHA512: mkpasswd [-y] [-l saltlength] [-s salt] [-p plaintext]\n");
397 printf(" SHA256: mkpasswd -x [-l saltlength] [-s salt] [-p plaintext]\n");
398 printf(" MD5: mkpasswd -m [-l saltlength] [-s salt] [-p plaintext]\n");
399 printf("Blowfish: mkpasswd -b [-r rounds] [-l saltlength] [-s salt]\n");
400 printf(" [-p plaintext]\n");
401 printf("Use -h for full usage\n");