-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtripcode.c
170 lines (129 loc) · 4.09 KB
/
tripcode.c
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>
#ifndef SECURE_TRIP
#ifdef SJIS_CONVERT
#include <iconv.h>
#endif
#include <openssl/des.h>
const char *salt = "................................"
".............../0123456789ABCDEF"
"GABCDEFGHIJKLMNOPQRSTUVWXYZabcde"
"fabcdefghijklmnopqrstuvwxyz....."
"................................"
"................................"
"................................"
"................................";
#else
#include <openssl/sha.h>
#include "salt.h"
extern const unsigned char salt[];
const char *b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdef"
"ghijklmnopqrstuvwxyz0123456789+/";
#endif
int main(int argc, char **argv)
{
#ifndef SECURE_TRIP
char s[3], c_ret[14], *trip = c_ret + 3, *sjis, cap[9];
int k;
#else
char trip[16];
unsigned char *cap, *sectrip_bin;
#endif
int i, j;
size_t l;
if (argc < 2) {
fprintf(stderr, "Usage: %s capcode\n", argv[0]);
return 1;
}
for (i = 1; i < argc; ++i) {
#ifndef SECURE_TRIP
#ifdef SJIS_CONVERT
size_t outleft = 20;
sjis = malloc(20);
memset(sjis, 0, 20);
l = strlen(argv[i]);
iconv(iconv_open("SJIS//IGNORE", "UTF-8"),
&argv[i], &l, &sjis, &outleft);
outleft = 20 - outleft;
sjis[outleft] = 0;
sjis -= outleft;
#else
l = strlen(argv[i]);
sjis = argv[i];
#endif
memset(cap, 0, 9);
for (j = k = 0; j < 8 && k < 8; ++j, ++k) {
switch (sjis[j]) {
case '&':
if (sjis[j + 1] != '#') {
cap[k] = '&';
if (++k < 8) cap[k] = 'a';
if (++k < 8) cap[k] = 'm';
if (++k < 8) cap[k] = 'p';
if (++k < 8) cap[k] = ';';
}
break;
case '"':
cap[k] = '&';
if (++k < 8) cap[k] = 'q';
if (++k < 8) cap[k] = 'u';
if (++k < 8) cap[k] = 'o';
if (++k < 8) cap[k] = 't';
if (++k < 8) cap[k] = ';';
break;
case '\'':
cap[k] = '&';
if (++k < 8) cap[k] = '#';
if (++k < 8) cap[k] = '0';
if (++k < 8) cap[k] = '3';
if (++k < 8) cap[k] = '9';
if (++k < 8) cap[k] = ';';
break;
case '<':
cap[k] = '&';
if (++k < 8) cap[k] = 'l';
if (++k < 8) cap[k] = 't';
if (++k < 8) cap[k] = ';';
break;
case '>':
cap[k] = '&';
if (++k < 8) cap[k] = 'g';
if (++k < 8) cap[k] = 't';
if (++k < 8) cap[k] = ';';
break;
default:
cap[k] = sjis[j];
}
}
cap[k > 8 ? 8 : k] = 0;
l = strlen(cap);
s[0] = l > 1 ? salt[(unsigned char) cap[1]] : l > 0 ? 'H' : '.';
s[1] = l > 2 ? salt[(unsigned char) cap[2]] : l > 1 ? 'H' : '.';
DES_fcrypt(cap, s, c_ret);
trip[10] = 0;
#ifdef SJIS_CONVERT
free(sjis);
#endif
#else
l = strlen(argv[i]);
cap = malloc((l + sizeof(salt)) * sizeof(unsigned char));
memcpy(cap, argv[i], l);
memcpy(cap + l, salt, sizeof(salt));
sectrip_bin = SHA1(cap, l + sizeof(salt), NULL);
free(cap);
for (j = 0; j < 4; ++j) {
trip[j * 4] = b64[sectrip_bin[j * 3] >> 2];
trip[j * 4 + 1] = b64[((sectrip_bin[j * 3] & 3) << 4) |
(sectrip_bin[j * 3 + 1] >> 4)];
trip[j * 4 + 2] = b64[((sectrip_bin[j * 3 + 1] & 15) << 2) |
(sectrip_bin[j * 3 + 2] >> 6)];
trip[j * 4 + 3] = b64[sectrip_bin[j * 3 + 2] & 63];
}
trip[15] = 0;
#endif
printf("%s\n", trip);
}
return 0;
}