four_bites_aes 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +50 -0
- data/Rakefile +13 -0
- data/VERSION +1 -0
- data/examples/example.rb +23 -0
- data/four_bites_aes.gemspec +46 -0
- data/lib/four_bites_aes.rb +331 -0
- data/spec/basic_spec.rb +16 -0
- metadata +62 -0
data/README.markdown
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
Four Bites AES
|
2
|
+
==============
|
3
|
+
Author: Mark Percival
|
4
|
+
Email: mark@mpercival.com
|
5
|
+
Copyright: Mark Percival 2008
|
6
|
+
License: MIT
|
7
|
+
|
8
|
+
-----------
|
9
|
+
|
10
|
+
This is a simple library that takes any 4 byte integer and encrypts it with a modified version of AES.
|
11
|
+
It's designed with URL shorteners in mind, where brevity is key, and security through obscurity is OK.
|
12
|
+
|
13
|
+
Two goals with this project
|
14
|
+
|
15
|
+
- Keep the total number of records somewhat secret
|
16
|
+
- Prevent people from easily guessing the next record
|
17
|
+
|
18
|
+
mySQL integers are 4 bytes long, so I wanted to account for that, and keep the encrypted number the same length.
|
19
|
+
|
20
|
+
I certainly wouldn't base my security on this system.
|
21
|
+
|
22
|
+
This library isn't designed to prevent sophisticated attacks. It's simple designed to keep your total record count
|
23
|
+
secret, and prevent the average joe/jane from easily guessing the next number, all in the shortest
|
24
|
+
possible manner.
|
25
|
+
|
26
|
+
|
27
|
+
### Details about the code
|
28
|
+
|
29
|
+
I followed the AES standard as much as possible, except I used 8 rounds, and a 256 bit unexpanded key. The ShiftRows
|
30
|
+
method became a rotation of the 4 bytes, and the MixColumns became one single column mix. Other than that, it's essentially
|
31
|
+
the same algorithm.
|
32
|
+
|
33
|
+
## Example code
|
34
|
+
|
35
|
+
class Link < ActiveRecord::Base
|
36
|
+
require 'four_bites_aes'
|
37
|
+
|
38
|
+
def to_param
|
39
|
+
@crypte.encode(self.id)
|
40
|
+
end
|
41
|
+
|
42
|
+
def find_by_aes_id(id)
|
43
|
+
self.find(@crypter.decode(id))
|
44
|
+
end
|
45
|
+
|
46
|
+
def encoder
|
47
|
+
@crypter ||= FourBitesAES.new("supersecret")
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
data/Rakefile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
begin
|
2
|
+
require 'jeweler'
|
3
|
+
Jeweler::Tasks.new do |gemspec|
|
4
|
+
gemspec.name = "four_bites_aes"
|
5
|
+
gemspec.summary = "Obsfuscate your ID's"
|
6
|
+
gemspec.description = "Useful for URL shortners and places where you don't want guessed ID's"
|
7
|
+
gemspec.email = "mark@mpercival.com"
|
8
|
+
gemspec.homepage = "http://github.com/mark/fourbitesaes"
|
9
|
+
gemspec.authors = ["Mark Percival"]
|
10
|
+
end
|
11
|
+
rescue LoadError
|
12
|
+
puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
|
13
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.0.0
|
data/examples/example.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'four_bites_aes'
|
2
|
+
require 'digest/sha2'
|
3
|
+
require 'benchmark'
|
4
|
+
|
5
|
+
# The key should be 256 bits long, a SHA2 hex string for example
|
6
|
+
key = Digest::SHA2.hexdigest("It's only going to encrypt 4 bytes!!!")
|
7
|
+
|
8
|
+
DB_CRYPT = FourBitesAES.new(key)
|
9
|
+
|
10
|
+
n = 1000
|
11
|
+
Benchmark.bm do |x|
|
12
|
+
x.report("Cycle #{n} times") {
|
13
|
+
n.times {
|
14
|
+
enc = DB_CRYPT.enc(n)
|
15
|
+
dec = DB_CRYPT.dec(enc)
|
16
|
+
}
|
17
|
+
}
|
18
|
+
end
|
19
|
+
|
20
|
+
10.times { |i|
|
21
|
+
p enc = DB_CRYPT.enc(i)
|
22
|
+
p dec = DB_CRYPT.dec(enc)
|
23
|
+
}
|
@@ -0,0 +1,46 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{four_bites_aes}
|
8
|
+
s.version = "1.0.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Mark Percival"]
|
12
|
+
s.date = %q{2009-10-10}
|
13
|
+
s.description = %q{Useful for URL shortners and places where you don't want guessed ID's}
|
14
|
+
s.email = %q{mark@mpercival.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"README.markdown"
|
17
|
+
]
|
18
|
+
s.files = [
|
19
|
+
"README.markdown",
|
20
|
+
"Rakefile",
|
21
|
+
"VERSION",
|
22
|
+
"examples/example.rb",
|
23
|
+
"four_bites_aes.gemspec",
|
24
|
+
"lib/four_bites_aes.rb",
|
25
|
+
"spec/basic_spec.rb"
|
26
|
+
]
|
27
|
+
s.homepage = %q{http://github.com/mark/fourbitesaes}
|
28
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
29
|
+
s.require_paths = ["lib"]
|
30
|
+
s.rubygems_version = %q{1.3.5}
|
31
|
+
s.summary = %q{Obsfuscate your ID's}
|
32
|
+
s.test_files = [
|
33
|
+
"spec/basic_spec.rb",
|
34
|
+
"examples/example.rb"
|
35
|
+
]
|
36
|
+
|
37
|
+
if s.respond_to? :specification_version then
|
38
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
39
|
+
s.specification_version = 3
|
40
|
+
|
41
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
42
|
+
else
|
43
|
+
end
|
44
|
+
else
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,331 @@
|
|
1
|
+
require 'digest/sha2'
|
2
|
+
|
3
|
+
class FourBitesAES
|
4
|
+
|
5
|
+
S_BOX = [
|
6
|
+
0x63,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0x40, 0x45, 0x46,
|
99
|
+
0x4f, 0x4c, 0x49, 0x4a, 0x6b, 0x68, 0x6d, 0x6e, 0x67, 0x64, 0x61, 0x62,
|
100
|
+
0x73, 0x70, 0x75, 0x76, 0x7f, 0x7c, 0x79, 0x7a, 0x3b, 0x38, 0x3d, 0x3e,
|
101
|
+
0x37, 0x34, 0x31, 0x32, 0x23, 0x20, 0x25, 0x26, 0x2f, 0x2c, 0x29, 0x2a,
|
102
|
+
0x0b, 0x08, 0x0d, 0x0e, 0x07, 0x04, 0x01, 0x02, 0x13, 0x10, 0x15, 0x16,
|
103
|
+
0x1f, 0x1c, 0x19, 0x1a
|
104
|
+
]
|
105
|
+
|
106
|
+
G9X = [
|
107
|
+
0x00, 0x09, 0x12, 0x1b, 0x24, 0x2d, 0x36, 0x3f, 0x48, 0x41, 0x5a, 0x53,
|
108
|
+
0x6c, 0x65, 0x7e, 0x77, 0x90, 0x99, 0x82, 0x8b, 0xb4, 0xbd, 0xa6, 0xaf,
|
109
|
+
0xd8, 0xd1, 0xca, 0xc3, 0xfc, 0xf5, 0xee, 0xe7, 0x3b, 0x32, 0x29, 0x20,
|
110
|
+
0x1f, 0x16, 0x0d, 0x04, 0x73, 0x7a, 0x61, 0x68, 0x57, 0x5e, 0x45, 0x4c,
|
111
|
+
0xab, 0xa2, 0xb9, 0xb0, 0x8f, 0x86, 0x9d, 0x94, 0xe3, 0xea, 0xf1, 0xf8,
|
112
|
+
0xc7, 0xce, 0xd5, 0xdc, 0x76, 0x7f, 0x64, 0x6d, 0x52, 0x5b, 0x40, 0x49,
|
113
|
+
0x3e, 0x37, 0x2c, 0x25, 0x1a, 0x13, 0x08, 0x01, 0xe6, 0xef, 0xf4, 0xfd,
|
114
|
+
0xc2, 0xcb, 0xd0, 0xd9, 0xae, 0xa7, 0xbc, 0xb5, 0x8a, 0x83, 0x98, 0x91,
|
115
|
+
0x4d, 0x44, 0x5f, 0x56, 0x69, 0x60, 0x7b, 0x72, 0x05, 0x0c, 0x17, 0x1e,
|
116
|
+
0x21, 0x28, 0x33, 0x3a, 0xdd, 0xd4, 0xcf, 0xc6, 0xf9, 0xf0, 0xeb, 0xe2,
|
117
|
+
0x95, 0x9c, 0x87, 0x8e, 0xb1, 0xb8, 0xa3, 0xaa, 0xec, 0xe5, 0xfe, 0xf7,
|
118
|
+
0xc8, 0xc1, 0xda, 0xd3, 0xa4, 0xad, 0xb6, 0xbf, 0x80, 0x89, 0x92, 0x9b,
|
119
|
+
0x7c, 0x75, 0x6e, 0x67, 0x58, 0x51, 0x4a, 0x43, 0x34, 0x3d, 0x26, 0x2f,
|
120
|
+
0x10, 0x19, 0x02, 0x0b, 0xd7, 0xde, 0xc5, 0xcc, 0xf3, 0xfa, 0xe1, 0xe8,
|
121
|
+
0x9f, 0x96, 0x8d, 0x84, 0xbb, 0xb2, 0xa9, 0xa0, 0x47, 0x4e, 0x55, 0x5c,
|
122
|
+
0x63, 0x6a, 0x71, 0x78, 0x0f, 0x06, 0x1d, 0x14, 0x2b, 0x22, 0x39, 0x30,
|
123
|
+
0x9a, 0x93, 0x88, 0x81, 0xbe, 0xb7, 0xac, 0xa5, 0xd2, 0xdb, 0xc0, 0xc9,
|
124
|
+
0xf6, 0xff, 0xe4, 0xed, 0x0a, 0x03, 0x18, 0x11, 0x2e, 0x27, 0x3c, 0x35,
|
125
|
+
0x42, 0x4b, 0x50, 0x59, 0x66, 0x6f, 0x74, 0x7d, 0xa1, 0xa8, 0xb3, 0xba,
|
126
|
+
0x85, 0x8c, 0x97, 0x9e, 0xe9, 0xe0, 0xfb, 0xf2, 0xcd, 0xc4, 0xdf, 0xd6,
|
127
|
+
0x31, 0x38, 0x23, 0x2a, 0x15, 0x1c, 0x07, 0x0e, 0x79, 0x70, 0x6b, 0x62,
|
128
|
+
0x5d, 0x54, 0x4f, 0x46
|
129
|
+
]
|
130
|
+
|
131
|
+
GBX = [
|
132
|
+
0x00, 0x0b, 0x16, 0x1d, 0x2c, 0x27, 0x3a, 0x31, 0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, 0xf8, 0xf3, 0xee, 0xe5, 0x3c, 0x37, 0x2a, 0x21,
|
147
|
+
0x10, 0x1b, 0x06, 0x0d, 0x64, 0x6f, 0x72, 0x79, 0x48, 0x43, 0x5e, 0x55,
|
148
|
+
0x01, 0x0a, 0x17, 0x1c, 0x2d, 0x26, 0x3b, 0x30, 0x59, 0x52, 0x4f, 0x44,
|
149
|
+
0x75, 0x7e, 0x63, 0x68, 0xb1, 0xba, 0xa7, 0xac, 0x9d, 0x96, 0x8b, 0x80,
|
150
|
+
0xe9, 0xe2, 0xff, 0xf4, 0xc5, 0xce, 0xd3, 0xd8, 0x7a, 0x71, 0x6c, 0x67,
|
151
|
+
0x56, 0x5d, 0x40, 0x4b, 0x22, 0x29, 0x34, 0x3f, 0x0e, 0x05, 0x18, 0x13,
|
152
|
+
0xca, 0xc1, 0xdc, 0xd7, 0xe6, 0xed, 0xf0, 0xfb, 0x92, 0x99, 0x84, 0x8f,
|
153
|
+
0xbe, 0xb5, 0xa8, 0xa3
|
154
|
+
]
|
155
|
+
|
156
|
+
GDX = [
|
157
|
+
0x00, 0x0d, 0x1a, 0x17, 0x34, 0x39, 0x2e, 0x23, 0x68, 0x65, 0x72, 0x7f,
|
158
|
+
0x5c, 0x51, 0x46, 0x4b, 0xd0, 0xdd, 0xca, 0xc7, 0xe4, 0xe9, 0xfe, 0xf3,
|
159
|
+
0xb8, 0xb5, 0xa2, 0xaf, 0x8c, 0x81, 0x96, 0x9b, 0xbb, 0xb6, 0xa1, 0xac,
|
160
|
+
0x8f, 0x82, 0x95, 0x98, 0xd3, 0xde, 0xc9, 0xc4, 0xe7, 0xea, 0xfd, 0xf0,
|
161
|
+
0x6b, 0x66, 0x71, 0x7c, 0x5f, 0x52, 0x45, 0x48, 0x03, 0x0e, 0x19, 0x14,
|
162
|
+
0x37, 0x3a, 0x2d, 0x20, 0x6d, 0x60, 0x77, 0x7a, 0x59, 0x54, 0x43, 0x4e,
|
163
|
+
0x05, 0x08, 0x1f, 0x12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0xdc, 0xd1, 0xc6, 0xcb, 0xe8, 0xe5, 0xf2, 0xff, 0xb4, 0xb9, 0xae, 0xa3,
|
178
|
+
0x80, 0x8d, 0x9a, 0x97
|
179
|
+
]
|
180
|
+
|
181
|
+
GEX = [
|
182
|
+
0x00, 0x0e, 0x1c, 0x12, 0x38, 0x36, 0x24, 0x2a, 0x70, 0x7e, 0x6c, 0x62,
|
183
|
+
0x48, 0x46, 0x54, 0x5a, 0xe0, 0xee, 0xfc, 0xf2, 0xd8, 0xd6, 0xc4, 0xca,
|
184
|
+
0x90, 0x9e, 0x8c, 0x82, 0xa8, 0xa6, 0xb4, 0xba, 0xdb, 0xd5, 0xc7, 0xc9,
|
185
|
+
0xe3, 0xed, 0xff, 0xf1, 0xab, 0xa5, 0xb7, 0xb9, 0x93, 0x9d, 0x8f, 0x81,
|
186
|
+
0x3b, 0x35, 0x27, 0x29, 0x03, 0x0d, 0x1f, 0x11, 0x4b, 0x45, 0x57, 0x59,
|
187
|
+
0x73, 0x7d, 0x6f, 0x61, 0xad, 0xa3, 0xb1, 0xbf, 0x95, 0x9b, 0x89, 0x87,
|
188
|
+
0xdd, 0xd3, 0xc1, 0xcf, 0xe5, 0xeb, 0xf9, 0xf7, 0x4d, 0x43, 0x51, 0x5f,
|
189
|
+
0x75, 0x7b, 0x69, 0x67, 0x3d, 0x33, 0x21, 0x2f, 0x05, 0x0b, 0x19, 0x17,
|
190
|
+
0x76, 0x78, 0x6a, 0x64, 0x4e, 0x40, 0x52, 0x5c, 0x06, 0x08, 0x1a, 0x14,
|
191
|
+
0x3e, 0x30, 0x22, 0x2c, 0x96, 0x98, 0x8a, 0x84, 0xae, 0xa0, 0xb2, 0xbc,
|
192
|
+
0xe6, 0xe8, 0xfa, 0xf4, 0xde, 0xd0, 0xc2, 0xcc, 0x41, 0x4f, 0x5d, 0x53,
|
193
|
+
0x79, 0x77, 0x65, 0x6b, 0x31, 0x3f, 0x2d, 0x23, 0x09, 0x07, 0x15, 0x1b,
|
194
|
+
0xa1, 0xaf, 0xbd, 0xb3, 0x99, 0x97, 0x85, 0x8b, 0xd1, 0xdf, 0xcd, 0xc3,
|
195
|
+
0xe9, 0xe7, 0xf5, 0xfb, 0x9a, 0x94, 0x86, 0x88, 0xa2, 0xac, 0xbe, 0xb0,
|
196
|
+
0xea, 0xe4, 0xf6, 0xf8, 0xd2, 0xdc, 0xce, 0xc0, 0x7a, 0x74, 0x66, 0x68,
|
197
|
+
0x42, 0x4c, 0x5e, 0x50, 0x0a, 0x04, 0x16, 0x18, 0x32, 0x3c, 0x2e, 0x20,
|
198
|
+
0xec, 0xe2, 0xf0, 0xfe, 0xd4, 0xda, 0xc8, 0xc6, 0x9c, 0x92, 0x80, 0x8e,
|
199
|
+
0xa4, 0xaa, 0xb8, 0xb6, 0x0c, 0x02, 0x10, 0x1e, 0x34, 0x3a, 0x28, 0x26,
|
200
|
+
0x7c, 0x72, 0x60, 0x6e, 0x44, 0x4a, 0x58, 0x56, 0x37, 0x39, 0x2b, 0x25,
|
201
|
+
0x0f, 0x01, 0x13, 0x1d, 0x47, 0x49, 0x5b, 0x55, 0x7f, 0x71, 0x63, 0x6d,
|
202
|
+
0xd7, 0xd9, 0xcb, 0xc5, 0xef, 0xe1, 0xf3, 0xfd, 0xa7, 0xa9, 0xbb, 0xb5,
|
203
|
+
0x9f, 0x91, 0x83, 0x8d
|
204
|
+
]
|
205
|
+
|
206
|
+
def initialize(key)
|
207
|
+
hexkey = Digest::SHA2.hexdigest(key)
|
208
|
+
@key = []
|
209
|
+
(0..64).step(2) do |start|
|
210
|
+
@key << hexkey[start,2].to_i(16)
|
211
|
+
end
|
212
|
+
end
|
213
|
+
|
214
|
+
|
215
|
+
def enc(number)
|
216
|
+
# Takes a 4-byte number and encrypts it with standard AES with a 256 bit key string
|
217
|
+
state = integer_to_hex_array(number.to_i)
|
218
|
+
|
219
|
+
state = xor_block(state, @key.slice(0,4))
|
220
|
+
|
221
|
+
1.upto(7) do |i|
|
222
|
+
state = sub_bytes(state)
|
223
|
+
state = rotate_box(state)
|
224
|
+
state = mix_columns(state) unless i == 7
|
225
|
+
state = xor_block(state, @key.slice(i*4,4))
|
226
|
+
end
|
227
|
+
hex_array_to_integer(state)
|
228
|
+
end
|
229
|
+
alias_method :encode, :enc
|
230
|
+
|
231
|
+
def dec(number)
|
232
|
+
# Takes a 4-byte number and decrypts it with standard AES with a 256 bit key string
|
233
|
+
state = integer_to_hex_array(number)
|
234
|
+
7.downto(1) do |i|
|
235
|
+
state = xor_block(state, @key.slice(i*4,4))
|
236
|
+
state = imix_columns(state) unless i == 7
|
237
|
+
state = irotate_box(state)
|
238
|
+
state = isub_bytes(state)
|
239
|
+
end
|
240
|
+
|
241
|
+
state = xor_block(state, @key.slice(0,4))
|
242
|
+
|
243
|
+
hex_array_to_integer(state)
|
244
|
+
end
|
245
|
+
alias_method :decode, :dec
|
246
|
+
|
247
|
+
def sub_bytes(state)
|
248
|
+
i = 0
|
249
|
+
state.each do |b|
|
250
|
+
state[i] = S_BOX[b]
|
251
|
+
i+=1
|
252
|
+
end
|
253
|
+
state
|
254
|
+
end
|
255
|
+
|
256
|
+
def isub_bytes(state)
|
257
|
+
i = 0
|
258
|
+
state.each do |b|
|
259
|
+
state[i] = IS_BOX[b]
|
260
|
+
i+=1
|
261
|
+
end
|
262
|
+
state
|
263
|
+
end
|
264
|
+
|
265
|
+
def rotate_box(block)
|
266
|
+
state = []
|
267
|
+
state[0] = block[3]
|
268
|
+
state[1] = block[0]
|
269
|
+
state[2] = block[1]
|
270
|
+
state[3] = block[2]
|
271
|
+
state
|
272
|
+
end
|
273
|
+
|
274
|
+
def irotate_box(block)
|
275
|
+
state = []
|
276
|
+
state[0] = block[1]
|
277
|
+
state[1] = block[2]
|
278
|
+
state[2] = block[3]
|
279
|
+
state[3] = block[0]
|
280
|
+
state
|
281
|
+
end
|
282
|
+
|
283
|
+
def xor_block(block, key)
|
284
|
+
state = []
|
285
|
+
block.each_with_index do |b, i|
|
286
|
+
state << (b ^ key[i])
|
287
|
+
end
|
288
|
+
state
|
289
|
+
end
|
290
|
+
|
291
|
+
def mix_columns(state)
|
292
|
+
t = []
|
293
|
+
t[0] = G2X.at(state[0]) ^ G3X.at(state[1]) ^
|
294
|
+
state[2] ^ state[3]
|
295
|
+
t[1] = state[0] ^ G2X.at(state[1]) ^
|
296
|
+
G3X.at(state[2]) ^ state[3]
|
297
|
+
t[2] = state[0] ^ state[1] ^
|
298
|
+
G2X.at(state[2]) ^ G3X.at(state[3])
|
299
|
+
t[3] = G3X.at(state[0]) ^ state[1] ^
|
300
|
+
state[2] ^ G2X.at(state[3])
|
301
|
+
t
|
302
|
+
end
|
303
|
+
|
304
|
+
def imix_columns(state)
|
305
|
+
t = []
|
306
|
+
t[0] = GEX.at(state[0]) ^ GBX.at(state[1]) ^
|
307
|
+
GDX.at(state[2]) ^ G9X.at(state[3])
|
308
|
+
t[1] = G9X.at(state[0]) ^ GEX.at(state[1]) ^
|
309
|
+
GBX.at(state[2]) ^ GDX.at(state[3])
|
310
|
+
t[2] = GDX.at(state[0]) ^ G9X.at(state[1]) ^
|
311
|
+
GEX.at(state[2]) ^ GBX.at(state[3])
|
312
|
+
t[3] = GBX.at(state[0]) ^ GDX.at(state[1]) ^
|
313
|
+
G9X.at(state[2]) ^ GEX.at(state[3])
|
314
|
+
t
|
315
|
+
end
|
316
|
+
|
317
|
+
def hex_array_to_integer(arr)
|
318
|
+
(arr[0] << 24) + (arr[1] << 16 ) + (arr[2] << 8) + (arr[3])
|
319
|
+
end
|
320
|
+
|
321
|
+
def integer_to_hex_array(num)
|
322
|
+
num = num.to_i
|
323
|
+
[
|
324
|
+
(num & 4278190080) >> 24,
|
325
|
+
(num & 16711680) >> 16,
|
326
|
+
(num & 65280) >> 8,
|
327
|
+
(num & 255)
|
328
|
+
]
|
329
|
+
end
|
330
|
+
|
331
|
+
end
|
data/spec/basic_spec.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'spec'
|
2
|
+
require "lib/four_bites_aes"
|
3
|
+
|
4
|
+
describe "A really super secure id encrypter" do
|
5
|
+
|
6
|
+
before(:all) do
|
7
|
+
@crypter = FourBitesAES.new("password")
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should encode and decode an id" do
|
11
|
+
eid = @crypter.encode(12345)
|
12
|
+
@crypter.decode(eid).should eql 12345
|
13
|
+
end
|
14
|
+
|
15
|
+
|
16
|
+
end
|
metadata
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: four_bites_aes
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Mark Percival
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-10-10 00:00:00 -04:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Useful for URL shortners and places where you don't want guessed ID's
|
17
|
+
email: mark@mpercival.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README.markdown
|
24
|
+
files:
|
25
|
+
- README.markdown
|
26
|
+
- Rakefile
|
27
|
+
- VERSION
|
28
|
+
- examples/example.rb
|
29
|
+
- four_bites_aes.gemspec
|
30
|
+
- lib/four_bites_aes.rb
|
31
|
+
- spec/basic_spec.rb
|
32
|
+
has_rdoc: true
|
33
|
+
homepage: http://github.com/mark/fourbitesaes
|
34
|
+
licenses: []
|
35
|
+
|
36
|
+
post_install_message:
|
37
|
+
rdoc_options:
|
38
|
+
- --charset=UTF-8
|
39
|
+
require_paths:
|
40
|
+
- lib
|
41
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: "0"
|
46
|
+
version:
|
47
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
48
|
+
requirements:
|
49
|
+
- - ">="
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: "0"
|
52
|
+
version:
|
53
|
+
requirements: []
|
54
|
+
|
55
|
+
rubyforge_project:
|
56
|
+
rubygems_version: 1.3.5
|
57
|
+
signing_key:
|
58
|
+
specification_version: 3
|
59
|
+
summary: Obsfuscate your ID's
|
60
|
+
test_files:
|
61
|
+
- spec/basic_spec.rb
|
62
|
+
- examples/example.rb
|