flatulent 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (69) hide show
  1. data/README +60 -16
  2. data/flatulent-0.0.2.gem +0 -0
  3. data/lib/flatulent/crypt/blowfish-tables.rb +190 -0
  4. data/lib/flatulent/crypt/blowfish.rb +109 -0
  5. data/lib/flatulent/crypt/cbc.rb +123 -0
  6. data/lib/flatulent/crypt/gost.rb +140 -0
  7. data/lib/flatulent/crypt/idea.rb +193 -0
  8. data/lib/flatulent/crypt/noise.rb +94 -0
  9. data/lib/flatulent/crypt/purerubystringio.rb +378 -0
  10. data/lib/flatulent/crypt/rijndael-tables.rb +117 -0
  11. data/lib/flatulent/crypt/rijndael.rb +269 -0
  12. data/lib/flatulent/crypt/stringxor.rb +27 -0
  13. data/lib/flatulent.rb +332 -121
  14. data/lib/flatulent.rb.bak +337 -0
  15. data/rails/app/controllers/flatulent_controller.rb +61 -6
  16. data/rails/lib/flatulent/attributes.rb +79 -0
  17. data/rails/lib/flatulent/crypt/blowfish-tables.rb +190 -0
  18. data/rails/lib/flatulent/crypt/blowfish.rb +109 -0
  19. data/rails/lib/flatulent/crypt/cbc.rb +123 -0
  20. data/rails/lib/flatulent/crypt/gost.rb +140 -0
  21. data/rails/lib/flatulent/crypt/idea.rb +193 -0
  22. data/rails/lib/flatulent/crypt/noise.rb +94 -0
  23. data/rails/lib/flatulent/crypt/purerubystringio.rb +378 -0
  24. data/rails/lib/flatulent/crypt/rijndael-tables.rb +117 -0
  25. data/rails/lib/flatulent/crypt/rijndael.rb +269 -0
  26. data/rails/lib/flatulent/fontfiles/banner.flf +2494 -0
  27. data/rails/lib/flatulent/fontfiles/big.flf +2204 -0
  28. data/rails/lib/flatulent/fontfiles/block.flf +1691 -0
  29. data/rails/lib/flatulent/fontfiles/bubble.flf +1630 -0
  30. data/rails/lib/flatulent/fontfiles/digital.flf +1286 -0
  31. data/rails/lib/flatulent/fontfiles/ivrit.flf +900 -0
  32. data/rails/lib/flatulent/fontfiles/lean.flf +1691 -0
  33. data/rails/lib/flatulent/fontfiles/mini.flf +899 -0
  34. data/rails/lib/flatulent/fontfiles/mnemonic.flf +3702 -0
  35. data/rails/lib/flatulent/fontfiles/script.flf +1493 -0
  36. data/rails/lib/flatulent/fontfiles/shadow.flf +1097 -0
  37. data/rails/lib/flatulent/fontfiles/slant.flf +1295 -0
  38. data/rails/lib/flatulent/fontfiles/small.flf +1097 -0
  39. data/rails/lib/flatulent/fontfiles/smscript.flf +1097 -0
  40. data/rails/lib/flatulent/fontfiles/smshadow.flf +899 -0
  41. data/rails/lib/flatulent/fontfiles/smslant.flf +1097 -0
  42. data/rails/lib/flatulent/fontfiles/standard.flf +2227 -0
  43. data/rails/lib/flatulent/fontfiles/term.flf +600 -0
  44. data/rails/lib/flatulent/pervasives.rb +32 -0
  45. data/rails/lib/flatulent/stringxor.rb +27 -0
  46. data/rails/lib/flatulent/text/double_metaphone.rb +356 -0
  47. data/rails/lib/flatulent/text/figlet/font.rb +117 -0
  48. data/rails/lib/flatulent/text/figlet/smusher.rb +64 -0
  49. data/rails/lib/flatulent/text/figlet/typesetter.rb +68 -0
  50. data/rails/lib/flatulent/text/figlet.rb +17 -0
  51. data/rails/lib/flatulent/text/levenshtein.rb +65 -0
  52. data/rails/lib/flatulent/text/metaphone.rb +97 -0
  53. data/rails/lib/flatulent/text/porter_stemming.rb +171 -0
  54. data/rails/lib/flatulent/text/soundex.rb +61 -0
  55. data/rails/lib/flatulent/text.rb +6 -0
  56. data/rails/lib/flatulent.rb +450 -0
  57. data/rails/log/development.log +14297 -0
  58. data/rails/log/fastcgi.crash.log +111 -0
  59. data/rails/log/lighttpd.access.log +3993 -0
  60. data/rails/log/lighttpd.error.log +111 -0
  61. data/rails/tmp/cache/javascripts/prototype.js-gzip-3275912-71260-1183440172 +0 -0
  62. data/rails/tmp/sessions/ruby_sess.32d68bc997054475 +0 -0
  63. data/rails/tmp/sessions/ruby_sess.4694a4b9bdf9bcf4 +0 -0
  64. data/rails/tmp/sessions/ruby_sess.99469fde69043a05 +0 -0
  65. data/rails/tmp/sessions/ruby_sess.a588c0a457345912 +0 -0
  66. data/rails/tmp/sessions/ruby_sess.b3344125a84a3efa +0 -0
  67. data/samples.rb +10 -0
  68. metadata +69 -3
  69. data/flatulent-0.0.0.gem +0 -0
