racaptcha 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3102d9e8e9810b9c94ee92f9193e2224658d92cb543cb0a9c93053addbd86dc9
4
- data.tar.gz: ea22b7a3a954aa0d1144d42c338117909f472288600cefff5d551854e80a6580
3
+ metadata.gz: dec1a0d703eb8ccfc53f47566764dc8250d8fcb37612399fe4c52597647b4541
4
+ data.tar.gz: be5d3f05420806f2eacb170f776a2390fb53296873fed58017e6df0fb0e0875e
5
5
  SHA512:
6
- metadata.gz: 204c5511eefe7c6b085d080ea1576ce74c5a9d2eb2bbe99a97dbf449e6108efca44f7fd447950e73226bd3ba27211a82b40a73f9b31ae43aedd685bfa973ab03
7
- data.tar.gz: 4f5a919f5ed63ca87815c0a9950ab93c678a67e522f893cad3a0a9816189235a7e8ce3462f806b6bc0c933371675a361c5eb7ac882e601d631d3c05bee19e406
6
+ metadata.gz: a863263bc814cc5be17262ff1d11a4fc143231ff9e1aad4c7153987a059d0c5a1b33245908546d551031fbbc7726bf045bceef9a50c19287614df9a5d9868985
7
+ data.tar.gz: aa74be8f8891145e26f9e144256e129d2b53eb00bad6a443dba371ae2c42d2ff86e0418dfe81e2e55b0054194d63c47157bdfa3edf0c3f9c44394a433710b699
data/.gitignore CHANGED
@@ -1,3 +1,5 @@
1
+ *.gem
2
+ /.config
1
3
  /.bundle/
2
4
  /.yardoc
3
5
  /_yardoc/
