id_shuffler 0.0.0 → 0.0.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/ext/id_shuffler/id_shuffler.c +5 -5
- data/lib/id_shuffler.rb +14 -2
- metadata +1 -1
@@ -93,18 +93,18 @@ static VALUE r_base32_unshuffle(VALUE self, VALUE shuffled_str, VALUE key_str) {
|
|
93
93
|
lfsr_num = lfsr_num & 32767;
|
94
94
|
l = (r ^ lfsr_num);
|
95
95
|
r = l_orig;
|
96
|
-
|
96
|
+
}
|
97
97
|
// return the resulting original number as a ruby fixnum
|
98
98
|
unsigned long original;
|
99
|
-
|
99
|
+
original = (l << 15) + r;
|
100
100
|
return ULONG2NUM(original);
|
101
101
|
}
|
102
102
|
|
103
103
|
void Init_id_shuffler(void) {
|
104
104
|
// add two class methods to our IdShuffler class: shuffle and unshuffle, each
|
105
105
|
// of which take the object to be [unshuffled] and the key.
|
106
|
-
|
107
|
-
|
108
|
-
|
106
|
+
VALUE klass = rb_define_class("IdShuffler", rb_cObject);
|
107
|
+
rb_define_singleton_method(klass, "shuffle_with_raw_key", r_base32_shuffle, 2);
|
108
|
+
rb_define_singleton_method(klass, "unshuffle_with_raw_key", r_base32_unshuffle, 2);
|
109
109
|
}
|
110
110
|
|
data/lib/id_shuffler.rb
CHANGED
@@ -18,11 +18,23 @@ class IdShuffler
|
|
18
18
|
end
|
19
19
|
|
20
20
|
def shuffle(i)
|
21
|
-
self.class.
|
21
|
+
self.class.shuffle_with_raw_key(i.to_i, @key)
|
22
22
|
end
|
23
23
|
|
24
24
|
def unshuffle(s)
|
25
|
-
self.class.
|
25
|
+
self.class.unshuffle_with_raw_key(s.to_s, @key)
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.shuffle(i, key)
|
29
|
+
@keycache ||= {}
|
30
|
+
@keycache[key] ||= Digest::MD5.digest(key)
|
31
|
+
shuffle_with_raw_key(i.to_i, @keycache[key])
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.unshuffle(s, key)
|
35
|
+
@keycache ||= {}
|
36
|
+
@keycache[key] ||= Digest::MD5.digest(key)
|
37
|
+
unshuffle_with_raw_key(s.to_s, @keycache[key])
|
26
38
|
end
|
27
39
|
|
28
40
|
end
|