rubytea 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. data/ext/.gitignore +3 -0
  2. data/ext/extconf.rb +3 -0
  3. data/ext/tea.c +130 -0
  4. data/rubytea.gemspec +12 -0
  5. metadata +69 -0
data/ext/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ Makefile
2
+ *.o
3
+ *.so
data/ext/extconf.rb ADDED
@@ -0,0 +1,3 @@
1
+ require 'mkmf'
2
+
3
+ create_makefile('rubytea')
data/ext/tea.c ADDED
@@ -0,0 +1,130 @@
1
+ #include <stdint.h>
2
+ #include <stdio.h>
3
+ #include <ruby.h>
4
+ #define DELTA 0x9e3779b9
5
+ #define MX (((z>>5^y<<2) + (y>>3^z<<4)) ^ ((sum^y) + (k[(p&3)^e] ^ z)))
6
+
7
+ void btea(uint32_t *v, int n, uint32_t const k[4]) {
8
+ uint32_t y, z, sum;
9
+ int p, rounds, e;
10
+ if (n > 1) { /* Coding Part */
11
+ rounds = 6 + 52/n;
12
+ sum = 0;
13
+ z = v[n-1];
14
+ do {
15
+ sum += DELTA;
16
+ e = (sum >> 2) & 3;
17
+ for (p=0; p<n-1; p++) {
18
+ y = v[p+1];
19
+ z = v[p] += MX;
20
+ }
21
+ y = v[0];
22
+ z = v[n-1] += MX;
23
+ } while (--rounds);
24
+ } else if (n < -1) { /* Decoding Part */
25
+ n = -n;
26
+ rounds = 6 + 52/n;
27
+ sum = rounds*DELTA;
28
+ y = v[0];
29
+ do {
30
+ e = (sum >> 2) & 3;
31
+ for (p=n-1; p>0; p--) {
32
+ z = v[p-1];
33
+ y = v[p] -= MX;
34
+ }
35
+ z = v[n-1];
36
+ y = v[0] -= MX;
37
+ } while ((sum -= DELTA) != 0);
38
+ }
39
+ }
40
+
41
+ typedef struct {
42
+ uint32_t key[4];
43
+ } TEAKey;
44
+
45
+ void tkey_free(void *teakey) {
46
+ free(teakey);
47
+ }
48
+
49
+ VALUE tkey_alloc(VALUE klass) {
50
+ TEAKey *key;
51
+ VALUE obj;
52
+ key = malloc(sizeof(TEAKey));
53
+ obj = Data_Wrap_Struct(klass, 0, tkey_free, key);
54
+ }
55
+
56
+ VALUE tkey_init(VALUE self, VALUE k0, VALUE k1, VALUE k2, VALUE k3) {
57
+ TEAKey *key;
58
+ Data_Get_Struct(self, TEAKey, key);
59
+ key->key[0] = NUM2UINT(k0);
60
+ key->key[1] = NUM2UINT(k1);
61
+ key->key[2] = NUM2UINT(k2);
62
+ key->key[3] = NUM2UINT(k3);
63
+ return self;
64
+ }
65
+
66
+ VALUE tkey_init_copy(VALUE copy, VALUE orig) {
67
+ TEAKey *key_copy, *key_orig;
68
+ Data_Get_Struct(copy, TEAKey, key_copy);
69
+ Data_Get_Struct(orig, TEAKey, key_orig);
70
+
71
+ if (TYPE(orig) != T_DATA || RDATA(orig)->dfree != (RUBY_DATA_FUNC)tkey_free)
72
+ rb_raise(rb_eTypeError, "wrong argument type copying TEAKey");
73
+
74
+ MEMCPY(key_copy, key_orig, TEAKey, 1);
75
+
76
+ return copy;
77
+ }
78
+
79
+ VALUE rubytea_exec(VALUE self, VALUE data, VALUE rkey, char op)
80
+ {
81
+ int len = RARRAY_LEN(data);
82
+ int i;
83
+ if (len < 2) rb_raise(rb_eArgError, "TEA::encrypt() can only encrypt an array with two or more ints");
84
+ size_t c = sizeof(uint32_t) * len;
85
+ uint32_t *ptr = ALLOCA_N(uint32_t, len);
86
+
87
+ for (i = 0; i < len; ++i)
88
+ {
89
+ ptr[i] = NUM2UINT(rb_ary_entry(data, i));
90
+ }
91
+
92
+ TEAKey *key;
93
+
94
+ Data_Get_Struct(rkey, TEAKey, key);
95
+
96
+ btea(ptr, op * len, key->key);
97
+
98
+ for (i = 0; i < len; ++i)
99
+ {
100
+ rb_ary_store(data, i, UINT2NUM(ptr[i]));
101
+ }
102
+ return data;
103
+
104
+ }
105
+
106
+ VALUE rubytea_encrypt(VALUE self, VALUE data, VALUE key)
107
+ {
108
+ return rubytea_exec(self, data, key, 1);
109
+ }
110
+
111
+ VALUE rubytea_decrypt(VALUE self, VALUE data, VALUE key)
112
+ {
113
+ return rubytea_exec(self, data, key, -1);
114
+ }
115
+
116
+ void Init_rubytea()
117
+ {
118
+ VALUE RubyTEA;
119
+ RubyTEA = rb_define_module("TEA");
120
+
121
+ rb_define_module_function(RubyTEA, "encrypt!", rubytea_encrypt, 2);
122
+ rb_define_module_function(RubyTEA, "decrypt!", rubytea_decrypt, 2);
123
+
124
+ VALUE TK;
125
+ TK = rb_define_class_under(RubyTEA, "Key", rb_cObject);
126
+ rb_define_alloc_func(TK, tkey_alloc);
127
+ rb_define_method(TK, "initialize", tkey_init, 4);
128
+ rb_define_method(TK, "initialize_copy", tkey_init_copy, 1);
129
+ }
130
+
data/rubytea.gemspec ADDED
@@ -0,0 +1,12 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'rubytea'
3
+ s.version = '0.1.0'
4
+ s.files = `git ls-files`.split("\n")
5
+ s.extensions = ["ext/extconf.rb"]
6
+
7
+ s.require_paths = %w(ext)
8
+ s.summary = "A wrapper around the XXTEA block encryption algorithm"
9
+
10
+ s.add_development_dependency 'rake-compiler', '~> 0.7.6'
11
+
12
+ end
metadata ADDED
@@ -0,0 +1,69 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rubytea
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.0
6
+ platform: ruby
7
+ authors: []
8
+
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-04-06 00:00:00 +01:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: rake-compiler
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ~>
23
+ - !ruby/object:Gem::Version
24
+ version: 0.7.6
25
+ type: :development
26
+ version_requirements: *id001
27
+ description:
28
+ email:
29
+ executables: []
30
+
31
+ extensions:
32
+ - ext/extconf.rb
33
+ extra_rdoc_files: []
34
+
35
+ files:
36
+ - ext/.gitignore
37
+ - ext/extconf.rb
38
+ - ext/tea.c
39
+ - rubytea.gemspec
40
+ has_rdoc: true
41
+ homepage:
42
+ licenses: []
43
+
44
+ post_install_message:
45
+ rdoc_options: []
46
+
47
+ require_paths:
48
+ - ext
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: "0"
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: "0"
61
+ requirements: []
62
+
63
+ rubyforge_project:
64
+ rubygems_version: 1.6.2
65
+ signing_key:
66
+ specification_version: 3
67
+ summary: A wrapper around the XXTEA block encryption algorithm
68
+ test_files: []
69
+