@@ -0,0 +1,269 @@
1
+ # rijndael.rb Richard Kernahan <kernighan_rich@rubyforge.org>
2
+
3
+ # Adapted from the reference C implementation:
4
+ # rijndael-alg-ref.c v2.2 March 2002
5
+ # Reference ANSI C code
6
+ # authors: Paulo Barreto and Vincent Rijmen
7
+ # This code is placed in the public domain.
8
+
9
+ module Crypt
10
+ class Rijndael
11
+
12
+ require 'crypt/cbc'
13
+ include Crypt::CBC
14
+
15
+ require 'crypt/rijndael-tables'
16
+ include Crypt::RijndaelTables
17
+
18
+
19
+ def initialize(userKey, keyBits = 256, blockBits = 128)
20
+ case keyBits
21
+ when 128
22
+ @keyWords = 4
23
+ when 192
24
+ @keyWords = 6
25
+ when 256
26
+ @keyWords = 8
27
+ else raise "The key must be 128, 192, or 256 bits long."
28
+ end
29
+
30
+ case (keyBits >= blockBits) ? keyBits : blockBits
31
+ when 128
32
+ @rounds = 10
33
+ when 192
34
+ @rounds = 12
35
+ when 256
36
+ @rounds = 14
37
+ else raise "The key and block sizes must be 128, 192, or 256 bits long."
38
+ end
39
+
40
+ case blockBits
41
+ when 128
42
+ @blockSize = 16
43
+ @blockWords = 4
44
+ @shiftIndex = 0
45
+ when 192
46
+ @blockSize = 24
47
+ @blockWords = 6
48
+ @shiftIndex = 1
49
+ when 256
50
+ @blockSize = 32
51
+ @blockWords = 8
52
+ @shiftIndex = 2
53
+ else raise "The block size must be 128, 192, or 256 bits long."
54
+ end
55
+
56
+ uk = userKey.unpack('C'*userKey.length)
57
+ maxUsefulSizeOfUserKey = (keyBits/8)
58
+ uk = uk[0..maxUsefulSizeOfUserKey-1] # truncate
59
+ padding = 0
60
+ if (userKey.length < keyBits/8)
61
+ shortfallInUserKey = (keyBits/8 - userKey.length)
62
+ shortfallInUserKey.times { uk << padding }
63
+ end
64
+ @key = [[], [], [], []]
65
+ 0.upto(uk.length-1) { |pos|
66
+ @key[pos % 4][pos / 4] = uk[pos]
67
+ }
68
+ @roundKeys = generate_key_schedule(@key, keyBits, blockBits)
69
+ end
70
+
71
+
72
+ def block_size
73
+ return(@blockSize) # needed for CBC
74
+ end
75
+
76
+
77
+ def mul(a, b)
78
+ if ((a ==0) | (b == 0))
79
+ result = 0
80
+ else
81
+ result = AlogTable[(LogTable[a] + LogTable[b]) % 255]
82
+ end
83
+ return(result)
84
+ end
85
+
86
+
87
+ def add_round_key(blockArray, roundKey)
88
+ 0.upto(3) { |i|
89
+ 0.upto(@blockWords) { |j|
90
+ blockArray[i][j] ^= roundKey[i][j]
91
+ }
92
+ }
93
+ return(blockArray)
94
+ end
95
+
96
+
97
+ def shift_rows(blockArray, direction)
98
+ tmp = []
99
+ 1.upto(3) { |i| # row zero remains unchanged
100
+ 0.upto(@blockWords-1) { |j|
101
+ tmp[j] = blockArray[i][(j + Shifts[@shiftIndex][i][direction]) % @blockWords]
102
+ }
103
+ 0.upto(@blockWords-1) { |j|
104
+ blockArray[i][j] = tmp[j]
105
+ }
106
+ }
107
+ return(blockArray)
108
+ end
109
+
110
+
111
+ def substitution(blockArray, sBox)
112
+ # replace every byte of the input with the byte at that position in the S-box
113
+ 0.upto(3) { |i|
114
+ 0.upto(@blockWords-1) { |j|
115
+ blockArray[i][j] = sBox[blockArray[i][j]]
116
+ }
117
+ }
118
+ return(blockArray)
119
+ end
120
+
121
+
122
+ def mix_columns(blockArray)
123
+ mixed = [[], [], [], []]
124
+ 0.upto(@blockWords-1) { |j|
125
+ 0.upto(3) { |i|
126
+ mixed[i][j] = mul(2,blockArray[i][j]) ^
127
+ mul(3,blockArray[(i + 1) % 4][j]) ^
128
+ blockArray[(i + 2) % 4][j] ^
129
+ blockArray[(i + 3) % 4][j]
130
+ }
131
+ }
132
+ return(mixed)
133
+ end
134
+
135
+
136
+ def inverse_mix_columns(blockArray)
137
+ unmixed = [[], [], [], []]
138
+ 0.upto(@blockWords-1) { |j|
139
+ 0.upto(3) { |i|
140
+ unmixed[i][j] = mul(0xe, blockArray[i][j]) ^
141
+ mul(0xb, blockArray[(i + 1) % 4][j]) ^
142
+ mul(0xd, blockArray[(i + 2) % 4][j]) ^
143
+ mul(0x9, blockArray[(i + 3) % 4][j])
144
+ }
145
+ }
146
+ return(unmixed)
147
+ end
148
+
149
+
150
+ def generate_key_schedule(k, keyBits, blockBits)
151
+ tk = k[0..3][0..@keyWords-1] # using slice to get a copy instead of a reference
152
+ keySched = []
153
+ (@rounds + 1).times { keySched << [[], [], [], []] }
154
+ t = 0
155
+ j = 0
156
+ while ((j < @keyWords) && (t < (@rounds+1)*@blockWords))
157
+ 0.upto(3) { |i|
158
+ keySched[t / @blockWords][i][t % @blockWords] = tk[i][j]
159
+ }
160
+ j += 1
161
+ t += 1
162
+ end
163
+ # while not enough round key material collected, calculate new values
164
+ rconIndex = 0
165
+ while (t < (@rounds+1)*@blockWords)
166
+ 0.upto(3) { |i|
167
+ tk[i][0] ^= S[tk[(i + 1) % 4][@keyWords - 1]]
168
+ }
169
+ tk[0][0] ^= Rcon[rconIndex]
170
+ rconIndex = rconIndex.next
171
+ if (@keyWords != 8)
172
+ 1.upto(@keyWords - 1) { |j|
173
+ 0.upto(3) { |i|
174
+ tk[i][j] ^= tk[i][j-1];
175
+ }
176
+ }
177
+ else
178
+ 1.upto(@keyWords/2 - 1) { |j|
179
+ 0.upto(3) { |i|
180
+ tk[i][j] ^= tk[i][j-1]
181
+ }
182
+ }
183
+ 0.upto(3) { |i|
184
+ tk[i][@keyWords/2] ^= S[tk[i][@keyWords/2 - 1]]
185
+ }
186
+ (@keyWords/2 + 1).upto(@keyWords - 1) { |j|
187
+ 0.upto(3) { |i|
188
+ tk[i][j] ^= tk[i][j-1]
189
+ }
190
+ }
191
+ end
192
+ j = 0
193
+ while ((j < @keyWords) && (t < (@rounds+1) * @blockWords))
194
+ 0.upto(3) { |i|
195
+ keySched[t / @blockWords][i][t % @blockWords] = tk[i][j]
196
+ }
197
+ j += 1
198
+ t += 1
199
+ end
200
+ end
201
+ return(keySched)
202
+ end
203
+
204
+
205
+ def encrypt_byte_array(blockArray)
206
+ blockArray = add_round_key(blockArray, @roundKeys[0])
207
+ 1.upto(@rounds - 1) { |round|
208
+ blockArray = substitution(blockArray, S)
209
+ blockArray = shift_rows(blockArray, 0)
210
+ blockArray = mix_columns(blockArray)
211
+ blockArray = add_round_key(blockArray, @roundKeys[round])
212
+ }
213
+ # special round without mix_columns
214
+ blockArray = substitution(blockArray,S)
215
+ blockArray = shift_rows(blockArray,0)
216
+ blockArray = add_round_key(blockArray, @roundKeys[@rounds])
217
+ return(blockArray)
218
+ end
219
+
220
+
221
+ def encrypt_block(block)
222
+ raise "block must be #{@blockSize} bytes long" if (block.length() != @blockSize)
223
+ blockArray = [[], [], [], []]
224
+ 0.upto(@blockSize - 1) { |pos|
225
+ blockArray[pos % 4][pos / 4] = block[pos]
226
+ }
227
+ encryptedBlock = encrypt_byte_array(blockArray)
228
+ encrypted = ""
229
+ 0.upto(@blockSize - 1) { |pos|
230
+ encrypted << encryptedBlock[pos % 4][pos / 4]
231
+ }
232
+ return(encrypted)
233
+ end
234
+
235
+
236
+ def decrypt_byte_array(blockArray)
237
+ # first special round without inverse_mix_columns
238
+ # add_round_key is an involution - applying it a second time returns the original result
239
+ blockArray = add_round_key(blockArray, @roundKeys[@rounds])
240
+ blockArray = substitution(blockArray,Si) # using inverse S-box
241
+ blockArray = shift_rows(blockArray,1)
242
+ (@rounds-1).downto(1) { |round|
243
+ blockArray = add_round_key(blockArray, @roundKeys[round])
244
+ blockArray = inverse_mix_columns(blockArray)
245
+ blockArray = substitution(blockArray, Si)
246
+ blockArray = shift_rows(blockArray, 1)
247
+ }
248
+ blockArray = add_round_key(blockArray, @roundKeys[0])
249
+ return(blockArray)
250
+ end
251
+
252
+
253
+ def decrypt_block(block)
254
+ raise "block must be #{@blockSize} bytes long" if (block.length() != @blockSize)
255
+ blockArray = [[], [], [], []]
256
+ 0.upto(@blockSize - 1) { |pos|
257
+ blockArray[pos % 4][pos / 4] = block[pos]
258
+ }
259
+ decryptedBlock = decrypt_byte_array(blockArray)
260
+ decrypted = ""
261
+ 0.upto(@blockSize - 1) { |pos|
262
+ decrypted << decryptedBlock[pos % 4][pos / 4]
263
+ }
264
+ return(decrypted)
265
+ end
266
+
267
+
268
+ end
269
+ end