xxtea-ruby 1.2.0
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.
- checksums.yaml +7 -0
- data/.gitignore +8 -0
- data/.travis.yml +9 -0
- data/Gemfile +7 -0
- data/LICENSE.md +20 -0
- data/README.md +37 -0
- data/README_zh_CN.md +36 -0
- data/Rakefile +19 -0
- data/ext/xxtea/extconf.rb +3 -0
- data/ext/xxtea/ruby_xxtea.c +74 -0
- data/ext/xxtea/xxtea.c +260 -0
- data/ext/xxtea/xxtea.h +54 -0
- data/test/xxtea_test.rb +20 -0
- data/xxtea-ruby.gemspec +20 -0
- metadata +62 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 488c7cce3d24c1541ba45624f3d5da139831541c
|
4
|
+
data.tar.gz: d36abcdaaa44bd169df2b2cbba895e47882e6853
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3e646c093bf4e44d84bfcc4fbf50b27bfc7ed4e81200d495e1be676eb55b4ba867313e77a5d2cfb7addc2f6535f3266f0f25eff15f6d4d02e69829488dd23dc4
|
7
|
+
data.tar.gz: 873474e74ca764d7972b409915633eea03584de3da170f138c2d64195c35276c6fe189acdf9ff1ebc16f7451bc8ef4948bb52fe331ea8fcc853a9d42e35cf1f0
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.md
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2008-2016 Ma Bingyao <mabingyao@gmail.com>
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
6
|
+
this software and associated documentation files (the "Software"), to deal in
|
7
|
+
the Software without restriction, including without limitation the rights to
|
8
|
+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
9
|
+
the Software, and to permit persons to whom the Software is furnished to do so,
|
10
|
+
subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
17
|
+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
18
|
+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
19
|
+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
20
|
+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# XXTEA for Ruby
|
2
|
+
|
3
|
+
[](https://travis-ci.org/xxtea/xxtea-ruby)
|
4
|
+
[](https://rubygems.org/gems/xxtea-ruby)
|
5
|
+
[](https://rubygems.org/gems/xxtea-ruby)
|
6
|
+
|
7
|
+
## Introduction
|
8
|
+
|
9
|
+
XXTEA is a fast and secure encryption algorithm. This is a XXTEA library for Ruby.
|
10
|
+
|
11
|
+
It is different from the original XXTEA encryption algorithm. It encrypts and decrypts raw binary data instead of 32bit integer array, and the key is also the raw binary data.
|
12
|
+
|
13
|
+
## Installation
|
14
|
+
|
15
|
+
```sh
|
16
|
+
gem install xxtea-ruby
|
17
|
+
```
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
# encoding: utf-8
|
23
|
+
require "xxtea"
|
24
|
+
text = "Hello World! 你好,中国!"
|
25
|
+
key = "1234567890"
|
26
|
+
encrypt_data = XXTEA.encrypt(text, key)
|
27
|
+
decrypt_data = XXTEA.decrypt_utf8(encrypt_data, key)
|
28
|
+
puts (text == decrypt_data ? "success!" : "fail!");
|
29
|
+
```
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
XXTEA.decrypt_utf8(encrypt_data, key) == XXTEA.decrypt(encrypt_data, key).force_encoding(Encoding::UTF_8)
|
33
|
+
```
|
34
|
+
|
35
|
+
## Note
|
36
|
+
|
37
|
+
There is no decrypt_utf8 function in ruby 1.8.x or lower version.
|
data/README_zh_CN.md
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# XXTEA 加密算法的 Ruby 实现
|
2
|
+
|
3
|
+
[](https://travis-ci.org/xxtea/xxtea-ruby)
|
4
|
+
[](https://rubygems.org/gems/xxtea-ruby)
|
5
|
+
|
6
|
+
## 简介
|
7
|
+
|
8
|
+
XXTEA 是一个快速安全的加密算法。本项目是 XXTEA 加密算法的 Ruby 实现。
|
9
|
+
|
10
|
+
它不同于原始的 XXTEA 加密算法。它是针对原始二进制数据类型进行加密的,而不是针对 32 位 int 数组。同样,密钥也是原始二进制数据类型。
|
11
|
+
|
12
|
+
## 安装
|
13
|
+
|
14
|
+
```sh
|
15
|
+
gem install xxtea-ruby
|
16
|
+
```
|
17
|
+
|
18
|
+
## 使用
|
19
|
+
|
20
|
+
```ruby
|
21
|
+
# encoding: utf-8
|
22
|
+
require "xxtea"
|
23
|
+
text = "Hello World! 你好,中国!"
|
24
|
+
key = "1234567890"
|
25
|
+
encrypt_data = XXTEA.encrypt(text, key)
|
26
|
+
decrypt_data = XXTEA.decrypt_utf8(encrypt_data, key)
|
27
|
+
puts (text == decrypt_data ? "success!" : "fail!");
|
28
|
+
```
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
XXTEA.decrypt_utf8(encrypt_data, key) == XXTEA.decrypt(encrypt_data, key).force_encoding(Encoding::UTF_8)
|
32
|
+
```
|
33
|
+
|
34
|
+
## 注意
|
35
|
+
|
36
|
+
在 1.8.x 或更低版本的 Ruby 上,没有 decrypt_utf8 这个函数。
|
data/Rakefile
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
2
|
+
require 'rake/extensiontask'
|
3
|
+
require 'rake/testtask'
|
4
|
+
require 'rake/clean'
|
5
|
+
|
6
|
+
Rake::ExtensionTask.new('xxtea') do |ext|
|
7
|
+
ext.lib_dir = 'lib'
|
8
|
+
end
|
9
|
+
|
10
|
+
Rake::TestTask.new do |t|
|
11
|
+
t.libs << 'test'
|
12
|
+
t.pattern = 'test/*_test.rb'
|
13
|
+
t.verbose = true
|
14
|
+
end
|
15
|
+
|
16
|
+
desc "Run tests"
|
17
|
+
task :default => :test
|
18
|
+
|
19
|
+
Rake::Task[:test].prerequisites << :compile
|
@@ -0,0 +1,74 @@
|
|
1
|
+
/**********************************************************\
|
2
|
+
| |
|
3
|
+
| xxtea.c |
|
4
|
+
| |
|
5
|
+
| XXTEA encryption algorithm library for Lua. |
|
6
|
+
| |
|
7
|
+
| Encryption Algorithm Authors: |
|
8
|
+
| David J. Wheeler |
|
9
|
+
| Roger M. Needham |
|
10
|
+
| |
|
11
|
+
| Code Authors: Chen fei <cf850118@163.com> |
|
12
|
+
| Ma Bingyao <mabingyao@gmail.com> |
|
13
|
+
| LastModified: Feb 8, 2016 |
|
14
|
+
| |
|
15
|
+
\**********************************************************/
|
16
|
+
|
17
|
+
#include <ruby.h>
|
18
|
+
#ifdef RSTRING_NOEMBED
|
19
|
+
#include <ruby/encoding.h>
|
20
|
+
#endif
|
21
|
+
#include "xxtea.h"
|
22
|
+
|
23
|
+
VALUE rb_encrypt(VALUE mod, VALUE data, VALUE key) {
|
24
|
+
unsigned char * result;
|
25
|
+
VALUE retval;
|
26
|
+
size_t data_len, out_len;
|
27
|
+
|
28
|
+
Check_Type(data, T_STRING);
|
29
|
+
Check_Type(key, T_STRING);
|
30
|
+
|
31
|
+
data_len = RSTRING_LEN(data);
|
32
|
+
|
33
|
+
result = xxtea_encrypt(RSTRING_PTR(data), data_len, RSTRING_PTR(key), &out_len);
|
34
|
+
|
35
|
+
retval = rb_str_new((const char *)result, out_len);
|
36
|
+
|
37
|
+
free(result);
|
38
|
+
|
39
|
+
return retval;
|
40
|
+
}
|
41
|
+
|
42
|
+
VALUE rb_decrypt(VALUE mod, VALUE data, VALUE key) {
|
43
|
+
unsigned char *result;
|
44
|
+
VALUE retval;
|
45
|
+
size_t data_len, out_len;
|
46
|
+
|
47
|
+
Check_Type(data, T_STRING);
|
48
|
+
Check_Type(key, T_STRING);
|
49
|
+
|
50
|
+
data_len = RSTRING_LEN(data);
|
51
|
+
|
52
|
+
result = xxtea_decrypt(RSTRING_PTR(data), data_len, RSTRING_PTR(key), &out_len);
|
53
|
+
|
54
|
+
retval = rb_str_new((const char *)result, out_len);
|
55
|
+
|
56
|
+
free(result);
|
57
|
+
|
58
|
+
return retval;
|
59
|
+
}
|
60
|
+
|
61
|
+
#ifdef RSTRING_NOEMBED
|
62
|
+
VALUE rb_decrypt_utf8(VALUE mod, VALUE data, VALUE key) {
|
63
|
+
return rb_enc_associate(rb_decrypt(mod, data, key), rb_utf8_encoding());
|
64
|
+
}
|
65
|
+
#endif
|
66
|
+
|
67
|
+
void Init_xxtea() {
|
68
|
+
VALUE XXTEA = rb_define_module("XXTEA");
|
69
|
+
rb_define_singleton_method(XXTEA, "encrypt", rb_encrypt, 2);
|
70
|
+
rb_define_singleton_method(XXTEA, "decrypt", rb_decrypt, 2);
|
71
|
+
#ifdef RSTRING_NOEMBED
|
72
|
+
rb_define_singleton_method(XXTEA, "decrypt_utf8", rb_decrypt_utf8, 2);
|
73
|
+
#endif
|
74
|
+
}
|
data/ext/xxtea/xxtea.c
ADDED
@@ -0,0 +1,260 @@
|
|
1
|
+
/**********************************************************\
|
2
|
+
| |
|
3
|
+
| xxtea.c |
|
4
|
+
| |
|
5
|
+
| XXTEA encryption algorithm library for C. |
|
6
|
+
| |
|
7
|
+
| Encryption Algorithm Authors: |
|
8
|
+
| David J. Wheeler |
|
9
|
+
| Roger M. Needham |
|
10
|
+
| |
|
11
|
+
| Code Authors: Chen fei <cf850118@163.com> |
|
12
|
+
| Ma Bingyao <mabingyao@gmail.com> |
|
13
|
+
| LastModified: Feb 7, 2016 |
|
14
|
+
| |
|
15
|
+
\**********************************************************/
|
16
|
+
|
17
|
+
|
18
|
+
#include "xxtea.h"
|
19
|
+
|
20
|
+
#include <string.h>
|
21
|
+
#if defined(_MSC_VER) && _MSC_VER < 1600
|
22
|
+
typedef unsigned __int8 uint8_t;
|
23
|
+
typedef unsigned __int32 uint32_t;
|
24
|
+
#else
|
25
|
+
#if defined(__FreeBSD__) && __FreeBSD__ < 5
|
26
|
+
/* FreeBSD 4 doesn't have stdint.h file */
|
27
|
+
#include <inttypes.h>
|
28
|
+
#else
|
29
|
+
#include <stdint.h>
|
30
|
+
#endif
|
31
|
+
#endif
|
32
|
+
|
33
|
+
#include <sys/types.h> /* This will likely define BYTE_ORDER */
|
34
|
+
|
35
|
+
#ifndef BYTE_ORDER
|
36
|
+
#if (BSD >= 199103)
|
37
|
+
# include <machine/endian.h>
|
38
|
+
#else
|
39
|
+
#if defined(linux) || defined(__linux__)
|
40
|
+
# include <endian.h>
|
41
|
+
#else
|
42
|
+
#define LITTLE_ENDIAN 1234 /* least-significant byte first (vax, pc) */
|
43
|
+
#define BIG_ENDIAN 4321 /* most-significant byte first (IBM, net) */
|
44
|
+
#define PDP_ENDIAN 3412 /* LSB first in word, MSW first in long (pdp)*/
|
45
|
+
|
46
|
+
#if defined(__i386__) || defined(__x86_64__) || defined(__amd64__) || \
|
47
|
+
defined(vax) || defined(ns32000) || defined(sun386) || \
|
48
|
+
defined(MIPSEL) || defined(_MIPSEL) || defined(BIT_ZERO_ON_RIGHT) || \
|
49
|
+
defined(__alpha__) || defined(__alpha)
|
50
|
+
#define BYTE_ORDER LITTLE_ENDIAN
|
51
|
+
#endif
|
52
|
+
|
53
|
+
#if defined(sel) || defined(pyr) || defined(mc68000) || defined(sparc) || \
|
54
|
+
defined(is68k) || defined(tahoe) || defined(ibm032) || defined(ibm370) || \
|
55
|
+
defined(MIPSEB) || defined(_MIPSEB) || defined(_IBMR2) || defined(DGUX) ||\
|
56
|
+
defined(apollo) || defined(__convex__) || defined(_CRAY) || \
|
57
|
+
defined(__hppa) || defined(__hp9000) || \
|
58
|
+
defined(__hp9000s300) || defined(__hp9000s700) || \
|
59
|
+
defined (BIT_ZERO_ON_LEFT) || defined(m68k) || defined(__sparc)
|
60
|
+
#define BYTE_ORDER BIG_ENDIAN
|
61
|
+
#endif
|
62
|
+
#endif /* linux */
|
63
|
+
#endif /* BSD */
|
64
|
+
#endif /* BYTE_ORDER */
|
65
|
+
|
66
|
+
#ifndef BYTE_ORDER
|
67
|
+
#ifdef __BYTE_ORDER
|
68
|
+
#if defined(__LITTLE_ENDIAN) && defined(__BIG_ENDIAN)
|
69
|
+
#ifndef LITTLE_ENDIAN
|
70
|
+
#define LITTLE_ENDIAN __LITTLE_ENDIAN
|
71
|
+
#endif
|
72
|
+
#ifndef BIG_ENDIAN
|
73
|
+
#define BIG_ENDIAN __BIG_ENDIAN
|
74
|
+
#endif
|
75
|
+
#if (__BYTE_ORDER == __LITTLE_ENDIAN)
|
76
|
+
#define BYTE_ORDER LITTLE_ENDIAN
|
77
|
+
#else
|
78
|
+
#define BYTE_ORDER BIG_ENDIAN
|
79
|
+
#endif
|
80
|
+
#endif
|
81
|
+
#endif
|
82
|
+
#endif
|
83
|
+
|
84
|
+
#define MX (((z >> 5) ^ (y << 2)) + ((y >> 3) ^ (z << 4))) ^ ((sum ^ y) + (key[(p & 3) ^ e] ^ z))
|
85
|
+
#define DELTA 0x9e3779b9
|
86
|
+
|
87
|
+
#define FIXED_KEY \
|
88
|
+
size_t i;\
|
89
|
+
uint8_t fixed_key[16];\
|
90
|
+
memcpy(fixed_key, key, 16);\
|
91
|
+
for (i = 0; (i < 16) && (fixed_key[i] != 0); ++i);\
|
92
|
+
for (++i; i < 16; ++i) fixed_key[i] = 0;\
|
93
|
+
|
94
|
+
|
95
|
+
static uint32_t * xxtea_to_uint_array(const uint8_t * data, size_t len, int inc_len, size_t * out_len) {
|
96
|
+
uint32_t *out;
|
97
|
+
#if !(defined(BYTE_ORDER) && (BYTE_ORDER == LITTLE_ENDIAN))
|
98
|
+
size_t i;
|
99
|
+
#endif
|
100
|
+
size_t n;
|
101
|
+
|
102
|
+
n = (((len & 3) == 0) ? (len >> 2) : ((len >> 2) + 1));
|
103
|
+
|
104
|
+
if (inc_len) {
|
105
|
+
out = (uint32_t *)calloc(n + 1, sizeof(uint32_t));
|
106
|
+
if (!out) return NULL;
|
107
|
+
out[n] = (uint32_t)len;
|
108
|
+
*out_len = n + 1;
|
109
|
+
}
|
110
|
+
else {
|
111
|
+
out = (uint32_t *)calloc(n, sizeof(uint32_t));
|
112
|
+
if (!out) return NULL;
|
113
|
+
*out_len = n;
|
114
|
+
}
|
115
|
+
#if defined(BYTE_ORDER) && (BYTE_ORDER == LITTLE_ENDIAN)
|
116
|
+
memcpy(out, data, len);
|
117
|
+
#else
|
118
|
+
for (i = 0; i < len; ++i) {
|
119
|
+
out[i >> 2] |= (uint32_t)data[i] << ((i & 3) << 3);
|
120
|
+
}
|
121
|
+
#endif
|
122
|
+
|
123
|
+
return out;
|
124
|
+
}
|
125
|
+
|
126
|
+
static uint8_t * xxtea_to_ubyte_array(const uint32_t * data, size_t len, int inc_len, size_t * out_len) {
|
127
|
+
uint8_t *out;
|
128
|
+
#if !(defined(BYTE_ORDER) && (BYTE_ORDER == LITTLE_ENDIAN))
|
129
|
+
size_t i;
|
130
|
+
#endif
|
131
|
+
size_t m, n;
|
132
|
+
|
133
|
+
n = len << 2;
|
134
|
+
|
135
|
+
if (inc_len) {
|
136
|
+
m = data[len - 1];
|
137
|
+
n -= 4;
|
138
|
+
if ((m < n - 3) || (m > n)) return NULL;
|
139
|
+
n = m;
|
140
|
+
}
|
141
|
+
|
142
|
+
out = (uint8_t *)malloc(n + 1);
|
143
|
+
|
144
|
+
#if defined(BYTE_ORDER) && (BYTE_ORDER == LITTLE_ENDIAN)
|
145
|
+
memcpy(out, data, n);
|
146
|
+
#else
|
147
|
+
for (i = 0; i < n; ++i) {
|
148
|
+
out[i] = (uint8_t)(data[i >> 2] >> ((i & 3) << 3));
|
149
|
+
}
|
150
|
+
#endif
|
151
|
+
|
152
|
+
out[n] = '\0';
|
153
|
+
*out_len = n;
|
154
|
+
|
155
|
+
return out;
|
156
|
+
}
|
157
|
+
|
158
|
+
static uint32_t * xxtea_uint_encrypt(uint32_t * data, size_t len, uint32_t * key) {
|
159
|
+
uint32_t n = (uint32_t)len - 1;
|
160
|
+
uint32_t z = data[n], y, p, q = 6 + 52 / (n + 1), sum = 0, e;
|
161
|
+
|
162
|
+
if (n < 1) return data;
|
163
|
+
|
164
|
+
while (0 < q--) {
|
165
|
+
sum += DELTA;
|
166
|
+
e = sum >> 2 & 3;
|
167
|
+
|
168
|
+
for (p = 0; p < n; p++) {
|
169
|
+
y = data[p + 1];
|
170
|
+
z = data[p] += MX;
|
171
|
+
}
|
172
|
+
|
173
|
+
y = data[0];
|
174
|
+
z = data[n] += MX;
|
175
|
+
}
|
176
|
+
|
177
|
+
return data;
|
178
|
+
}
|
179
|
+
|
180
|
+
static uint32_t * xxtea_uint_decrypt(uint32_t * data, size_t len, uint32_t * key) {
|
181
|
+
uint32_t n = (uint32_t)len - 1;
|
182
|
+
uint32_t z, y = data[0], p, q = 6 + 52 / (n + 1), sum = q * DELTA, e;
|
183
|
+
|
184
|
+
if (n < 1) return data;
|
185
|
+
|
186
|
+
while (sum != 0) {
|
187
|
+
e = sum >> 2 & 3;
|
188
|
+
|
189
|
+
for (p = n; p > 0; p--) {
|
190
|
+
z = data[p - 1];
|
191
|
+
y = data[p] -= MX;
|
192
|
+
}
|
193
|
+
|
194
|
+
z = data[n];
|
195
|
+
y = data[0] -= MX;
|
196
|
+
sum -= DELTA;
|
197
|
+
}
|
198
|
+
|
199
|
+
return data;
|
200
|
+
}
|
201
|
+
|
202
|
+
static uint8_t * xxtea_ubyte_encrypt(const uint8_t * data, size_t len, const uint8_t * key, size_t * out_len) {
|
203
|
+
uint8_t *out;
|
204
|
+
uint32_t *data_array, *key_array;
|
205
|
+
size_t data_len, key_len;
|
206
|
+
|
207
|
+
if (!len) return NULL;
|
208
|
+
|
209
|
+
data_array = xxtea_to_uint_array(data, len, 1, &data_len);
|
210
|
+
if (!data_array) return NULL;
|
211
|
+
|
212
|
+
key_array = xxtea_to_uint_array(key, 16, 0, &key_len);
|
213
|
+
if (!key_array) {
|
214
|
+
free(data_array);
|
215
|
+
return NULL;
|
216
|
+
}
|
217
|
+
|
218
|
+
out = xxtea_to_ubyte_array(xxtea_uint_encrypt(data_array, data_len, key_array), data_len, 0, out_len);
|
219
|
+
|
220
|
+
free(data_array);
|
221
|
+
free(key_array);
|
222
|
+
|
223
|
+
return out;
|
224
|
+
}
|
225
|
+
|
226
|
+
static uint8_t * xxtea_ubyte_decrypt(const uint8_t * data, size_t len, const uint8_t * key, size_t * out_len) {
|
227
|
+
uint8_t *out;
|
228
|
+
uint32_t *data_array, *key_array;
|
229
|
+
size_t data_len, key_len;
|
230
|
+
|
231
|
+
if (!len) return NULL;
|
232
|
+
|
233
|
+
data_array = xxtea_to_uint_array(data, len, 0, &data_len);
|
234
|
+
if (!data_array) return NULL;
|
235
|
+
|
236
|
+
key_array = xxtea_to_uint_array(key, 16, 0, &key_len);
|
237
|
+
if (!key_array) {
|
238
|
+
free(data_array);
|
239
|
+
return NULL;
|
240
|
+
}
|
241
|
+
|
242
|
+
out = xxtea_to_ubyte_array(xxtea_uint_decrypt(data_array, data_len, key_array), data_len, 1, out_len);
|
243
|
+
|
244
|
+
free(data_array);
|
245
|
+
free(key_array);
|
246
|
+
|
247
|
+
return out;
|
248
|
+
}
|
249
|
+
|
250
|
+
// public functions
|
251
|
+
|
252
|
+
void * xxtea_encrypt(const void * data, size_t len, const void * key, size_t * out_len) {
|
253
|
+
FIXED_KEY
|
254
|
+
return xxtea_ubyte_encrypt(data, len, fixed_key, out_len);
|
255
|
+
}
|
256
|
+
|
257
|
+
void * xxtea_decrypt(const void * data, size_t len, const void * key, size_t * out_len) {
|
258
|
+
FIXED_KEY
|
259
|
+
return xxtea_ubyte_decrypt(data, len, fixed_key, out_len);
|
260
|
+
}
|
data/ext/xxtea/xxtea.h
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
/**********************************************************\
|
2
|
+
| |
|
3
|
+
| xxtea.h |
|
4
|
+
| |
|
5
|
+
| XXTEA encryption algorithm library for C. |
|
6
|
+
| |
|
7
|
+
| Encryption Algorithm Authors: |
|
8
|
+
| David J. Wheeler |
|
9
|
+
| Roger M. Needham |
|
10
|
+
| |
|
11
|
+
| Code Authors: Chen fei <cf850118@163.com> |
|
12
|
+
| Ma Bingyao <mabingyao@gmail.com> |
|
13
|
+
| LastModified: Mar 3, 2015 |
|
14
|
+
| |
|
15
|
+
\**********************************************************/
|
16
|
+
|
17
|
+
#ifndef XXTEA_INCLUDED
|
18
|
+
#define XXTEA_INCLUDED
|
19
|
+
|
20
|
+
#include <stdlib.h>
|
21
|
+
|
22
|
+
#ifdef __cplusplus
|
23
|
+
extern "C" {
|
24
|
+
#endif
|
25
|
+
|
26
|
+
/**
|
27
|
+
* Function: xxtea_encrypt
|
28
|
+
* @data: Data to be encrypted
|
29
|
+
* @len: Length of the data to be encrypted
|
30
|
+
* @key: Symmetric key
|
31
|
+
* @out_len: Pointer to output length variable
|
32
|
+
* Returns: Encrypted data or %NULL on failure
|
33
|
+
*
|
34
|
+
* Caller is responsible for freeing the returned buffer.
|
35
|
+
*/
|
36
|
+
void * xxtea_encrypt(const void * data, size_t len, const void * key, size_t * out_len);
|
37
|
+
|
38
|
+
/**
|
39
|
+
* Function: xxtea_decrypt
|
40
|
+
* @data: Data to be decrypted
|
41
|
+
* @len: Length of the data to be decrypted
|
42
|
+
* @key: Symmetric key
|
43
|
+
* @out_len: Pointer to output length variable
|
44
|
+
* Returns: Decrypted data or %NULL on failure
|
45
|
+
*
|
46
|
+
* Caller is responsible for freeing the returned buffer.
|
47
|
+
*/
|
48
|
+
void * xxtea_decrypt(const void * data, size_t len, const void * key, size_t * out_len);
|
49
|
+
|
50
|
+
#ifdef __cplusplus
|
51
|
+
}
|
52
|
+
#endif
|
53
|
+
|
54
|
+
#endif
|
data/test/xxtea_test.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require "minitest/autorun"
|
3
|
+
require "xxtea"
|
4
|
+
|
5
|
+
describe "XXTEA" do
|
6
|
+
it "test xxtea encrypt and decrypt" do
|
7
|
+
text = "Hello World! 你好,中国!"
|
8
|
+
key = "1234567890"
|
9
|
+
encrypt_data = XXTEA.encrypt(text, key)
|
10
|
+
if RUBY_VERSION >= "1.9.0" then
|
11
|
+
decrypt_data = XXTEA.decrypt_utf8(encrypt_data, key)
|
12
|
+
decrypt_data2 = XXTEA.decrypt(encrypt_data, key).force_encoding(Encoding::UTF_8)
|
13
|
+
assert_equal text, decrypt_data
|
14
|
+
assert_equal decrypt_data, decrypt_data2
|
15
|
+
else
|
16
|
+
decrypt_data = XXTEA.decrypt(encrypt_data, key)
|
17
|
+
assert_equal text, decrypt_data
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
data/xxtea-ruby.gemspec
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = 'xxtea-ruby'
|
3
|
+
s.version = '1.2.0'
|
4
|
+
s.license = 'MIT'
|
5
|
+
s.author = 'Ma Bingyao ( andot )'
|
6
|
+
s.email = 'mabingyao@gmail.com'
|
7
|
+
s.homepage = 'https://github.com/xxtea/xxtea-ruby'
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.description = <<-EOF
|
10
|
+
XXTEA is a fast and secure encryption algorithm. This is a XXTEA library for Ruby.
|
11
|
+
|
12
|
+
It is different from the original XXTEA encryption algorithm. It encrypts and decrypts raw binary data instead of 32bit integer array, and the key is also the raw binary data.
|
13
|
+
EOF
|
14
|
+
|
15
|
+
s.summary = 'XXTEA is a fast and secure encryption algorithm. This is a XXTEA library for Ruby.'
|
16
|
+
s.files = `git ls-files`.split($/)
|
17
|
+
s.test_files = s.files.grep(%r{^test/})
|
18
|
+
s.require_path = 'lib'
|
19
|
+
s.extensions = ["ext/xxtea/extconf.rb"]
|
20
|
+
end
|
metadata
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: xxtea-ruby
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.2.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ma Bingyao ( andot )
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-02-07 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: |2
|
14
|
+
XXTEA is a fast and secure encryption algorithm. This is a XXTEA library for Ruby.
|
15
|
+
|
16
|
+
It is different from the original XXTEA encryption algorithm. It encrypts and decrypts raw binary data instead of 32bit integer array, and the key is also the raw binary data.
|
17
|
+
email: mabingyao@gmail.com
|
18
|
+
executables: []
|
19
|
+
extensions:
|
20
|
+
- ext/xxtea/extconf.rb
|
21
|
+
extra_rdoc_files: []
|
22
|
+
files:
|
23
|
+
- ".gitignore"
|
24
|
+
- ".travis.yml"
|
25
|
+
- Gemfile
|
26
|
+
- LICENSE.md
|
27
|
+
- README.md
|
28
|
+
- README_zh_CN.md
|
29
|
+
- Rakefile
|
30
|
+
- ext/xxtea/extconf.rb
|
31
|
+
- ext/xxtea/ruby_xxtea.c
|
32
|
+
- ext/xxtea/xxtea.c
|
33
|
+
- ext/xxtea/xxtea.h
|
34
|
+
- test/xxtea_test.rb
|
35
|
+
- xxtea-ruby.gemspec
|
36
|
+
homepage: https://github.com/xxtea/xxtea-ruby
|
37
|
+
licenses:
|
38
|
+
- MIT
|
39
|
+
metadata: {}
|
40
|
+
post_install_message:
|
41
|
+
rdoc_options: []
|
42
|
+
require_paths:
|
43
|
+
- lib
|
44
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
requirements: []
|
55
|
+
rubyforge_project:
|
56
|
+
rubygems_version: 2.5.0
|
57
|
+
signing_key:
|
58
|
+
specification_version: 4
|
59
|
+
summary: XXTEA is a fast and secure encryption algorithm. This is a XXTEA library
|
60
|
+
for Ruby.
|
61
|
+
test_files:
|
62
|
+
- test/xxtea_test.rb
|