dolores-shacontest 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Dolores Labs
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,7 @@
1
+ = shacontest
2
+
3
+ Description goes here.
4
+
5
+ == Copyright
6
+
7
+ Copyright (c) 2009 Dolores Labs. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,101 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "shacontest"
8
+ gem.summary = %Q{Generate SHA keys and win an iPhone}
9
+ gem.email = "brian@doloreslabs.com"
10
+ gem.homepage = "http://github.com/dolores/shacontest"
11
+ gem.authors = ["Brian P O'Rourke"]
12
+ gem.extensions = ["ext/dl_contest/extconf.rb"]
13
+ end
14
+
15
+ rescue LoadError
16
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
17
+ puts "This is required only at buildtime."
18
+ end
19
+
20
+ require 'spec/rake/spectask'
21
+ Spec::Rake::SpecTask.new(:spec) do |spec|
22
+ spec.libs << 'lib' << 'spec'
23
+ spec.spec_files = FileList['spec/**/*_spec.rb']
24
+ end
25
+
26
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
27
+ spec.libs << 'lib' << 'spec'
28
+ spec.pattern = 'spec/**/*_spec.rb'
29
+ spec.rcov = true
30
+ end
31
+
32
+ task :default => [:compile, :spec]
33
+
34
+ require 'rake/rdoctask'
35
+ Rake::RDocTask.new do |rdoc|
36
+ if File.exist?('VERSION.yml')
37
+ config = YAML.load(File.read('VERSION.yml'))
38
+ version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
39
+ else
40
+ version = ""
41
+ end
42
+
43
+ rdoc.rdoc_dir = 'rdoc'
44
+ rdoc.title = "shacontest #{version}"
45
+ rdoc.rdoc_files.include('README*')
46
+ rdoc.rdoc_files.include('lib/**/*.rb')
47
+ end
48
+
49
+
50
+ require 'rake/clean'
51
+ extension = 'dl_contest'
52
+ ext = "ext/#{extension}"
53
+ BIN = "*.{bundle,jar,so,o,obj,pdb,lib,def,exp,class}"
54
+ CLEAN.include ["#{ext}/#{BIN}", "lib/**/#{BIN}", "#{ext}/Makefile",
55
+ '**/.*.sw?', '*.gem', '.config', 'pkg']
56
+
57
+
58
+ ext_so = "#{ext}/#{extension}.#{Config::CONFIG['DLEXT']}"
59
+ ext_files = FileList[
60
+ "#{ext}/*.c",
61
+ "#{ext}/*.h",
62
+ "#{ext}/extconf.rb",
63
+ "#{ext}/Makefile",
64
+ "lib"
65
+ ]
66
+
67
+ desc "Builds the #{ext} extension"
68
+ task extension.to_sym => [ "#{ext}/Makefile", ext_so ]
69
+
70
+ file "#{ext}/Makefile" => ["#{ext}/extconf.rb"] do
71
+ Dir.chdir(ext) do ruby "extconf.rb" end
72
+ end
73
+
74
+ file ext_so => ext_files do
75
+ Dir.chdir(ext) do
76
+ sh(RUBY_PLATFORM =~ /mswin/ ? 'nmake' : 'make')
77
+ end
78
+ cp ext_so, "lib"
79
+ end
80
+
81
+ desc "Compiles the ruby extension"
82
+ task :compile => :dl_contest do
83
+ if Dir.glob(File.join('lib', 'dl_contest.*')).length == 0
84
+ STDERR.puts "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
85
+ STDERR.puts "Compilation failed. The build was not"
86
+ STDERR.puts "properly configured to run on this system."
87
+ STDERR.puts "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
88
+ exit(1)
89
+ end
90
+ end
91
+
92
+ desc 'Benchmarks performance'
93
+ task :benchmark => :compile do
94
+ $: << 'lib'
95
+ require 'dl_contest'
96
+ require 'benchmark'
97
+
98
+ Benchmark.bm do |x|
99
+ x.report { Contest.new.benchmark }
100
+ end
101
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.1
data/bin/shacontest ADDED
@@ -0,0 +1,4 @@
1
+ #! /usr/bin/env ruby
2
+ require 'shacontest'
3
+
4
+ Shacontest::CLInterface.new.main
@@ -0,0 +1,43 @@
1
+ #include "ruby.h"
2
+
3
+ static VALUE c_test(VALUE self)
4
+ {
5
+ return test_suite() ? Qtrue : Qfalse;
6
+ }
7
+
8
+ static VALUE c_benchmark(VALUE self)
9
+ {
10
+ return benchmark() ? Qtrue : Qfalse;
11
+ }
12
+
13
+ static VALUE c_best_capitalization(VALUE self, VALUE target, VALUE my_string, VALUE start, VALUE end, VALUE output_freq)
14
+ {
15
+ char *p_target;
16
+ VALUE str_target = StringValue(target);
17
+ p_target = RSTRING(str_target)->ptr;
18
+
19
+ char *p_my_string;
20
+ VALUE str_my_string = StringValue(my_string);
21
+ p_my_string = RSTRING(str_my_string)->ptr;
22
+
23
+ char words[140];
24
+ // this is only safe since we're unicode-free on the rb side
25
+ strcpy(words, p_my_string);
26
+
27
+ long long int l_start = NUM2LL(start);
28
+ long long int l_end = NUM2LL(end);
29
+ long long int l_output_freq = NUM2LL(output_freq);
30
+
31
+ find_best_capitalization(p_target, words, l_start, l_end, l_output_freq);
32
+
33
+ return rb_str_new2( words );
34
+ }
35
+
36
+ VALUE cContest;
37
+
38
+ void Init_dl_contest() {
39
+ cContest = rb_define_class("Contest", rb_cObject);
40
+ rb_define_method(cContest, "test", c_test, 0);
41
+ rb_define_method(cContest, "benchmark", c_benchmark, 0);
42
+ rb_define_method(cContest, "best_capitalization", c_best_capitalization, 5);
43
+ }
@@ -0,0 +1,3 @@
1
+ require 'mkmf'
2
+ $CFLAGS = "-O3 -fno-common -arch ppc -arch i386 -pipe"
3
+ create_makefile 'dl_contest'
@@ -0,0 +1,30 @@
1
+ /*
2
+ jbig2dec
3
+
4
+ Copyright (c) 2002 artofcode LLC.
5
+
6
+ This program is free software; you can redistribute it and/or modify
7
+ it under the terms of the GNU General Public License as published by
8
+ the Free Software Foundation; either version 2 of the License, or
9
+ (at your option) any later version.
10
+
11
+ $Id: os_types.h,v 1.1 2002/07/20 17:23:15 giles Exp $
12
+ */
13
+
14
+ /*
15
+ indirection layer for build and platform-specific definitions
16
+
17
+ in general, this header should insure that the stdint types are
18
+ available, and that any optional compile flags are defined if
19
+ the build system doesn't pass them directly.
20
+ */
21
+
22
+ #ifdef HAVE_CONFIG_H
23
+ #include "config_types.h"
24
+ #elif defined(_WIN32)
25
+ #include "config_win32.h"
26
+ #endif
27
+
28
+ #if defined(HAVE_STDINT_H) || defined(__MACOS__)
29
+ #include <stdint.h>
30
+ #endif
@@ -0,0 +1,585 @@
1
+ #ifndef _SHA1_H
2
+ #define _SHA1_H
3
+ #include <string.h>
4
+
5
+ #define uint8 unsigned char
6
+ #define uint32 unsigned long int
7
+ #define SHA1_DIGEST_SIZE_32 5
8
+ /* self test */
9
+ #include <stdlib.h>
10
+ #include <limits.h>
11
+ #include <stdio.h>
12
+ #include <errno.h>
13
+
14
+ struct sha1_context
15
+ {
16
+ uint32 total[2];
17
+ uint32 state[5];
18
+ uint8 buffer[64];
19
+ };
20
+
21
+ void sha1_starts( struct sha1_context *ctx );
22
+ void sha1_update( struct sha1_context *ctx, uint8 *input, uint32 length );
23
+ void sha1_finish( struct sha1_context *ctx, uint8 digest[20] );
24
+
25
+ #endif /* sha1.h */
26
+
27
+
28
+
29
+ /*
30
+ * FIPS 180-1 compliant SHA-1 implementation,
31
+ * by Christophe Devine <devine@cr0.net>;
32
+ * this program is licensed under the GPL.
33
+ */
34
+
35
+
36
+
37
+ #define GET_UINT32(n,b,i) \
38
+ { \
39
+ (n) = (uint32) ((uint8 *) b)[(i)+3] \
40
+ | (((uint32) ((uint8 *) b)[(i)+2]) << 8) \
41
+ | (((uint32) ((uint8 *) b)[(i)+1]) << 16) \
42
+ | (((uint32) ((uint8 *) b)[(i)] ) << 24); \
43
+ }
44
+
45
+ #define PUT_UINT32(n,b,i) \
46
+ { \
47
+ (((uint8 *) b)[(i)+3]) = (uint8) (((n) ) & 0xFF); \
48
+ (((uint8 *) b)[(i)+2]) = (uint8) (((n) >> 8) & 0xFF); \
49
+ (((uint8 *) b)[(i)+1]) = (uint8) (((n) >> 16) & 0xFF); \
50
+ (((uint8 *) b)[(i)] ) = (uint8) (((n) >> 24) & 0xFF); \
51
+ }
52
+
53
+ void sha1_starts( struct sha1_context *ctx )
54
+ {
55
+ ctx->total[0] = 0;
56
+ ctx->total[1] = 0;
57
+ ctx->state[0] = 0x67452301;
58
+ ctx->state[1] = 0xEFCDAB89;
59
+ ctx->state[2] = 0x98BADCFE;
60
+ ctx->state[3] = 0x10325476;
61
+ ctx->state[4] = 0xC3D2E1F0;
62
+ }
63
+
64
+ void sha1_process( struct sha1_context *ctx, uint8 data[64] )
65
+ {
66
+ uint32 temp, A, B, C, D, E, W[16];
67
+
68
+ GET_UINT32( W[0], data, 0 );
69
+ GET_UINT32( W[1], data, 4 );
70
+ GET_UINT32( W[2], data, 8 );
71
+ GET_UINT32( W[3], data, 12 );
72
+ GET_UINT32( W[4], data, 16 );
73
+ GET_UINT32( W[5], data, 20 );
74
+ GET_UINT32( W[6], data, 24 );
75
+ GET_UINT32( W[7], data, 28 );
76
+ GET_UINT32( W[8], data, 32 );
77
+ GET_UINT32( W[9], data, 36 );
78
+ GET_UINT32( W[10], data, 40 );
79
+ GET_UINT32( W[11], data, 44 );
80
+ GET_UINT32( W[12], data, 48 );
81
+ GET_UINT32( W[13], data, 52 );
82
+ GET_UINT32( W[14], data, 56 );
83
+ GET_UINT32( W[15], data, 60 );
84
+
85
+ #define S(x,n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n)))
86
+
87
+ #define R(t) \
88
+ ( \
89
+ temp = W[(t - 3) & 0x0F] ^ W[(t - 8) & 0x0F] ^ \
90
+ W[(t - 14) & 0x0F] ^ W[ t & 0x0F], \
91
+ ( W[t & 0x0F] = S(temp,1) ) \
92
+ )
93
+
94
+ #define P(a,b,c,d,e,x) \
95
+ { \
96
+ e += S(a,5) + F(b,c,d) + K + x; b = S(b,30); \
97
+ }
98
+
99
+ A = ctx->state[0];
100
+ B = ctx->state[1];
101
+ C = ctx->state[2];
102
+ D = ctx->state[3];
103
+ E = ctx->state[4];
104
+
105
+ #define F(x,y,z) (z ^ (x & (y ^ z)))
106
+ #define K 0x5A827999
107
+
108
+ P( A, B, C, D, E, W[0] );
109
+ P( E, A, B, C, D, W[1] );
110
+ P( D, E, A, B, C, W[2] );
111
+ P( C, D, E, A, B, W[3] );
112
+ P( B, C, D, E, A, W[4] );
113
+ P( A, B, C, D, E, W[5] );
114
+ P( E, A, B, C, D, W[6] );
115
+ P( D, E, A, B, C, W[7] );
116
+ P( C, D, E, A, B, W[8] );
117
+ P( B, C, D, E, A, W[9] );
118
+ P( A, B, C, D, E, W[10] );
119
+ P( E, A, B, C, D, W[11] );
120
+ P( D, E, A, B, C, W[12] );
121
+ P( C, D, E, A, B, W[13] );
122
+ P( B, C, D, E, A, W[14] );
123
+ P( A, B, C, D, E, W[15] );
124
+ P( E, A, B, C, D, R(16) );
125
+ P( D, E, A, B, C, R(17) );
126
+ P( C, D, E, A, B, R(18) );
127
+ P( B, C, D, E, A, R(19) );
128
+
129
+ #undef K
130
+ #undef F
131
+
132
+ #define F(x,y,z) (x ^ y ^ z)
133
+ #define K 0x6ED9EBA1
134
+
135
+ P( A, B, C, D, E, R(20) );
136
+ P( E, A, B, C, D, R(21) );
137
+ P( D, E, A, B, C, R(22) );
138
+ P( C, D, E, A, B, R(23) );
139
+ P( B, C, D, E, A, R(24) );
140
+ P( A, B, C, D, E, R(25) );
141
+ P( E, A, B, C, D, R(26) );
142
+ P( D, E, A, B, C, R(27) );
143
+ P( C, D, E, A, B, R(28) );
144
+ P( B, C, D, E, A, R(29) );
145
+ P( A, B, C, D, E, R(30) );
146
+ P( E, A, B, C, D, R(31) );
147
+ P( D, E, A, B, C, R(32) );
148
+ P( C, D, E, A, B, R(33) );
149
+ P( B, C, D, E, A, R(34) );
150
+ P( A, B, C, D, E, R(35) );
151
+ P( E, A, B, C, D, R(36) );
152
+ P( D, E, A, B, C, R(37) );
153
+ P( C, D, E, A, B, R(38) );
154
+ P( B, C, D, E, A, R(39) );
155
+
156
+ #undef K
157
+ #undef F
158
+
159
+ #define F(x,y,z) ((x & y) | (z & (x | y)))
160
+ #define K 0x8F1BBCDC
161
+
162
+ P( A, B, C, D, E, R(40) );
163
+ P( E, A, B, C, D, R(41) );
164
+ P( D, E, A, B, C, R(42) );
165
+ P( C, D, E, A, B, R(43) );
166
+ P( B, C, D, E, A, R(44) );
167
+ P( A, B, C, D, E, R(45) );
168
+ P( E, A, B, C, D, R(46) );
169
+ P( D, E, A, B, C, R(47) );
170
+ P( C, D, E, A, B, R(48) );
171
+ P( B, C, D, E, A, R(49) );
172
+ P( A, B, C, D, E, R(50) );
173
+ P( E, A, B, C, D, R(51) );
174
+ P( D, E, A, B, C, R(52) );
175
+ P( C, D, E, A, B, R(53) );
176
+ P( B, C, D, E, A, R(54) );
177
+ P( A, B, C, D, E, R(55) );
178
+ P( E, A, B, C, D, R(56) );
179
+ P( D, E, A, B, C, R(57) );
180
+ P( C, D, E, A, B, R(58) );
181
+ P( B, C, D, E, A, R(59) );
182
+
183
+ #undef K
184
+ #undef F
185
+
186
+ #define F(x,y,z) (x ^ y ^ z)
187
+ #define K 0xCA62C1D6
188
+
189
+ P( A, B, C, D, E, R(60) );
190
+ P( E, A, B, C, D, R(61) );
191
+ P( D, E, A, B, C, R(62) );
192
+ P( C, D, E, A, B, R(63) );
193
+ P( B, C, D, E, A, R(64) );
194
+ P( A, B, C, D, E, R(65) );
195
+ P( E, A, B, C, D, R(66) );
196
+ P( D, E, A, B, C, R(67) );
197
+ P( C, D, E, A, B, R(68) );
198
+ P( B, C, D, E, A, R(69) );
199
+ P( A, B, C, D, E, R(70) );
200
+ P( E, A, B, C, D, R(71) );
201
+ P( D, E, A, B, C, R(72) );
202
+ P( C, D, E, A, B, R(73) );
203
+ P( B, C, D, E, A, R(74) );
204
+ P( A, B, C, D, E, R(75) );
205
+ P( E, A, B, C, D, R(76) );
206
+ P( D, E, A, B, C, R(77) );
207
+ P( C, D, E, A, B, R(78) );
208
+ P( B, C, D, E, A, R(79) );
209
+
210
+ #undef K
211
+ #undef F
212
+
213
+ ctx->state[0] += A;
214
+ ctx->state[1] += B;
215
+ ctx->state[2] += C;
216
+ ctx->state[3] += D;
217
+ ctx->state[4] += E;
218
+ }
219
+
220
+ void sha1_update( struct sha1_context *ctx, uint8 *input, uint32 length )
221
+ {
222
+ uint32 left, fill;
223
+
224
+ if( ! length ) return;
225
+
226
+ left = ( ctx->total[0] >> 3 ) & 0x3F;
227
+ fill = 64 - left;
228
+
229
+ ctx->total[0] += length << 3;
230
+ ctx->total[1] += length >> 29;
231
+
232
+ ctx->total[0] &= 0xFFFFFFFF;
233
+ ctx->total[1] += ctx->total[0] < length << 3;
234
+
235
+ if( left && length >= fill )
236
+ {
237
+ memcpy( (void *) (ctx->buffer + left), (void *) input, fill );
238
+ sha1_process( ctx, ctx->buffer );
239
+ length -= fill;
240
+ input += fill;
241
+ left = 0;
242
+ }
243
+
244
+ while( length >= 64 )
245
+ {
246
+ sha1_process( ctx, input );
247
+ length -= 64;
248
+ input += 64;
249
+ }
250
+
251
+ if( length )
252
+ {
253
+ memcpy( (void *) (ctx->buffer + left), (void *) input, length );
254
+ }
255
+ }
256
+
257
+ static uint8 sha1_padding[64] =
258
+ {
259
+ 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
260
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
261
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
262
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
263
+ };
264
+
265
+ static uint8 hamm_cache[256];
266
+
267
+ void sha1_finish( struct sha1_context *ctx, uint8 digest[16] )
268
+ {
269
+ uint32 last, padn;
270
+ uint8 msglen[8];
271
+
272
+ PUT_UINT32( ctx->total[1], msglen, 0 );
273
+ PUT_UINT32( ctx->total[0], msglen, 4 );
274
+
275
+ last = ( ctx->total[0] >> 3 ) & 0x3F;
276
+ padn = ( last < 56 ) ? ( 56 - last ) : ( 120 - last );
277
+
278
+ sha1_update( ctx, sha1_padding, padn );
279
+ sha1_update( ctx, msglen, 8 );
280
+
281
+ PUT_UINT32( ctx->state[0], digest, 0 );
282
+ PUT_UINT32( ctx->state[1], digest, 4 );
283
+ PUT_UINT32( ctx->state[2], digest, 8 );
284
+ PUT_UINT32( ctx->state[3], digest, 12 );
285
+ PUT_UINT32( ctx->state[4], digest, 16 );
286
+ }
287
+
288
+
289
+ #include <stdlib.h>
290
+ #include <stdio.h>
291
+
292
+ #include <stdint.h>
293
+ int hamming_dist(const uint32_t digest1[SHA1_DIGEST_SIZE_32], const uint32_t digest2[SHA1_DIGEST_SIZE_32]) {
294
+
295
+ int i,j;
296
+ int total_dist = 0;
297
+ for (i = 0; i < SHA1_DIGEST_SIZE_32; i++) {
298
+ uint32_t x = digest1[i];
299
+ uint32_t y = digest2[i];
300
+ unsigned int dist = 0, val = x ^ y;
301
+
302
+ while(val)
303
+ {
304
+ ++dist;
305
+ val &= val - 1;
306
+ }
307
+
308
+ total_dist += dist;
309
+ }
310
+ return 0;//total_dist;
311
+ }
312
+
313
+
314
+
315
+ int build_hamm_cache() {
316
+ int i;
317
+ for(i=0; i<256; i++) {
318
+ int val = i;
319
+ int dist = 0;
320
+ while(val)
321
+ {
322
+ ++dist;
323
+ val &= val - 1;
324
+ }
325
+ hamm_cache[i] = dist;
326
+
327
+ }
328
+
329
+ }
330
+
331
+ int hamming_dist_cached(const uint32_t digest1[SHA1_DIGEST_SIZE_32], const uint32_t digest2[SHA1_DIGEST_SIZE_32] ) {
332
+
333
+ int i,j;
334
+ int total_dist = 0;
335
+ for (i = 0; i < SHA1_DIGEST_SIZE_32; i++) {
336
+ uint32_t x = digest1[i];
337
+ uint32_t y = digest2[i];
338
+ uint32_t val = (x ^ y);
339
+ uint32_t* val_ptr = &val;
340
+
341
+ int d0 = hamm_cache[(int)(((uint8_t*)val_ptr)[0])];
342
+ int d1 = hamm_cache[(int)(((uint8_t*)val_ptr)[1])];
343
+ int d2 = hamm_cache[(int)(((uint8_t*)val_ptr)[2])];
344
+ int d3 = hamm_cache[(int)(((uint8_t*)val_ptr)[3])];
345
+
346
+
347
+ total_dist += d0 + d1 +d2 + d3;
348
+ }
349
+
350
+ return total_dist;
351
+ }
352
+
353
+
354
+
355
+ int test_hamm_cache() {
356
+ uint32_t d1[5];
357
+ uint32_t d2[5];
358
+ d1[0] = 0; d1[1] = 0; d1[2] = 0; d1[3] = 0; d1[4] = 0;
359
+ d2[0] = 0; d2[1] = 0; d2[2] = 0; d2[3] = 0; d2[4] = 0;
360
+
361
+ int a = hamming_dist_cached(d1, d2);
362
+ printf("Hamm Cache Test: %i", a);
363
+ }
364
+
365
+
366
+ int find_best_capitalization(const char* target, char* words, uint64_t start, uint64_t end, uint64_t output_freq) {
367
+ build_hamm_cache();
368
+
369
+ struct sha1_context ctx;
370
+ uint8_t digestR[20];
371
+ char wordsR[10];
372
+ sha1_starts( &ctx );
373
+ sha1_update( &ctx, (uint8 *)target, strlen(target) );
374
+ sha1_finish( &ctx, digestR );
375
+
376
+ //SHA1_Update(&context, (uint8_t*)target, strlen(target));
377
+ //SHA1_Final(&context, digestR);
378
+ int i;
379
+
380
+ printf("Target Sentence: %s\n", target);
381
+ printf("My Sentence: %s\n", words);
382
+ printf("Start %llu\n", start);
383
+ printf("End %llu\n\n", end);
384
+
385
+
386
+ int l = strlen(words);
387
+ uint64_t p = 0; // iterates over all possible capitalizations
388
+
389
+ const char lower_case_mask = 32;
390
+ const char upper_case_mask = 255 - 32;
391
+
392
+ uint8_t digest[20];
393
+ char output[80];
394
+ int min = 100000;
395
+ char argmin[140];
396
+
397
+ // end = 1LL << 60;
398
+
399
+ //SHA1_Init(&context);
400
+ //SHA1_Update(&context, (uint8_t*)words, strlen(words));
401
+ //SHA1_Final(&context, digest);
402
+
403
+ int max_bit = 1;
404
+
405
+ p = start;
406
+
407
+ int z;
408
+ for (z=0;z<63;z++) {
409
+ if (p & (1LL << z)) {
410
+ max_bit = z;
411
+ }
412
+ }
413
+
414
+ sha1_starts( &ctx );
415
+ sha1_update( &ctx, (uint8 *)words, strlen(words) );
416
+ sha1_finish( &ctx, digest );
417
+
418
+ for (p; p < end; p++) {
419
+ //printf("%i\n", max_bit);
420
+
421
+ if (p & (1LL << max_bit)) {
422
+ max_bit++;
423
+ }
424
+
425
+ int offset = 0;
426
+ int i = 0;
427
+ for (i=0; i<=l; i++) {
428
+ //printf("%i", words[i]);
429
+ if (offset > max_bit) {
430
+ break;
431
+ }
432
+
433
+ if (words[i] == 0) { // end of string
434
+ break;
435
+ }
436
+
437
+ if (words[i] & 64) { // it's an ascii character
438
+
439
+ uint64_t mask = 1LL << offset;
440
+ if (p & mask) {// should be uppercase!
441
+ words[i] &= upper_case_mask;
442
+ } else { // should be lowercase!
443
+ words[i] |= lower_case_mask;
444
+ //if (word[i] )
445
+ }
446
+ offset++;
447
+ }
448
+ }
449
+ //printf("%s\n", words);
450
+
451
+ //SHA1_Init(&context);
452
+ //SHA1_Update(&context, (uint8_t*)words, strlen(words));
453
+ //SHA1_Final(&context, digest);
454
+
455
+ sha1_starts( &ctx );
456
+ sha1_update( &ctx, (uint8 *)words, strlen(words) );
457
+ sha1_finish( &ctx, digest );
458
+
459
+
460
+ int h = hamming_dist_cached((uint32_t*)digest, (uint32_t*)digestR);
461
+ if (h<min) {
462
+ min = h;
463
+ strcpy(argmin, words);
464
+ }
465
+
466
+ if (p % output_freq == 0) {
467
+ printf("Offset: %lld Word: %s Score: %i\n", p, argmin, min);
468
+ }
469
+ }
470
+ printf("Finishing...\n\nWord: %s Score: %i\n", argmin, min);
471
+ return min;
472
+ }
473
+
474
+ int test_suite() {
475
+ char t1[140];
476
+ strcpy(t1, "test test test test test abcdefghijklmnopqrstuvwxyz");
477
+ int test1 = find_best_capitalization("test", t1, 0, 1000, 10000000);
478
+ printf("\n\n***TEST 1 (basic test) Value: %i Should be 63\n\n\n", test1);
479
+
480
+ char t2[140];
481
+ strcpy(t2, "test test test test test abcdefghijklmnopqrstuvwxyz");
482
+ int test2 = find_best_capitalization("test", t2, (1LL<<33), (1LL<<33)+100, 10000000);
483
+ printf("\n\n***TEST 2 (64-bit compatibility test) Value: %i Should be 62\n\n\n", test2);
484
+
485
+ char t3[140];
486
+ strcpy(t3, "a12 b34 c56 d78 efgh");
487
+ int test3 = find_best_capitalization("test", t3, 0, 1000, 10000000);
488
+ printf("\n\n***TEST 3 (weird string input test) Value: %i Should be 60\n\n\n", test3);
489
+ }
490
+
491
+
492
+
493
+
494
+
495
+
496
+ int main(int argc, char** argv)
497
+ {
498
+
499
+ long long int output_freq;
500
+ char *endptr;
501
+ //test_best_hamming_dist();
502
+ char words[140];
503
+ if (argc == 6) {
504
+ strcpy(words, argv[2]);
505
+ //fprintf(stderr, "Here");
506
+ errno = 0;
507
+ long long int start = strtoll(argv[3], &endptr, 10);
508
+ //fprintf(stderr, "%i", errno);
509
+ //fprintf(stderr, "L\n\n%s\nVAL: %llu\n\n %lld %s\n\n", argv[3], start, endptr);
510
+ long long int end = strtoll(argv[4], &endptr, 10);
511
+ //fprintf(stderr, "O");
512
+ output_freq = strtoll(argv[5], &endptr, 10);
513
+
514
+ //uint64_t start = 0;
515
+ //uint64_t end = 1LL << 50;
516
+ //fprintf(stderr,"ere\n");
517
+ find_best_capitalization(argv[1], words, start, end, output_freq );
518
+ } else if (argc == 1) {
519
+ //test_hamm_cache();
520
+ test_suite();
521
+ } else {
522
+ printf("Usage: sha2k <target-sentence> <modifiable-sentence> <start> <end> <output-freq>\n");
523
+ }
524
+ return 0;
525
+
526
+ //digest_to_hex(digest, output);
527
+ //fprintf(stderr,"\t%s returned\n", output);
528
+ }
529
+
530
+ /*
531
+ int main( void )
532
+ {
533
+ int i, j;
534
+ char output[41], *buf;
535
+ struct sha1_context ctx;
536
+ unsigned char sha1sum[20];
537
+
538
+ for( i = 0; i < 3; i++ )
539
+ {
540
+ sha1_starts( &ctx );
541
+
542
+ if( i < 2 )
543
+ {
544
+ sha1_update( &ctx, (uint8 *) msg[i], strlen( msg[i] ) );
545
+ }
546
+ else
547
+ {
548
+ if( ! ( buf = (char *) malloc( 1000 ) ) )
549
+ {
550
+ perror( "malloc" );
551
+ return( 1 );
552
+ }
553
+
554
+ memset( buf, 'a', 1000 );
555
+
556
+ for( j = 0; j < 1000; j++ )
557
+ {
558
+ sha1_update( &ctx, (uint8 *) buf, 1000 );
559
+ }
560
+
561
+ free( buf );
562
+ }
563
+
564
+ sha1_finish( &ctx, sha1sum );
565
+
566
+ for( j = 0; j < 20; j++ )
567
+ {
568
+ sprintf( output + j * 2, "%02x", sha1sum[j] );
569
+ }
570
+
571
+ printf( "test %d ", i + 1 );
572
+
573
+ if( ! memcmp( output, val[i], 40 ) )
574
+ {
575
+ printf( "passed\n" );
576
+ }
577
+ else
578
+ {
579
+ printf( "failed\n" );
580
+ return( 1 );
581
+ }
582
+ }
583
+
584
+ return( 0 );
585
+ }*/
data/lib/shacontest.rb ADDED
@@ -0,0 +1,137 @@
1
+ require 'cgi'
2
+ require 'dl_contest'
3
+ require 'fileutils'
4
+ require 'net/http'
5
+ require 'optparse'
6
+ require 'uri'
7
+ require 'yaml'
8
+
9
+ CONFIGS = { 'email' => 'email address' }
10
+ module Shacontest
11
+ HOME = 'http://shacontest.doloreslabs.com/'
12
+ SENT = 'six six six six six six six six six six six six'
13
+ REF = 'I am not a big believer in fortune telling'
14
+
15
+ BLOCK_SIZE = 50000000
16
+ REPORT_FREQ = 1000000
17
+
18
+ class Competitor
19
+ def initialize(config)
20
+ @my_id = rando
21
+ @email = config['email']
22
+ puts "Hi, #{@email}. Your temporary worker ID is '#{@my_id}'"
23
+ end
24
+
25
+ def run
26
+ c = Contest.new
27
+ start = 1
28
+ finish = start + BLOCK_SIZE
29
+
30
+ while true
31
+ @best_yet = c.best_capitalization REF, "#{SENT}#{@my_id}", start, finish, REPORT_FREQ
32
+ phone_home
33
+ start = finish + 1
34
+ finish = start + BLOCK_SIZE
35
+ end
36
+ end
37
+
38
+ def phone_home
39
+ url = URI.parse('http://shacontest.doloreslabs.com/')
40
+ res = Net::HTTP.start(url.host, url.port) {|http|
41
+ http.request_get("/submit/#{CGI::escape(@email)}/#{CGI::escape(@best_yet)}")
42
+ }
43
+ end
44
+
45
+ def rando
46
+ chars = [('a'..'z')].map{|i| i.to_a}.flatten;
47
+ string = (0..5).map{ chars[rand(chars.length)] }.join;
48
+ end
49
+ end
50
+
51
+ class Config
52
+ CONFIG_DIR = "#{ENV['HOME']}/.dlabs"
53
+ CONFIG_FILE = "#{CONFIG_DIR}/config"
54
+
55
+ def initialize
56
+ reset
57
+ FileUtils.mkdir_p CONFIG_DIR
58
+ if File.exists?(CONFIG_FILE)
59
+ @config = File.open(CONFIG_FILE) {|yf| YAML.load(yf)}
60
+ end
61
+ end
62
+
63
+ # Accessor for all configuration data
64
+ def [](index)
65
+ @config[index]
66
+ end
67
+
68
+ def reset
69
+ @config = Hash.new
70
+ end
71
+
72
+ def configure
73
+ STDOUT.puts "Dolores Labs Crowd Computing Challenge"
74
+
75
+ CONFIGS.each do |k,v|
76
+ puts "Enter your #{v}"
77
+
78
+ @config[k] = gets.strip
79
+ end
80
+
81
+ File.open(CONFIG_FILE, 'w') {|f| YAML.dump(@config, f)}
82
+ puts "Configuration complete."
83
+ end
84
+
85
+ def configured?
86
+ configured = true
87
+ CONFIGS.each_key do |k|
88
+ configured = false if @config[k].nil?
89
+ end
90
+
91
+ configured
92
+ end
93
+ end
94
+
95
+ class CLInterface
96
+ def initialize
97
+ @config = Config.new
98
+ end
99
+
100
+ def main
101
+ interactive = true
102
+
103
+ opts = OptionParser.new do |opts|
104
+ opts.banner = <<-EOF
105
+ Dolores Labs Crowd Computing Challenge
106
+
107
+ Usage #{$0} [options]
108
+
109
+ EOF
110
+
111
+ opts.on('-h', '--help', 'Show this usage information') do
112
+ puts opts
113
+ return 0
114
+ end
115
+ opts.on('-i', '--interactive', 'Force interactive setup.') do
116
+ @config.reset
117
+ end
118
+ end
119
+
120
+ begin
121
+ opts.parse!(ARGV)
122
+ rescue OptionParser::ParseError => e
123
+ STDERR.puts e.message, $/
124
+ STDERR.puts opts
125
+ return 1
126
+ end
127
+
128
+ if (interactive)
129
+ # Check that we have all the user input needed
130
+ @config.configure unless @config.configured?
131
+
132
+ # Kick off the update
133
+ Competitor.new(@config).run
134
+ end
135
+ end
136
+ end
137
+ end
@@ -0,0 +1,54 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{shacontest}
5
+ s.version = "0.1.1"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Brian P O'Rourke"]
9
+ s.date = %q{2009-07-19}
10
+ s.default_executable = %q{shacontest}
11
+ s.email = %q{brian@doloreslabs.com}
12
+ s.executables = ["shacontest"]
13
+ s.extensions = ["ext/dl_contest/extconf.rb"]
14
+ s.extra_rdoc_files = [
15
+ "LICENSE",
16
+ "README.rdoc"
17
+ ]
18
+ s.files = [
19
+ ".document",
20
+ ".gitignore",
21
+ "LICENSE",
22
+ "README.rdoc",
23
+ "Rakefile",
24
+ "VERSION",
25
+ "bin/shacontest",
26
+ "ext/dl_contest/dl_contest.c",
27
+ "ext/dl_contest/extconf.rb",
28
+ "ext/dl_contest/os_types.h",
29
+ "ext/dl_contest/sha1b.c",
30
+ "lib/shacontest.rb",
31
+ "shacontest.gemspec",
32
+ "spec/shacontest_spec.rb",
33
+ "spec/spec_helper.rb"
34
+ ]
35
+ s.homepage = %q{http://github.com/dolores/shacontest}
36
+ s.rdoc_options = ["--charset=UTF-8"]
37
+ s.require_paths = ["lib"]
38
+ s.rubygems_version = %q{1.3.3}
39
+ s.summary = %q{Generate SHA keys and win an iPhone}
40
+ s.test_files = [
41
+ "spec/shacontest_spec.rb",
42
+ "spec/spec_helper.rb"
43
+ ]
44
+
45
+ if s.respond_to? :specification_version then
46
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
47
+ s.specification_version = 3
48
+
49
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
50
+ else
51
+ end
52
+ else
53
+ end
54
+ end
@@ -0,0 +1,28 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+ require 'benchmark'
3
+
4
+ describe Shacontest do
5
+ it "exists" do
6
+ Shacontest.nil?.should == false
7
+ end
8
+ end
9
+
10
+ describe Contest do
11
+ it 'has an internal test suite that runs correctly' do
12
+ Contest.new.test.should == true
13
+ end
14
+
15
+ it 'finds best capitalization' do
16
+ c = Contest.new
17
+ string = c.best_capitalization 'This is a candidate sentence', 'my case will be manipulated to get better output', 1, 5000000, 200000
18
+ string.should == 'MY CASE wiLL bE maNipuLAteD to get better output'
19
+ end
20
+
21
+ it 'has no egregious memory leaks' do
22
+ 1.times do |i|
23
+ #1000000.times do |i|
24
+ c = Contest.new
25
+ c.best_capitalization 'This is a candidate sentence', 'here is a set of characters to manipulate', 1, 100, 1000
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,9 @@
1
+ require 'spec'
2
+
3
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
4
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
5
+ require 'shacontest'
6
+
7
+ Spec::Runner.configure do |config|
8
+
9
+ end
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dolores-shacontest
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Brian P O'Rourke
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-07-19 00:00:00 -07:00
13
+ default_executable: shacontest
14
+ dependencies: []
15
+
16
+ description:
17
+ email: brian@doloreslabs.com
18
+ executables:
19
+ - shacontest
20
+ extensions:
21
+ - ext/dl_contest/extconf.rb
22
+ extra_rdoc_files:
23
+ - LICENSE
24
+ - README.rdoc
25
+ files:
26
+ - .document
27
+ - .gitignore
28
+ - LICENSE
29
+ - README.rdoc
30
+ - Rakefile
31
+ - VERSION
32
+ - bin/shacontest
33
+ - ext/dl_contest/dl_contest.c
34
+ - ext/dl_contest/extconf.rb
35
+ - ext/dl_contest/os_types.h
36
+ - ext/dl_contest/sha1b.c
37
+ - lib/shacontest.rb
38
+ - shacontest.gemspec
39
+ - spec/shacontest_spec.rb
40
+ - spec/spec_helper.rb
41
+ has_rdoc: false
42
+ homepage: http://github.com/dolores/shacontest
43
+ post_install_message:
44
+ rdoc_options:
45
+ - --charset=UTF-8
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ requirements:
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: "0"
53
+ version:
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "0"
59
+ version:
60
+ requirements: []
61
+
62
+ rubyforge_project:
63
+ rubygems_version: 1.2.0
64
+ signing_key:
65
+ specification_version: 3
66
+ summary: Generate SHA keys and win an iPhone
67
+ test_files:
68
+ - spec/shacontest_spec.rb
69
+ - spec/spec_helper.rb