libbb: remove glibc-style bloat from sha_crypt. -1130 bytes.
[oweals/busybox.git] / loginutils / cryptpw.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * cryptpw.c
4  *
5  * Cooked from passwd.c by Thomas Lundquist <thomasez@zelow.no>
6  */
7
8 #include "libbb.h"
9
10 #define TESTING 0
11
12 /*
13 set TESTING to 1 and pipe some file through this script
14 if you played with bbox's crypt implementation.
15
16 while read line; do
17         n=`./busybox cryptpw -a des -- "$line"`
18         o=`./busybox_org cryptpw -a des -- "$line"`
19         test "$n" != "$o" && {
20                 echo n="$n"
21                 echo o="$o"
22                 exit
23         }
24         n=`./busybox cryptpw -- "$line"`
25         o=`./busybox_org cryptpw -- "$line"`
26         test "$n" != "$o" && {
27                 echo n="$n"
28                 echo o="$o"
29                 exit
30         }
31 done
32  */
33
34 int cryptpw_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
35 int cryptpw_main(int argc UNUSED_PARAM, char **argv)
36 {
37         char salt[sizeof("$N$") + 16 + TESTING*100];
38         char *opt_a;
39         int opts;
40
41         opts = getopt32(argv, "a:", &opt_a);
42
43         if (opts && opt_a[0] == 'd') {
44                 crypt_make_salt(salt, 2/2, 0);     /* des */
45 #if TESTING
46                 strcpy(salt, "a.");
47 #endif
48         } else {
49                 salt[0] = '$';
50                 salt[1] = '1';
51                 salt[2] = '$';
52 #if !ENABLE_USE_BB_CRYPT || ENABLE_USE_BB_CRYPT_SHA
53                 if (opts && opt_a[0] == 's') {
54                         salt[1] = '5' + (strcmp(opt_a, "sha512") == 0);
55                         crypt_make_salt(salt + 3, 16/2, 0); /* sha */
56 #if TESTING
57                         strcpy(salt, "$5$rounds=5000$toolongsaltstring");
58                         // with "This is just a test" as password, should produce:
59                         // "$5$rounds=5000$toolongsaltstrin$Un/5jzAHMgOGZ5.mWJpuVolil07guHPvOW8mGRcvxa5"
60                         strcpy(salt, "$6$rounds=5000$toolongsaltstring");
61                         // with "This is just a test" as password, should produce:
62                         // "$6$rounds=5000$toolongsaltstrin$lQ8jolhgVRVhY4b5pZKaysCLi0QBxGoNeKQzQ3glMhwllF7oGDZxUhx1yxdYcz/e1JSbq3y6JMxxl8audkUEm0"
63 #endif
64                 } else
65 #endif
66                 {
67                         crypt_make_salt(salt + 3, 8/2, 0); /* md5 */
68 #if TESTING
69                         strcpy(salt + 3, "ajg./bcf");
70 #endif
71                 }
72         }
73
74         puts(pw_encrypt(argv[optind] ? argv[optind] : xmalloc_fgetline(stdin), salt, 1));
75
76         return 0;
77 }