id_shuffler 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -31,6 +31,9 @@
31
31
  *
32
32
  */
33
33
 
34
+ const int i = 1;
35
+ #define is_bigendian() ( (*(char*)&i) == 0 )
36
+
34
37
  const char ftable[256] = {
35
38
  0xa3,0xd7,0x09,0x83,0xf8,0x48,0xf6,0xf4,0xb3,0x21,0x15,0x78,0x99,0xb1,0xaf,0xf9,
36
39
  0xe7,0x2d,0x4d,0x8a,0xce,0x4c,0xca,0x2e,0x52,0x95,0xd9,0x1e,0x4e,0x38,0x44,0x28,
@@ -65,6 +68,16 @@ uint32_t g(char key[10], int k, uint32_t w)
65
68
  return (((g5<<8) + g6) & 32767); /* clip result to 15 bits */
66
69
  }
67
70
 
71
+ static VALUE r_g1(VALUE self) {
72
+ char key[10] = {1,2,3,4,5,6,7,8,9,10};
73
+ return ULONG2NUM(g(key,1,1));
74
+ }
75
+
76
+ static VALUE r_g2(VALUE self) {
77
+ char key[10] = {1,2,3,4,5,6,7,8,9,10};
78
+ return ULONG2NUM(g(key,1,32767));
79
+ }
80
+
68
81
  uint32_t skip30(char key[10], uint32_t num, int encrypt)
69
82
  {
70
83
  int k; /* round number */
@@ -93,6 +106,44 @@ uint32_t skip30(char key[10], uint32_t num, int encrypt)
93
106
  return (wr << 15) + (wl);
94
107
  }
95
108
 
109
+ static VALUE r_skip1(VALUE self) {
110
+ char key[10] = {1,2,3,4,5,6,7,8,9,10};
111
+ return ULONG2NUM(skip30(key,1,1));
112
+ }
113
+
114
+ static VALUE r_skip2(VALUE self) {
115
+ char key[10] = {1,2,3,4,5,6,7,8,9,10};
116
+ return ULONG2NUM(skip30(key,32767,1));
117
+ }
118
+
119
+ static VALUE r_skip3(VALUE self, VALUE original_num) {
120
+ char key[10] = {1,2,3,4,5,6,7,8,9,10};
121
+ unsigned long original_ulong;
122
+ original_ulong = NUM2ULONG(original_num);
123
+ uint32_t original = original_ulong;
124
+ return ULONG2NUM(skip30(key,original,1));
125
+ }
126
+
127
+ static VALUE r_skip4(VALUE self, VALUE original_num, VALUE key_str) {
128
+ char *key = StringValuePtr(key_str);
129
+ unsigned long original_ulong;
130
+ original_ulong = NUM2ULONG(original_num);
131
+ uint32_t original = original_ulong;
132
+ return ULONG2NUM(skip30(key,original,1));
133
+ }
134
+
135
+ //uint32_t little_endian_swap(uint32_t num)
136
+ //{
137
+ // if (is_bigendian()) {
138
+ // return num;
139
+ // } else {
140
+ // return ((num>>24)&0xff) | // move byte 3 to byte 0
141
+ // ((num<<8)&0xff0000) | // move byte 1 to byte 2
142
+ // ((num>>8)&0xff00) | // move byte 2 to byte 1
143
+ // ((num<<24)&0xff000000); // byte 0 to byte 3
144
+ // }
145
+ //}
146
+
96
147
  static VALUE r_base32_shuffle(VALUE self, VALUE original_num, VALUE key_str) {
97
148
  /* takes a number and a key string as input and returns a base-32 encoded
98
149
  * shuffled id. */
@@ -114,6 +165,7 @@ static VALUE r_base32_shuffle(VALUE self, VALUE original_num, VALUE key_str) {
114
165
 
115
166
  /* encrypt */
116
167
  uint32_t original = original_ulong;
168
+ //uint32_t shuffled = skip30(key, little_endian_swap(original), 1);
117
169
  uint32_t shuffled = skip30(key, original, 1);
118
170
 
119
171
  /* base-32 encode the result using "crockford 32" alphabet */
@@ -128,6 +180,20 @@ static VALUE r_base32_shuffle(VALUE self, VALUE original_num, VALUE key_str) {
128
180
  return rb_str_new(buf,6);
129
181
  }
130
182
 
183
+ static VALUE r_b32(VALUE self, VALUE original_num) {
184
+ char buf[6];
185
+ char *digits = "0123456789abcdefghjkmnpqrstvwxyz";
186
+ int cpos;
187
+ unsigned long original_ulong;
188
+ original_ulong = NUM2ULONG(original_num);
189
+ uint32_t original = original_ulong;
190
+ for (cpos = 0; cpos < 6; cpos ++) {
191
+ buf[cpos] = digits[((original >> (5*(5-cpos))) & 31)];
192
+ }
193
+ return rb_str_new(buf,6);
194
+ }
195
+
196
+
131
197
  static VALUE r_base32_unshuffle(VALUE self, VALUE shuffled_str, VALUE key_str) {
132
198
  /* take the a base-32 encoded ruby string and a key string, decodes and
133
199
  * unshuffles it, producing the original number. */
@@ -164,6 +230,7 @@ static VALUE r_base32_unshuffle(VALUE self, VALUE shuffled_str, VALUE key_str) {
164
230
  }
165
231
 
166
232
  /* decrypt */
233
+ //uint32_t original = little_endian_swap( skip30(key, shuffled, 0) );
167
234
  uint32_t original = skip30(key, shuffled, 0);
168
235
 
169
236
  /* return a ruby number */
@@ -178,5 +245,12 @@ void Init_id_shuffler(void) {
178
245
  VALUE klass = rb_define_class("IdShuffler", rb_cObject);
179
246
  rb_define_singleton_method(klass, "shuffle_with_raw_key", r_base32_shuffle, 2);
180
247
  rb_define_singleton_method(klass, "unshuffle_with_raw_key", r_base32_unshuffle, 2);
248
+ rb_define_singleton_method(klass, "g1", r_g1,0);
249
+ rb_define_singleton_method(klass, "g2", r_g2,0);
250
+ rb_define_singleton_method(klass, "skip1", r_skip1,0);
251
+ rb_define_singleton_method(klass, "skip2", r_skip2,0);
252
+ rb_define_singleton_method(klass, "skip3", r_skip3,1);
253
+ rb_define_singleton_method(klass, "skip4", r_skip4,2);
254
+ rb_define_singleton_method(klass, "b32", r_b32,1);
181
255
  }
182
256
 
@@ -41,4 +41,8 @@ class IdShuffler
41
41
  unshuffle_with_raw_key(s.to_s, Digest::MD5.digest(key))
42
42
  end
43
43
 
44
+ def self.version
45
+ "0.0.4"
46
+ end
47
+
44
48
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: id_shuffler
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: