File: encrypt.c

package info (click to toggle)
opensmtpd 7.8.0p0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,208 kB
  • sloc: ansic: 54,930; sh: 5,497; yacc: 3,162; makefile: 436; awk: 339
file content (130 lines) | stat: -rw-r--r-- 2,987 bytes parent folder | download | duplicates (3)
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
 * 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>

#ifndef CRYPT_GENSALT_IMPLEMENTS_DEFAULT_PREFIX
#define PASSWORD_LEN	128
#endif
#define SALT_LEN	16

struct hashing_method {
	char           *prefix;
	unsigned long	count;
};

static unsigned char itoa64[] =	 /* 0 ... 63 => ascii - 64 */
	"./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
static struct hashing_method ids[] = {
	{"$y$", 8},
	{"$gy$", 8},
	{"$7$", 8},
	{"$2a$", 10},
	{"$2b$", 10},
	{"$2y$", 10},
	{"$6$", 5000},
	{"$5$", 5000},
	{"$3$", 1},
	{"$2$", 10},
	{"$1$", 1000}
};

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     *id;
	char		salt[SALT_LEN+1];
#ifndef CRYPT_GENSALT_IMPLEMENTS_DEFAULT_PREFIX
	char		buffer[PASSWORD_LEN];
#else
	char           *buffer;
#endif
	int		n;
	const char     *p;
	int		nb_ids = sizeof(ids) / sizeof(ids[0]);

	for (n = 0; n < SALT_LEN; ++n)
		to64(&salt[n], arc4random_uniform(0xff), 1);
	salt[SALT_LEN] = '\0';

	for (n = 0; n < nb_ids; n++) {
		id = ids[n].prefix;
#ifndef CRYPT_GENSALT_IMPLEMENTS_DEFAULT_PREFIX
		(void)snprintf(buffer, sizeof buffer, "%s%s$", id, salt);
#else
                buffer = crypt_gensalt(id, ids[n].count, NULL, 0);
#endif
		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;
	}
}