1 /* $NetBSD: pwhash.c,v 1.15 2011/09/16 15:39:28 joerg Exp $ */
2 /* $OpenBSD: encrypt.c,v 1.16 2002/02/16 21:27:45 millert Exp $ */
5 * Copyright (c) 1996, Jason Downs. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS
17 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT,
20 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
31 __RCSID("$NetBSD: pwhash.c,v 1.15 2011/09/16 15:39:28 joerg Exp $");
34 #include <sys/types.h>
44 #include <login_cap.h>
48 * Very simple little program, for encrypting passwords from the command
49 * line. Useful for scripts and such.
63 "Usage: %s [-km] [-b rounds] [-S rounds] [-s salt] [-p | string]\n",
73 for (ptr
= &line
[strlen(line
) - 1]; ptr
> line
; ptr
--) {
74 if (!isspace((unsigned char)*ptr
))
79 for (ptr
= line
; *ptr
&& isspace((unsigned char)*ptr
); ptr
++)
86 print_passwd(char *string
, int operation
, const char *extra
)
88 char buf
[_PASSWORD_LEN
];
89 char option
[LINE_MAX
], *key
, *opt
;
91 const char *salt
= buf
;
96 * makekey mode: parse string into separate DES key and salt.
98 if (strlen(string
) != 10) {
99 /* To be compatible... */
107 error
= pw_gensalt(buf
, _PASSWORD_LEN
, "md5", extra
);
111 error
= pw_gensalt(buf
, _PASSWORD_LEN
, "sha1", extra
);
115 error
= pw_gensalt(buf
, _PASSWORD_LEN
, "blowfish", extra
);
123 pw_getconf(option
, sizeof(option
), "default", "localcipher");
125 key
= strsep(&opt
, ",");
126 error
= pw_gensalt(buf
, _PASSWORD_LEN
, key
, opt
);
131 err(1, "Cannot generate salt");
133 (void)fputs(crypt(string
, salt
), stdout
);
137 main(int argc
, char **argv
)
142 const char *extra
= NULL
; /* Store salt or number of rounds */
144 setprogname(argv
[0]);
146 if (strcmp(getprogname(), "makekey") == 0)
147 operation
= DO_MAKEKEY
;
149 while ((opt
= getopt(argc
, argv
, "kmpS:s:b:")) != -1) {
151 case 'k': /* Stdin/Stdout Unix crypt */
152 if (operation
!= -1 || prompt
)
154 operation
= DO_MAKEKEY
;
157 case 'm': /* MD5 password hash */
165 if (operation
== DO_MAKEKEY
)
170 case 'S': /* SHA1 password hash */
177 case 's': /* Unix crypt (DES) */
178 if (operation
!= -1 || optarg
[0] == '$')
184 case 'b': /* Blowfish password hash */
196 if (((argc
- optind
) < 1) || operation
== DO_MAKEKEY
) {
197 char line
[LINE_MAX
], *string
;
200 string
= getpass("Enter string: ");
201 print_passwd(string
, operation
, extra
);
202 (void)fputc('\n', stdout
);
204 /* Encrypt stdin to stdout. */
205 while (!feof(stdin
) &&
206 (fgets(line
, sizeof(line
), stdin
) != NULL
)) {
207 /* Kill the whitesapce. */
212 print_passwd(string
, operation
, extra
);
214 if (operation
== DO_MAKEKEY
) {
215 (void)fflush(stdout
);
218 (void)fputc('\n', stdout
);
224 /* can't combine -p with a supplied string */
228 /* Perhaps it isn't worth worrying about, but... */
229 if ((string
= strdup(argv
[optind
])) == NULL
)
231 /* Wipe the argument. */
232 (void)memset(argv
[optind
], 0, strlen(argv
[optind
]));
234 print_passwd(string
, operation
, extra
);
236 (void)fputc('\n', stdout
);
238 /* Wipe our copy, before we free it. */
239 (void)memset(string
, 0, strlen(string
));