1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
/*
* 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
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "includes.h"
#ifdef HAVE_CRYPT_H
#include <crypt.h> /* needed for crypt() */
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define PASSWORD_LEN 128
#define SALT_LEN 16
static unsigned char itoa64[] = /* 0 ... 63 => ascii - 64 */
"./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
static void to64(char *, long int, int);
static void print_passwd(const char *);
int
main(int argc, char *argv[])
{
char *line;
size_t linesz;
ssize_t linelen;
if (argc > 2) {
fprintf(stderr, "usage: encrypt <string>\n");
return (1);
}
if (argc == 2) {
print_passwd(argv[1]);
return (0);
}
line = NULL;
linesz = 0;
while ((linelen = getline(&line, &linesz, stdin)) != -1) {
if (line[linelen - 1] == '\n')
line[linelen - 1] = '\0';
print_passwd(line);
}
free(line);
return (0);
}
void
print_passwd(const char *string)
{
const char *ids[] = { "2a", "6", "5", "3", "2", "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], arc4random_uniform(0xff), 1);
salt[SALT_LEN] = '\0';
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;
if (strncmp(p, buffer, strlen(buffer)) != 0)
continue;
printf("%s\n", p);
return;
}
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];
v >>= 6;
}
}
|