@@ -40,7 +40,45 @@ static char *colors[] = {
40
40
  "\xD5\x00\x00"
41
41
  "\xff\xff\xff" "," "\0\0\0\0" "\xc8\0\x46\0" "\0" "\x04",
42
42
 
43
- // Deep Orange A700 #DD2C00
43
+ // Red #B71C1C
44
+ "GIF89a" "\xc8\0\x46\0" "\x83" "\0\0"
45
+ "\xB7\x1C\x1C"
46
+ "\xB7\x1C\x1C"
47
+ "\xB7\x1C\x1C"
48
+ "\xB7\x1C\x1C"
49
+ "\xB7\x1C\x1C"
50
+ "\xB7\x1C\x1C"
51
+ "\xB7\x1C\x1C"
52
+ "\xB7\x1C\x1C"
53
+ "\xB7\x1C\x1C"
54
+ "\xB7\x1C\x1C"
55
+ "\xB7\x1C\x1C"
56
+ "\xB7\x1C\x1C"
57
+ "\xB7\x1C\x1C"
58
+ "\xB7\x1C\x1C"
59
+ "\xB7\x1C\x1C"
60
+ "\xff\xff\xff" "," "\0\0\0\0" "\xc8\0\x46\0" "\0" "\x04",
61
+
62
+ // Red #E53935
63
+ "GIF89a" "\xc8\0\x46\0" "\x83" "\0\0"
64
+ "\xE5\x39\x35"
65
+ "\xE5\x39\x35"
66
+ "\xE5\x39\x35"
67
+ "\xE5\x39\x35"
68
+ "\xE5\x39\x35"
69
+ "\xE5\x39\x35"
70
+ "\xE5\x39\x35"
71
+ "\xE5\x39\x35"
72
+ "\xE5\x39\x35"
73
+ "\xE5\x39\x35"
74
+ "\xE5\x39\x35"
75
+ "\xE5\x39\x35"
76
+ "\xE5\x39\x35"
77
+ "\xE5\x39\x35"
78
+ "\xE5\x39\x35"
79
+ "\xff\xff\xff" "," "\0\0\0\0" "\xc8\0\x46\0" "\0" "\x04",
80
+
81
+ // Orange A700 #DD2C00
44
82
  "GIF89a" "\xc8\0\x46\0" "\x83" "\0\0"
45
83
  "\xDD\x2C\x00"
46
84
  "\xDD\x2C\x00"
@@ -59,7 +97,140 @@ static char *colors[] = {
59
97
  "\xDD\x2C\x00"
60
98
  "\xff\xff\xff" "," "\0\0\0\0" "\xc8\0\x46\0" "\0" "\x04",
61
99
 
62
- // Blue A700 #2962FF
100
+ // Orange #FF3D00
101
+ "GIF89a" "\xc8\0\x46\0" "\x83" "\0\0"
102
+ "\xFF\x3D\x00"
103
+ "\xFF\x3D\x00"
104
+ "\xFF\x3D\x00"
105
+ "\xFF\x3D\x00"
106
+ "\xFF\x3D\x00"
107
+ "\xFF\x3D\x00"
108
+ "\xFF\x3D\x00"
109
+ "\xFF\x3D\x00"
110
+ "\xFF\x3D\x00"
111
+ "\xFF\x3D\x00"
112
+ "\xFF\x3D\x00"
113
+ "\xFF\x3D\x00"
114
+ "\xFF\x3D\x00"
115
+ "\xFF\x3D\x00"
116
+ "\xFF\x3D\x00"
117
+ "\xff\xff\xff" "," "\0\0\0\0" "\xc8\0\x46\0" "\0" "\x04",
118
+
119
+ // Orange #BF360C
120
+ "GIF89a" "\xc8\0\x46\0" "\x83" "\0\0"
121
+ "\xBF\x36\x0C"
122
+ "\xBF\x36\x0C"
123
+ "\xBF\x36\x0C"
124
+ "\xBF\x36\x0C"
125
+ "\xBF\x36\x0C"
126
+ "\xBF\x36\x0C"
127
+ "\xBF\x36\x0C"
128
+ "\xBF\x36\x0C"
129
+ "\xBF\x36\x0C"
130
+ "\xBF\x36\x0C"
131
+ "\xBF\x36\x0C"
132
+ "\xBF\x36\x0C"
133
+ "\xBF\x36\x0C"
134
+ "\xBF\x36\x0C"
135
+ "\xBF\x36\x0C"
136
+ "\xff\xff\xff" "," "\0\0\0\0" "\xc8\0\x46\0" "\0" "\x04",
137
+
138
+ // Orange #FF6D00
139
+ "GIF89a" "\xc8\0\x46\0" "\x83" "\0\0"
140
+ "\xFF\x6D\x00"
141
+ "\xFF\x6D\x00"
142
+ "\xFF\x6D\x00"
143
+ "\xFF\x6D\x00"
144
+ "\xFF\x6D\x00"
145
+ "\xFF\x6D\x00"
146
+ "\xFF\x6D\x00"
147
+ "\xFF\x6D\x00"
148
+ "\xFF\x6D\x00"
149
+ "\xFF\x6D\x00"
150
+ "\xFF\x6D\x00"
151
+ "\xFF\x6D\x00"
152
+ "\xFF\x6D\x00"
153
+ "\xFF\x6D\x00"
154
+ "\xFF\x6D\x00"
155
+ "\xff\xff\xff" "," "\0\0\0\0" "\xc8\0\x46\0" "\0" "\x04",
156
+
157
+ // Green #00C853
158
+ "GIF89a" "\xc8\0\x46\0" "\x83" "\0\0"
159
+ "\x00\xC8\x53"
160
+ "\x00\xC8\x53"
161
+ "\x00\xC8\x53"
162
+ "\x00\xC8\x53"
163
+ "\x00\xC8\x53"
164
+ "\x00\xC8\x53"
165
+ "\x00\xC8\x53"
166
+ "\x00\xC8\x53"
167
+ "\x00\xC8\x53"
168
+ "\x00\xC8\x53"
169
+ "\x00\xC8\x53"
170
+ "\x00\xC8\x53"
171
+ "\x00\xC8\x53"
172
+ "\x00\xC8\x53"
173
+ "\x00\xC8\x53"
174
+ "\xff\xff\xff" "," "\0\0\0\0" "\xc8\0\x46\0" "\0" "\x04",
175
+
176
+ // Green #00C853
177
+ "GIF89a" "\xc8\0\x46\0" "\x83" "\0\0"
178
+ "\x00\xC8\x53"
179
+ "\x00\xC8\x53"
180
+ "\x00\xC8\x53"
181
+ "\x00\xC8\x53"
182
+ "\x00\xC8\x53"
183
+ "\x00\xC8\x53"
184
+ "\x00\xC8\x53"
185
+ "\x00\xC8\x53"
186
+ "\x00\xC8\x53"
187
+ "\x00\xC8\x53"
188
+ "\x00\xC8\x53"
189
+ "\x00\xC8\x53"
190
+ "\x00\xC8\x53"
191
+ "\x00\xC8\x53"
192
+ "\x00\xC8\x53"
193
+ "\xff\xff\xff" "," "\0\0\0\0" "\xc8\0\x46\0" "\0" "\x04",
194
+
195
+ // Green #64DD17
196
+ "GIF89a" "\xc8\0\x46\0" "\x83" "\0\0"
197
+ "\x64\xDD\x17"
198
+ "\x64\xDD\x17"
199
+ "\x64\xDD\x17"
200
+ "\x64\xDD\x17"
201
+ "\x64\xDD\x17"
202
+ "\x64\xDD\x17"
203
+ "\x64\xDD\x17"
204
+ "\x64\xDD\x17"
205
+ "\x64\xDD\x17"
206
+ "\x64\xDD\x17"
207
+ "\x64\xDD\x17"
208
+ "\x64\xDD\x17"
209
+ "\x64\xDD\x17"
210
+ "\x64\xDD\x17"
211
+ "\x64\xDD\x17"
212
+ "\xff\xff\xff" "," "\0\0\0\0" "\xc8\0\x46\0" "\0" "\x04",
213
+
214
+ // Blue #2E7D32
215
+ "GIF89a" "\xc8\0\x46\0" "\x83" "\0\0"
216
+ "\x2E\x7D\x32"
217
+ "\x2E\x7D\x32"
218
+ "\x2E\x7D\x32"
219
+ "\x2E\x7D\x32"
220
+ "\x2E\x7D\x32"
221
+ "\x2E\x7D\x32"
222
+ "\x2E\x7D\x32"
223
+ "\x2E\x7D\x32"
224
+ "\x2E\x7D\x32"
225
+ "\x2E\x7D\x32"
226
+ "\x2E\x7D\x32"
227
+ "\x2E\x7D\x32"
228
+ "\x2E\x7D\x32"
229
+ "\x2E\x7D\x32"
230
+ "\x2E\x7D\x32"
231
+ "\xff\xff\xff" "," "\0\0\0\0" "\xc8\0\x46\0" "\0" "\x04",
232
+
233
+ // Blue #2962FF
63
234
  "GIF89a" "\xc8\0\x46\0" "\x83" "\0\0"
64
235
  "\x29\x62\xFF"
65
236
  "\x29\x62\xFF"
@@ -78,23 +249,61 @@ static char *colors[] = {
78
249
  "\x29\x62\xFF"
79
250
  "\xff\xff\xff" "," "\0\0\0\0" "\xc8\0\x46\0" "\0" "\x04",
80
251
 
81
- // Pink A700 #C51162
252
+ // Blue #0D47A1
253
+ "GIF89a" "\xc8\0\x46\0" "\x83" "\0\0"
254
+ "\x0D\x47\xA1"
255
+ "\x0D\x47\xA1"
256
+ "\x0D\x47\xA1"
257
+ "\x0D\x47\xA1"
258
+ "\x0D\x47\xA1"
259
+ "\x0D\x47\xA1"
260
+ "\x0D\x47\xA1"
261
+ "\x0D\x47\xA1"
262
+ "\x0D\x47\xA1"
263
+ "\x0D\x47\xA1"
264
+ "\x0D\x47\xA1"
265
+ "\x0D\x47\xA1"
266
+ "\x0D\x47\xA1"
267
+ "\x0D\x47\xA1"
268
+ "\x0D\x47\xA1"
269
+ "\xff\xff\xff" "," "\0\0\0\0" "\xc8\0\x46\0" "\0" "\x04",
270
+
271
+ // Blue #1A237E
82
272
  "GIF89a" "\xc8\0\x46\0" "\x83" "\0\0"
83
- "\xC5\x11\x62"
84
- "\xC5\x11\x62"
85
- "\xC5\x11\x62"
86
- "\xC5\x11\x62"
87
- "\xC5\x11\x62"
88
- "\xC5\x11\x62"
89
- "\xC5\x11\x62"
90
- "\xC5\x11\x62"
91
- "\xC5\x11\x62"
92
- "\xC5\x11\x62"
93
- "\xC5\x11\x62"
94
- "\xC5\x11\x62"
95
- "\xC5\x11\x62"
96
- "\xC5\x11\x62"
97
- "\xC5\x11\x62"
273
+ "\x1A\x23\x7E"
274
+ "\x1A\x23\x7E"
275
+ "\x1A\x23\x7E"
276
+ "\x1A\x23\x7E"
277
+ "\x1A\x23\x7E"
278
+ "\x1A\x23\x7E"
279
+ "\x1A\x23\x7E"
280
+ "\x1A\x23\x7E"
281
+ "\x1A\x23\x7E"
282
+ "\x1A\x23\x7E"
283
+ "\x1A\x23\x7E"
284
+ "\x1A\x23\x7E"
285
+ "\x1A\x23\x7E"
286
+ "\x1A\x23\x7E"
287
+ "\x1A\x23\x7E"
288
+ "\xff\xff\xff" "," "\0\0\0\0" "\xc8\0\x46\0" "\0" "\x04",
289
+
290
+ // Blue #0091EA
291
+ "GIF89a" "\xc8\0\x46\0" "\x83" "\0\0"
292
+ "\x00\x91\xEA"
293
+ "\x00\x91\xEA"
294
+ "\x00\x91\xEA"
295
+ "\x00\x91\xEA"
296
+ "\x00\x91\xEA"
297
+ "\x00\x91\xEA"
298
+ "\x00\x91\xEA"
299
+ "\x00\x91\xEA"
300
+ "\x00\x91\xEA"
301
+ "\x00\x91\xEA"
302
+ "\x00\x91\xEA"
303
+ "\x00\x91\xEA"
304
+ "\x00\x91\xEA"
305
+ "\x00\x91\xEA"
306
+ "\x00\x91\xEA"
98
307
  "\xff\xff\xff" "," "\0\0\0\0" "\xc8\0\x46\0" "\0" "\x04",
99
308
 
100
309
  // Deep Purple A700 #6200EA
@@ -114,5 +323,43 @@ static char *colors[] = {
114
323
  "\x62\x00\xEA"
115
324
  "\x62\x00\xEA"
116
325
  "\x62\x00\xEA"
326
+ "\xff\xff\xff" "," "\0\0\0\0" "\xc8\0\x46\0" "\0" "\x04",
327
+
328
+ // Pink #F50057
329
+ "GIF89a" "\xc8\0\x46\0" "\x83" "\0\0"
330
+ "\xF5\x00\x57"
331
+ "\xF5\x00\x57"
332
+ "\xF5\x00\x57"
333
+ "\xF5\x00\x57"
334
+ "\xF5\x00\x57"
335
+ "\xF5\x00\x57"
336
+ "\xF5\x00\x57"
337
+ "\xF5\x00\x57"
338
+ "\xF5\x00\x57"
339
+ "\xF5\x00\x57"
340
+ "\xF5\x00\x57"
341
+ "\xF5\x00\x57"
342
+ "\xF5\x00\x57"
343
+ "\xF5\x00\x57"
344
+ "\xF5\x00\x57"
345
+ "\xff\xff\xff" "," "\0\0\0\0" "\xc8\0\x46\0" "\0" "\x04",
346
+
347
+ // Deep Purple A700 #4A148C
348
+ "GIF89a" "\xc8\0\x46\0" "\x83" "\0\0"
349
+ "\x49\x14\x8C"
350
+ "\x49\x14\x8C"
351
+ "\x49\x14\x8C"
352
+ "\x49\x14\x8C"
353
+ "\x49\x14\x8C"
354
+ "\x49\x14\x8C"
355
+ "\x49\x14\x8C"
356
+ "\x49\x14\x8C"
357
+ "\x49\x14\x8C"
358
+ "\x49\x14\x8C"
359
+ "\x49\x14\x8C"
360
+ "\x49\x14\x8C"
361
+ "\x49\x14\x8C"
362
+ "\x49\x14\x8C"
363
+ "\x49\x14\x8C"
117
364
  "\xff\xff\xff" "," "\0\0\0\0" "\xc8\0\x46\0" "\0" "\x04"
118
365
  };
@@ -1,7 +1,7 @@
1
1
  // http://github.com/ITikhonov/captcha
2
2
  const int gifsize;
3
- void captcha(unsigned char im[70*200], unsigned char l[8], int length, int i_line, int i_filter);
4
- void makegif(unsigned char im[70*200], unsigned char gif[gifsize], int style);
3
+ void captcha(unsigned char im[70 * 200], unsigned char l[8], int length, int i_line, int i_filter);
4
+ void makegif(unsigned char im[70 * 200], unsigned char gif[gifsize], int style);
5
5
 
6
6
  #include <unistd.h>
7
7
  #include <stdint.h>
@@ -14,75 +14,94 @@ void makegif(unsigned char im[70*200], unsigned char gif[gifsize], int style);
14
14
  #include "colors.h"
15
15
 
16
16
  static int8_t *lt[];
17
- const int gifsize=17646;
17
+ const int gifsize = 17646;
18
18
 
19
- void makegif(unsigned char im[70*200], unsigned char gif[gifsize], int style) {
19
+ void makegif(unsigned char im[70 * 200], unsigned char gif[gifsize], int style)
20
+ {
20
21
  // tag ; widthxheight ; GCT:0:0:7 ; bgcolor + aspect // GCT
21
22
  // Image Separator // left x top // widthxheight // Flags
22
23
  // LZW code size
23
24
  srand(time(NULL));
24
- int color_len = (int) sizeof(colors) / sizeof(colors[0]);
25
+ int color_len = (int)sizeof(colors) / sizeof(colors[0]);
25
26
  int color_idx = rand() % color_len;
26
- if (style == 0) {
27
+ if (style == 0)
28
+ {
27
29
  color_idx = 0;
28
30
  }
29
- memcpy(gif,colors[color_idx],13+48+10+1);
30
-
31
- int x,y;
32
- unsigned char *i=im;
33
- unsigned char *p=gif+13+48+10+1;
34
- for(y=0;y<70;y++) {
35
- *p++=250; // Data length 5*50=250
36
- for(x=0;x<50;x++)
31
+ memcpy(gif, colors[color_idx], 13 + 48 + 10 + 1);
32
+
33
+ int x, y;
34
+ unsigned char *i = im;
35
+ unsigned char *p = gif + 13 + 48 + 10 + 1;
36
+ for (y = 0; y < 70; y++)
37
+ {
38
+ *p++ = 250; // Data length 5*50=250
39
+ for (x = 0; x < 50; x++)
37
40
  {
38
- unsigned char a=i[0]>>4,b=i[1]>>4,c=i[2]>>4,d=i[3]>>4;
39
-
40
- p[0]=16|(a<<5); // bbb10000
41
- p[1]=(a>>3)|64|(b<<7); // b10000xb
42
- p[2]=b>>1; // 0000xbbb
43
- p[3]=1|(c<<1); // 00xbbbb1
44
- p[4]=4|(d<<3); // xbbbb100
45
- i+=4;
46
- p+=5;
41
+ unsigned char a = i[0] >> 4, b = i[1] >> 4, c = i[2] >> 4, d = i[3] >> 4;
42
+
43
+ p[0] = 16 | (a << 5); // bbb10000
44
+ p[1] = (a >> 3) | 64 | (b << 7); // b10000xb
45
+ p[2] = b >> 1; // 0000xbbb
46
+ p[3] = 1 | (c << 1); // 00xbbbb1
47
+ p[4] = 4 | (d << 3); // xbbbb100
48
+ i += 4;
49
+ p += 5;
47
50
  }
48
51
  }
49
52
 
50
53
  // Data length // End of LZW (b10001) // Terminator // GIF End
51
- memcpy(gif+gifsize-4,"\x01" "\x11" "\x00" ";",4);
54
+ memcpy(gif + gifsize - 4, "\x01"
55
+ "\x11"
56
+ "\x00"
57
+ ";",
58
+ 4);
52
59
  }
53
60
 
54
- static const int8_t sw[200]={0, 4, 8, 12, 16, 20, 23, 27, 31, 35, 39, 43, 47, 50, 54, 58, 61, 65, 68, 71, 75, 78, 81, 84, 87, 90, 93, 96, 98, 101, 103, 105, 108, 110, 112, 114, 115, 117, 119, 120, 121, 122, 123, 124, 125, 126, 126, 127, 127, 127, 127, 127, 127, 127, 126, 126, 125, 124, 123, 122, 121, 120, 119, 117, 115, 114, 112, 110, 108, 105, 103, 101, 98, 96, 93, 90, 87, 84, 81, 78, 75, 71, 68, 65, 61, 58, 54, 50, 47, 43, 39, 35, 31, 27, 23, 20, 16, 12, 8, 4, 0, -4, -8, -12, -16, -20, -23, -27, -31, -35, -39, -43, -47, -50, -54, -58, -61, -65, -68, -71, -75, -78, -81, -84, -87, -90, -93, -96, -98, -101, -103, -105, -108, -110, -112, -114, -115, -117, -119, -120, -121, -122, -123, -124, -125, -126, -126, -127, -127, -127, -127, -127, -127, -127, -126, -126, -125, -124, -123, -122, -121, -120, -119, -117, -115, -114, -112, -110, -108, -105, -103, -101, -98, -96, -93, -90, -87, -84, -81, -78, -75, -71, -68, -65, -61, -58, -54, -50, -47, -43, -39, -35, -31, -27, -23, -20, -16, -12, -8, -4};
55
-
56
-
57
- #define MAX(x,y) ((x>y)?(x):(y))
58
-
59
- static int letter(int n, int pos, unsigned char im[70*200], unsigned char swr[200], uint8_t s1, uint8_t s2) {
60
- int8_t *p=lt[n];
61
- unsigned char *r=im+200*16+pos;
62
- unsigned char *i=r;
63
- int sk1=s1+pos;
64
- int sk2=s2+pos;
65
- int mpos=pos;
66
- int row=0;
67
- for(;*p!=-101;p++) {
68
- if(*p<0) {
69
- if(*p==-100) { r+=200; i=r; sk1=s1+pos; row++; continue; }
70
- i+=-*p;
61
+ static const int8_t sw[200] = {0, 4, 8, 12, 16, 20, 23, 27, 31, 35, 39, 43, 47, 50, 54, 58, 61, 65, 68, 71, 75, 78, 81, 84, 87, 90, 93, 96, 98, 101, 103, 105, 108, 110, 112, 114, 115, 117, 119, 120, 121, 122, 123, 124, 125, 126, 126, 127, 127, 127, 127, 127, 127, 127, 126, 126, 125, 124, 123, 122, 121, 120, 119, 117, 115, 114, 112, 110, 108, 105, 103, 101, 98, 96, 93, 90, 87, 84, 81, 78, 75, 71, 68, 65, 61, 58, 54, 50, 47, 43, 39, 35, 31, 27, 23, 20, 16, 12, 8, 4, 0, -4, -8, -12, -16, -20, -23, -27, -31, -35, -39, -43, -47, -50, -54, -58, -61, -65, -68, -71, -75, -78, -81, -84, -87, -90, -93, -96, -98, -101, -103, -105, -108, -110, -112, -114, -115, -117, -119, -120, -121, -122, -123, -124, -125, -126, -126, -127, -127, -127, -127, -127, -127, -127, -126, -126, -125, -124, -123, -122, -121, -120, -119, -117, -115, -114, -112, -110, -108, -105, -103, -101, -98, -96, -93, -90, -87, -84, -81, -78, -75, -71, -68, -65, -61, -58, -54, -50, -47, -43, -39, -35, -31, -27, -23, -20, -16, -12, -8, -4};
62
+
63
+ #define MAX(x, y) ((x > y) ? (x) : (y))
64
+
65
+ static int letter(int n, int pos, unsigned char im[70 * 200], unsigned char swr[200], uint8_t s1, uint8_t s2)
66
+ {
67
+ int8_t *p = lt[n];
68
+ unsigned char *r = im + 200 * 16 + pos;
69
+ unsigned char *i = r;
70
+ int sk1 = s1 + pos;
71
+ int sk2 = s2 + pos;
72
+ int mpos = pos;
73
+ int row = 0;
74
+ for (; *p != -101; p++)
75
+ {
76
+ if (*p < 0)
77
+ {
78
+ if (*p == -100)
79
+ {
80
+ r += 200;
81
+ i = r;
82
+ sk1 = s1 + pos;
83
+ row++;
84
+ continue;
85
+ }
86
+ i += -*p;
71
87
  continue;
72
88
  }
73
89
 
74
- if(sk1>=200) sk1=sk1%200;
75
- int skew=sw[sk1]/16;
76
- sk1+=(swr[pos+i-r]&0x1)+1;
90
+ if (sk1 >= 200)
91
+ sk1 = sk1 % 200;
92
+ int skew = sw[sk1] / 16;
93
+ sk1 += (swr[pos + i - r] & 0x1) + 1;
77
94
 
78
- if(sk2>=200) sk2=sk2%200;
79
- int skewh=sw[sk2]/70;
80
- sk2+=(swr[row]&0x1);
95
+ if (sk2 >= 200)
96
+ sk2 = sk2 % 200;
97
+ int skewh = sw[sk2] / 70;
98
+ sk2 += (swr[row] & 0x1);
81
99
 
82
- unsigned char *x=i+skew*200+skewh;
83
- mpos=MAX(mpos,pos+i-r);
100
+ unsigned char *x = i + skew * 200 + skewh;
101
+ mpos = MAX(mpos, pos + i - r);
84
102
 
85
- if((x-im)<70*200) *x=(*p)<<4;
103
+ if ((x - im) < 70 * 200)
104
+ *x = (*p) << 4;
86
105
  i++;
87
106
  }
88
107
  return mpos + 3;
@@ -92,100 +111,141 @@ static int letter(int n, int pos, unsigned char im[70*200], unsigned char swr[20
92
111
 
93
112
  uint32_t dr[NDOTS];
94
113
 
95
- static void line(unsigned char im[70*200], unsigned char swr[200], uint8_t s1) {
114
+ static void line(unsigned char im[70 * 200], unsigned char swr[200], uint8_t s1)
115
+ {
96
116
  int x;
97
- int sk1=s1;
98
- for(x=0;x<199;x++) {
99
- if(sk1>=200) sk1=sk1%200;
100
- int skew=sw[sk1]/20;
101
- sk1+=swr[x]&0x3+1;
102
- unsigned char *i= im+(200*(45+skew)+x);
103
- i[0]=0; i[1]=0; i[200]=0; i[201]=0;
117
+ int sk1 = s1;
118
+ for (x = 0; x < 199; x++)
119
+ {
120
+ if (sk1 >= 200)
121
+ sk1 = sk1 % 200;
122
+ int skew = sw[sk1] / 20;
123
+ sk1 += swr[x] & 0x3 + 1;
124
+ unsigned char *i = im + (200 * (45 + skew) + x);
125
+ i[0] = 0;
126
+ i[1] = 0;
127
+ i[2] = 0;
128
+ i[3] = 0;
129
+ i[4] = 0;
130
+ i[195] = 0;
131
+ i[196] = 0;
132
+ i[197] = 0;
133
+ i[198] = 0;
134
+ i[200] = 0;
104
135
  }
105
136
  }
106
137
 
107
- static void dots(unsigned char im[70*200]) {
138
+ static void dots(unsigned char im[70 * 200])
139
+ {
108
140
  int n;
109
- for(n=0;n<NDOTS;n++) {
110
- uint32_t v=dr[n];
111
- unsigned char *i=im+v%(200*67);
112
-
113
- i[0]=0xff;
114
- i[1]=0xff;
115
- i[2]=0xff;
116
- i[200]=0xff;
117
- i[201]=0xff;
118
- i[202]=0xff;
141
+ for (n = 0; n < NDOTS; n++)
142
+ {
143
+ uint32_t v = dr[n];
144
+ unsigned char *i = im + v % (200 * 67);
145
+
146
+ i[0] = 0xff;
147
+ i[1] = 0xff;
148
+ i[2] = 0xff;
149
+ i[200] = 0xff;
150
+ i[201] = 0xff;
151
+ i[202] = 0xff;
119
152
  }
120
153
  }
121
154
 
122
- static void blur(unsigned char im[70*200]) {
123
- unsigned char *i=im;
124
- int x,y;
125
- for(y=0;y<68;y++) {
126
- for(x=0;x<198;x++) {
127
- unsigned int c11=*i,c12=i[1],c21=i[200],c22=i[201];
128
- *i++=((c11+c12+c21+c22)/4);
155
+ static void blur(unsigned char im[70 * 200])
156
+ {
157
+ unsigned char *i = im;
158
+ int x, y;
159
+ for (y = 0; y < 68; y++)
160
+ {
161
+ for (x = 0; x < 198; x++)
162
+ {
163
+ unsigned int c11 = *i, c12 = i[1], c21 = i[200], c22 = i[201];
164
+ *i++ = ((c11 + c12 + c21 + c22) / 4);
129
165
  }
130
166
  }
131
167
  }
132
168
 
133
- static void filter(unsigned char im[70*200]) {
134
- unsigned char om[70*200];
135
- unsigned char *i=im;
136
- unsigned char *o=om;
169
+ static void filter(unsigned char im[70 * 200])
170
+ {
171
+ unsigned char om[70 * 200];
172
+ unsigned char *i = im;
173
+ unsigned char *o = om;
137
174
 
138
- memset(om,0xff,sizeof(om));
175
+ memset(om, 0xff, sizeof(om));
139
176
 
140
- int x,y;
141
- for(y=0;y<70;y++) {
142
- for(x=4;x<200-4;x++) {
143
- if(i[0]>0xf0 && i[1]<0xf0) { o[0]=0; o[1]=0; }
144
- else if(i[0]<0xf0 && i[1]>0xf0) { o[0]=0; o[1]=0; }
177
+ int x, y;
178
+ for (y = 0; y < 70; y++)
179
+ {
180
+ for (x = 4; x < 200 - 4; x++)
181
+ {
182
+ if (i[0] > 0xf0 && i[1] < 0xf0)
183
+ {
184
+ o[0] = 0;
185
+ o[1] = 0;
186
+ }
187
+ else if (i[0] < 0xf0 && i[1] > 0xf0)
188
+ {
189
+ o[0] = 0;
190
+ o[1] = 0;
191
+ }
145
192
 
146
193
  i++;
147
194
  o++;
148
195
  }
149
196
  }
150
197
 
151
- memmove(im,om,sizeof(om));
198
+ memmove(im, om, sizeof(om));
152
199
  }
153
200
 
154
- static const char *letters="abcdafahijklmnopqrstuvwxyz";
201
+ static const char *letters = "abcdafahijklmnopqrstuvwxyz";
155
202
 
156
- void captcha(unsigned char im[70*200], unsigned char l[8], int length, int i_line, int i_filter) {
203
+ void captcha(unsigned char im[70 * 200], unsigned char l[8], int length, int i_line, int i_filter)
204
+ {
157
205
  unsigned char swr[200];
158
- uint8_t s1,s2;
159
-
160
- int f=open("/dev/urandom",O_RDONLY);
161
- read(f,l,5); read(f,swr,200); read(f,dr,sizeof(dr)); read(f,&s1,1); read(f,&s2,1);
206
+ uint8_t s1, s2;
207
+
208
+ int f = open("/dev/urandom", O_RDONLY);
209
+ read(f, l, 5);
210
+ read(f, swr, 200);
211
+ read(f, dr, sizeof(dr));
212
+ read(f, &s1, 1);
213
+ read(f, &s2, 1);
162
214
  close(f);
163
- memset(im,0xff,200*70); s1=s1&0x7f; s2=s2&0x3f;
215
+ memset(im, 0xff, 200 * 70);
216
+ s1 = s1 & 0x7f;
217
+ s2 = s2 & 0x3f;
164
218
 
165
219
  int x;
166
- for(x=0;x<length;x++){
167
- l[x]%=25;
220
+ for (x = 0; x < length; x++)
221
+ {
222
+ l[x] %= 25;
168
223
  }
169
- for(x=length;x<8;x++){
170
- l[length]=0;
224
+ for (x = length; x < 8; x++)
225
+ {
226
+ l[length] = 0;
171
227
  }
172
228
  //l[0]%=25; l[1]%=25; l[2]%=25; l[3]%=25; l[4]=0; // l[4]%=25; l[5]=0;
173
- int p=30;
174
- for(x=0;x<length;x++){
175
- p=letter(l[x],p,im,swr,s1,s2);
229
+ int p = 30;
230
+ for (x = 0; x < length; x++)
231
+ {
232
+ p = letter(l[x], p, im, swr, s1, s2);
176
233
  }
177
234
 
178
- if (i_line == 1) {
179
- line(im,swr,s1);
235
+ if (i_line == 1)
236
+ {
237
+ line(im, swr, s1);
180
238
  }
181
- // dots(im);
182
- if (i_filter == 1) {
239
+ dots(im);
240
+ if (i_filter == 1)
241
+ {
183
242
  blur(im);
184
243
  filter(im);
185
244
  }
186
245
 
187
- for(x=0;x<length;x++){
188
- l[x]=letters[l[x]];
246
+ for (x = 0; x < length; x++)
247
+ {
248
+ l[x] = letters[l[x]];
189
249
  }
190
250
  //l[1]=letters[l[1]]; l[2]=letters[l[2]]; l[3]=letters[l[3]]; //l[4]=letters[l[4]];
191
251
  }
@@ -214,14 +274,16 @@ void Init_racaptcha();
214
274
 
215
275
  VALUE create(VALUE self, VALUE style, VALUE length, VALUE line, VALUE filter);
216
276
 
217
- void Init_racaptcha() {
277
+ void Init_racaptcha()
278
+ {
218
279
  RaCaptcha = rb_define_module("RaCaptcha");
219
280
  rb_define_singleton_method(RaCaptcha, "create", create, 4);
220
281
  }
221
282
 
222
- VALUE create(VALUE self, VALUE style, VALUE length, VALUE line, VALUE filter) {
283
+ VALUE create(VALUE self, VALUE style, VALUE length, VALUE line, VALUE filter)
284
+ {
223
285
  char l[8];
224
- unsigned char im[80*200];
286
+ unsigned char im[80 * 200];
225
287
  unsigned char gif[gifsize];
226
288
  int i_style = FIX2INT(style);
227
289
  int i_length = FIX2INT(length);
@@ -237,4 +299,3 @@ VALUE create(VALUE self, VALUE style, VALUE length, VALUE line, VALUE filter) {
237
299
 
238
300
  return result;
239
301
  }
240
-
data/lib/racaptcha.rb CHANGED
@@ -1,42 +1,55 @@
1
1
  require "active_support/all"
2
- require "racaptcha/version"
3
2
  require "racaptcha/racaptcha"
3
+ require "racaptcha/version"
4
4
  require "racaptcha/configuration"
5
+ require "racaptcha/cache"
5
6
  require "racaptcha/errors/configuration"
6
7
 
7
8
  module RaCaptcha
8
9
  class << self
9
- def setup
10
- yield config
11
- end
12
-
13
10
  def config
14
- @config ||= Configuration.new
11
+ return @config if defined?(@config)
12
+
13
+ @config = Configuration.new
14
+ @config.style = :colorful
15
+ @config.length = 5
16
+ @config.strikethrough = true
17
+ @config.outline = false
18
+ @config.expires_in = 2.minutes
19
+ @config.skip_cache_store_check = false
20
+
21
+ @config.cache_store = :file_store
22
+ @config.cache_store
23
+ @config
15
24
  end
16
25
 
17
- def cache
18
- @cache ||= ActiveSupport::Cache.lookup_store(config.cache_store)
26
+ def setup(&block)
27
+ config.instance_exec(&block)
19
28
  end
20
29
 
21
30
  # generate new captcha without cache
22
31
  def generate
23
32
  style = config.style == :colorful ? 1 : 0
24
33
  length = config.length
34
+
25
35
  unless length.in?(3..7)
26
36
  raise RaCaptcha::Errors::Configuration, 'length config error, value must in 3..7'
27
37
  end
38
+
28
39
  strikethrough = config.strikethrough ? 1 : 0
29
40
  outline = config.outline ? 1 : 0
30
- self.create(style, length, strikethrough, outline)
41
+ create(style, length, strikethrough, outline)
31
42
  end
32
43
 
33
44
  # generate new captcha with cache
34
45
  def generate_captcha(cache_key = nil)
35
- res = generate
36
- _key = generate_cache_key(cache_key)
37
- _val = { code: res[0], time: Time.now.to_i }
38
- cache.write(_key, _val, expires_in: config.expires_in)
39
- [_key, res[0], res[1]]
46
+ res = RaCaptcha.generate
47
+ _val = {
48
+ code: res[0],
49
+ time: Time.now.to_i
50
+ }
51
+ RaCaptcha.cache.write(generate_cache_key(cache_key), _val, expires_in: RaCaptcha.config.expires_in)
52
+ res[1]
40
53
  end
41
54
 
42
55
  # verify captcha
@@ -50,18 +63,18 @@ module RaCaptcha
50
63
  options ||= {}
51
64
 
52
65
  _key = generate_cache_key(options[:cache_key])
53
- store_info = cache.read(_key)
66
+ store_info = RaCaptcha.cache.read(_key)
54
67
  # Make sure move used key
55
- cache.delete(_key) unless options[:keep_cache]
68
+ RaCaptcha.cache.delete(_key) unless options[:keep_cache]
56
69
 
57
70
  # Make sure session exist
58
71
  return false if store_info.blank?
59
72
 
60
73
  # Make sure not expire
61
- return false if (Time.now.to_i - store_info[:time]) > config.expires_in
74
+ return false if (Time.now.to_i - store_info[:time]) > RaCaptcha.config.expires_in
62
75
 
63
76
  # Make sure have captcha
64
- captcha = (options[:captcha] || '').downcase.strip
77
+ captcha = (options[:captcha] || "").downcase.strip
65
78
  return false if captcha.blank?
66
79
 
67
80
  return false if captcha != store_info[:code]
@@ -71,8 +84,9 @@ module RaCaptcha
71
84
 
72
85
  private
73
86
 
74
- def generate_cache_key(cache_key = nil)
75
- cache_key ||= ['racaptcha-token', Digest::SHA256.hexdigest(SecureRandom.uuid)].join(":")
87
+ def generate_cache_key(session_id = nil)
88
+ session_id_digest = Digest::SHA256.hexdigest(session_id.to_s)
89
+ ["racaptcha-token", session_id_digest].join(":")
76
90
  end
77
91
  end
78
92
  end
@@ -1,58 +1,19 @@
1
1
  module RaCaptcha
2
2
  class Configuration
3
3
  # Store Captcha code where, this config more like Rails config.cache_store
4
- # [:null_store, :memory_store, :file_store]
5
- def cache_store
6
- @cache_store ||= [:file_store, "tmp/cache/racaptcha/session"]
7
- end
8
-
9
- def cache_store=(cache_store)
10
- @cache_store = cache_store
11
- end
12
-
13
- # racaptcha expire time, default 2 minutes
14
- def expires_in
15
- @expires_in ||= 2.minutes
16
- end
17
-
18
- def expires_in=(expires_in)
19
- @expires_in = expires_in
20
- end
21
-
4
+ # default: Rails application config.cache_store
5
+ attr_accessor :cache_store
6
+ # rucaptcha expire time, default 2 minutes
7
+ attr_accessor :expires_in
22
8
  # Color style, default: :colorful, allows: [:colorful, :black_white]
23
- def style
24
- @style ||= :colorful
25
- end
26
-
27
- def style=(style)
28
- @style = style
29
- end
30
-
9
+ attr_accessor :style
31
10
  # Chars length: default 5, allows: [3..7]
32
- def length
33
- @length ||= 5
34
- end
35
-
36
- def length=(length)
37
- @length = length
38
- end
39
-
11
+ attr_accessor :length
40
12
  # strikethrough, default: true
41
- def strikethrough
42
- @strikethrough ||= true
43
- end
44
-
45
- def strikethrough=(strikethrough)
46
- @strikethrough = strikethrough
47
- end
48
-
13
+ attr_accessor :strikethrough
49
14
  # outline style for hard mode, default: false
50
- def outline
51
- @outline ||= false
52
- end
53
-
54
- def outline=(outline)
55
- @outline = outline
56
- end
15
+ attr_accessor :outline
16
+ # skip_cache_store_check, default: false
17
+ attr_accessor :skip_cache_store_check
57
18
  end
58
19
  end
@@ -1,3 +1,3 @@
1
- module Racaptcha
2
- VERSION = "0.1.0"
1
+ module RaCaptcha
2
+ VERSION = "0.2.0"
3
3
  end
data/racaptcha.gemspec CHANGED
@@ -5,7 +5,7 @@ require "racaptcha/version"
5
5
 
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = "racaptcha"
8
- spec.version = Racaptcha::VERSION
8
+ spec.version = RaCaptcha::VERSION
9
9
  spec.authors = ["wangrui"]
10
10
  spec.email = ["402399938@qq.com"]
11
11
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: racaptcha
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - wangrui
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-04-15 00:00:00.000000000 Z
11
+ date: 2021-08-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -74,7 +74,7 @@ metadata:
74
74
  homepage_uri: https://github.com/wangrui438/racaptcha
75
75
  source_code_uri: https://github.com/wangrui438/racaptcha
76
76
  changelog_uri: https://github.com/wangrui438/racaptcha/blob/master/CHANGELOG.md
77
- post_install_message:
77
+ post_install_message:
78
78
  rdoc_options: []
79
79
  require_paths:
80
80
  - lib
@@ -89,8 +89,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
89
89
  - !ruby/object:Gem::Version
90
90
  version: '0'
91
91
  requirements: []
92
- rubygems_version: 3.0.3
93
- signing_key:
92
+ rubyforge_project:
93
+ rubygems_version: 2.7.11
94
+ signing_key:
94
95
  specification_version: 4
95
96
  summary: This is a Captcha gem for ruby.
96
97
  test_files: []