diff options
author | Gilles Chehade <gilles@poolp.org> | 2013-10-27 13:49:16 +0100 |
---|---|---|
committer | Gilles Chehade <gilles@poolp.org> | 2013-10-27 13:49:16 +0100 |
commit | 67db2a90003eaf1bc2e1abba363ebbd13f4068d9 (patch) | |
tree | 9307cebf9de1686216f0ec8d72b6e3dd7641401c /contrib | |
parent | Merge branch 'master' into portable (diff) | |
download | OpenSMTPD-67db2a90003eaf1bc2e1abba363ebbd13f4068d9.tar.xz OpenSMTPD-67db2a90003eaf1bc2e1abba363ebbd13f4068d9.zip |
- improve encrypt utility to support different ciphers
- fix fgetln() loop
Diffstat (limited to 'contrib')
-rw-r--r-- | contrib/libexec/encrypt/encrypt.c | 58 |
1 files changed, 43 insertions, 15 deletions
diff --git a/contrib/libexec/encrypt/encrypt.c b/contrib/libexec/encrypt/encrypt.c index 95d1baf3..e266941f 100644 --- a/contrib/libexec/encrypt/encrypt.c +++ b/contrib/libexec/encrypt/encrypt.c @@ -1,5 +1,6 @@ /* * Copyright (c) 2013 Sunil Nimmagadda <sunil@sunilnimmagadda.com> + * Copyright (c) 2013 Gilles Chehade <gilles@poolp.org> * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -21,6 +22,7 @@ #include "openbsd-compat.h" #define PASSWORD_LEN 128 +#define SALT_LEN 16 static unsigned char itoa64[] = /* 0 ... 63 => ascii - 64 */ "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; @@ -31,21 +33,35 @@ static void print_passwd(const char *); int main(int argc, char *argv[]) { - char *s; + char *buf, *lbuf; size_t len; + if (argc > 2) { + fprintf(stderr, "usage: encrypt <string>\n"); + return (1); + } + if (argc == 2) { print_passwd(argv[1]); return (0); - } else if (argc > 2) { - fprintf(stderr, "usage: encrypt string\n"); - return (1); } - while ((s = fgetln(stdin, &len)) != NULL) { - s[len - 1] = '\0'; - print_passwd(s); + lbuf = NULL; + while ((buf = fgetln(stdin, &len))) { + if (buf[len - 1] == '\n') + buf[len - 1] = '\0'; + else { + if ((lbuf = malloc(len + 1)) == NULL) { + fprintf(stderr, "memory exhausted"); + return (1); + } + memcpy(lbuf, buf, len); + lbuf[len] = '\0'; + buf = lbuf; + } + print_passwd(buf); } + free(lbuf); return (0); } @@ -53,23 +69,35 @@ main(int argc, char *argv[]) void print_passwd(const char *string) { - char *c, salt[PASSWORD_LEN]; + const char *ids[] = { "2a", "6", "5", "1", NULL }; + const char *id; + char salt[SALT_LEN+1]; + char buffer[PASSWORD_LEN]; + int n; + const char *p; + + for (n = 0; n < SALT_LEN; ++n) + to64(&salt[n], chacha_uniform(0xff), 1); + salt[SALT_LEN] = '\0'; - to64(&salt[0], chacha_random(), 2); - salt[2] = '\0'; - if ((c = crypt(string, salt)) == NULL) { - fprintf(stderr, "crypt failed"); - exit(1); + for (n = 0; ids[n]; n++) { + id = ids[n]; + (void)snprintf(buffer, sizeof buffer, "$%s$%s$", id, salt); + if ((p = crypt(string, buffer)) == NULL) + continue; + printf("%s\n", p); + return; } - printf("%s\n", c); + salt[2] = 0; + printf("%s\n", crypt(string, salt)); } void to64(char *s, long int v, int n) { while (--n >= 0) { - *s++ = itoa64[v&0x3f]; + *s++ = itoa64[v & 0x3f]; v >>= 6; } } |