xxtea-ruby 1.2.0 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 488c7cce3d24c1541ba45624f3d5da139831541c
4
- data.tar.gz: d36abcdaaa44bd169df2b2cbba895e47882e6853
3
+ metadata.gz: efc56a30ea088303af96c80f4f36b5a809aa1bb0
4
+ data.tar.gz: d46a9ccdadb94859bbe668d477abcd0f9c92fccf
5
5
  SHA512:
6
- metadata.gz: 3e646c093bf4e44d84bfcc4fbf50b27bfc7ed4e81200d495e1be676eb55b4ba867313e77a5d2cfb7addc2f6535f3266f0f25eff15f6d4d02e69829488dd23dc4
7
- data.tar.gz: 873474e74ca764d7972b409915633eea03584de3da170f138c2d64195c35276c6fe189acdf9ff1ebc16f7451bc8ef4948bb52fe331ea8fcc853a9d42e35cf1f0
6
+ metadata.gz: 09c1d9fab2db59aa73713a8aabad63b9de3e992fa267a5be3659833a2fa4d0df7cc02f046ed54b0f4fe03e1c123f360a9f0e493655c5a97775ecfcb65f882d70
7
+ data.tar.gz: b7d99f7446473f63820ac0d667969e760070a445622c25b1c6f0bb4a8e556b1a63cb575399c67bf4bdebe4c5c5e0a2a567351226fff0847219970321c76cb4ea
data/.gitignore CHANGED
@@ -6,3 +6,5 @@ tmp
6
6
  *.bundle
7
7
 
8
8
  *.gem
9
+
10
+ *.dylib
@@ -6,4 +6,9 @@ rvm:
6
6
  - 2.0.0
7
7
  - 2.1
8
8
  - 2.2
9
+ - jruby-18mode
10
+ - jruby-19mode
11
+ - jruby-head
12
+ - rbx-2
13
+ - ree
9
14
  script: "bundle exec rake test"
data/Gemfile CHANGED
@@ -2,6 +2,7 @@ source "http://rubygems.org"
2
2
 
3
3
  gemspec
4
4
 
5
+ gem 'ffi'
5
6
  gem 'rake'
6
7
  gem 'rake-compiler'
7
8
  gem 'minitest', '~> 4.1'
data/README.md CHANGED
@@ -1,8 +1,15 @@
1
1
  # XXTEA for Ruby
2
2
 
3
+ <a href="https://github.com/xxtea/">
4
+ <img src="https://avatars1.githubusercontent.com/u/6683159?v=3&s=86" alt="XXTEA logo" title="XXTEA" align="right" />
5
+ </a>
6
+
3
7
  [![Build Status](https://travis-ci.org/xxtea/xxtea-ruby.svg?branch=master)](https://travis-ci.org/xxtea/xxtea-ruby)
4
8
  [![Gem](https://img.shields.io/gem/v/xxtea-ruby.svg)](https://rubygems.org/gems/xxtea-ruby)
5
9
  [![Gem](https://img.shields.io/gem/dt/xxtea-ruby.svg)](https://rubygems.org/gems/xxtea-ruby)
10
+ [![Gem](https://img.shields.io/gem/dtv/xxtea-ruby.svg)](https://rubygems.org/gems/xxtea-ruby)
11
+ [![Gems](https://img.shields.io/gem/rt/xxtea-ruby.svg)](https://rubygems.org/gems/xxtea-ruby)
12
+ [![Gems](https://img.shields.io/gem/rd/xxtea-ruby.svg)](https://rubygems.org/gems/xxtea-ruby)
6
13
 
7
14
  ## Introduction
8
15
 
@@ -34,4 +41,4 @@ XXTEA.decrypt_utf8(encrypt_data, key) == XXTEA.decrypt(encrypt_data, key).force_
34
41
 
35
42
  ## Note
36
43
 
37
- There is no decrypt_utf8 function in ruby 1.8.x or lower version.
44
+ decrypt_utf8 is an alias of decrypt in ruby 1.8.x or lower version.
@@ -1,7 +1,15 @@
1
1
  # XXTEA 加密算法的 Ruby 实现
2
2
 
3
+ <a href="https://github.com/xxtea/">
4
+ <img src="https://avatars1.githubusercontent.com/u/6683159?v=3&s=86" alt="XXTEA logo" title="XXTEA" align="right" />
5
+ </a>
6
+
3
7
  [![Build Status](https://travis-ci.org/xxtea/xxtea-ruby.svg?branch=master)](https://travis-ci.org/xxtea/xxtea-ruby)
4
8
  [![Gem](https://img.shields.io/gem/v/xxtea-ruby.svg)](https://rubygems.org/gems/xxtea-ruby)
9
+ [![Gem](https://img.shields.io/gem/dt/xxtea-ruby.svg)](https://rubygems.org/gems/xxtea-ruby)
10
+ [![Gem](https://img.shields.io/gem/dtv/xxtea-ruby.svg)](https://rubygems.org/gems/xxtea-ruby)
11
+ [![Gems](https://img.shields.io/gem/rt/xxtea-ruby.svg)](https://rubygems.org/gems/xxtea-ruby)
12
+ [![Gems](https://img.shields.io/gem/rd/xxtea-ruby.svg)](https://rubygems.org/gems/xxtea-ruby)
5
13
 
6
14
  ## 简介
7
15
 
@@ -33,4 +41,4 @@ XXTEA.decrypt_utf8(encrypt_data, key) == XXTEA.decrypt(encrypt_data, key).force_
33
41
 
34
42
  ## 注意
35
43
 
36
- 在 1.8.x 或更低版本的 Ruby 上,没有 decrypt_utf8 这个函数。
44
+ 在 1.8.x 或更低版本的 Ruby 上,decrypt_utf8 只是 decrypt 的一个别名。
data/Rakefile CHANGED
@@ -3,9 +3,11 @@ require 'rake/extensiontask'
3
3
  require 'rake/testtask'
4
4
  require 'rake/clean'
5
5
 
6
- Rake::ExtensionTask.new('xxtea') do |ext|
7
- ext.lib_dir = 'lib'
8
- end
6
+ #if !defined?(JRUBY_VERSION) then
7
+ Rake::ExtensionTask.new('xxtea') do |ext|
8
+ ext.lib_dir = 'lib/xxtea'
9
+ end
10
+ #end
9
11
 
10
12
  Rake::TestTask.new do |t|
11
13
  t.libs << 'test'
@@ -16,4 +18,6 @@ end
16
18
  desc "Run tests"
17
19
  task :default => :test
18
20
 
19
- Rake::Task[:test].prerequisites << :compile
21
+ #if !defined?(JRUBY_VERSION) then
22
+ Rake::Task[:test].prerequisites << :compile
23
+ #end
@@ -1,3 +1,38 @@
1
- require 'mkmf'
2
- $CFLAGS += ' -g -O3 -Wall '
3
- create_makefile('xxtea')
1
+ ############################################################
2
+ # #
3
+ # xxtea/extconf.rb #
4
+ # #
5
+ # XXTEA encryption algorithm library for Ruby. #
6
+ # #
7
+ # Encryption Algorithm Authors: #
8
+ # David J. Wheeler #
9
+ # Roger M. Needham #
10
+ # #
11
+ # Code Author: Ma Bingyao <mabingyao@gmail.com> #
12
+ # LastModified: Feb 12, 2016 #
13
+ # #
14
+ ############################################################
15
+
16
+ if !defined?(JRUBY_VERSION) then
17
+ require 'mkmf'
18
+ create_makefile('xxtea/xxtea')
19
+ else
20
+ file = File.open('CMakeLists.txt', 'w') do |file|
21
+ file.puts 'project(xxtea)'
22
+ file.puts 'cmake_minimum_required(VERSION 2.6)'
23
+ file.puts 'set(CMAKE_MACOSX_RPATH 1)' if RUBY_PLATFORM=~/darwin/
24
+ file.puts 'set(LIBXXTEA_SRC ' + File.expand_path(File.dirname(__FILE__) + '/xxtea.c') +')'
25
+ file.puts 'add_library(xxtea SHARED ${LIBXXTEA_SRC})'
26
+ file.puts 'set_target_properties(xxtea PROPERTIES PREFIX "")'
27
+ file.puts 'set_target_properties(xxtea PROPERTIES SUFFIX "")'
28
+ if RUBY_PLATFORM=~/darwin/ then
29
+ file.puts 'set_target_properties(xxtea PROPERTIES OUTPUT_NAME "xxtea.bundle")'
30
+ elsif RUBY_PLATFORM=~/win32|w32/ then
31
+ file.puts 'set_target_properties(xxtea PROPERTIES OUTPUT_NAME "xxtea.dll")'
32
+ else
33
+ file.puts 'set_target_properties(xxtea PROPERTIES OUTPUT_NAME "xxtea.so")'
34
+ end
35
+ file.puts 'install(TARGETS xxtea DESTINATION ' + File.expand_path(File.dirname(__FILE__) + '/../../lib/xxtea') + ')'
36
+ end
37
+ system('cmake -G "Unix Makefiles" .')
38
+ end
@@ -1,8 +1,8 @@
1
1
  /**********************************************************\
2
2
  | |
3
- | xxtea.c |
3
+ | ruby_xxtea.c |
4
4
  | |
5
- | XXTEA encryption algorithm library for Lua. |
5
+ | XXTEA encryption algorithm library for Ruby. |
6
6
  | |
7
7
  | Encryption Algorithm Authors: |
8
8
  | David J. Wheeler |
@@ -10,28 +10,31 @@
10
10
  | |
11
11
  | Code Authors: Chen fei <cf850118@163.com> |
12
12
  | Ma Bingyao <mabingyao@gmail.com> |
13
- | LastModified: Feb 8, 2016 |
13
+ | LastModified: Feb 11, 2016 |
14
14
  | |
15
15
  \**********************************************************/
16
16
 
17
17
  #include <ruby.h>
18
- #ifdef RSTRING_NOEMBED
19
- #include <ruby/encoding.h>
20
- #endif
21
18
  #include "xxtea.h"
22
19
 
23
20
  VALUE rb_encrypt(VALUE mod, VALUE data, VALUE key) {
24
- unsigned char * result;
21
+ void * result;
25
22
  VALUE retval;
26
23
  size_t data_len, out_len;
27
24
 
25
+ if (data == Qnil) return rb_str_new(0,0);
26
+
28
27
  Check_Type(data, T_STRING);
29
28
  Check_Type(key, T_STRING);
30
29
 
31
30
  data_len = RSTRING_LEN(data);
32
31
 
32
+ if (data_len == 0) return data;
33
+
33
34
  result = xxtea_encrypt(RSTRING_PTR(data), data_len, RSTRING_PTR(key), &out_len);
34
35
 
36
+ if (result == NULL) return rb_str_new(0,0);
37
+
35
38
  retval = rb_str_new((const char *)result, out_len);
36
39
 
37
40
  free(result);
@@ -40,17 +43,23 @@ VALUE rb_encrypt(VALUE mod, VALUE data, VALUE key) {
40
43
  }
41
44
 
42
45
  VALUE rb_decrypt(VALUE mod, VALUE data, VALUE key) {
43
- unsigned char *result;
46
+ void * result;
44
47
  VALUE retval;
45
48
  size_t data_len, out_len;
46
49
 
50
+ if (data == Qnil) return rb_str_new(0,0);
51
+
47
52
  Check_Type(data, T_STRING);
48
53
  Check_Type(key, T_STRING);
49
54
 
50
55
  data_len = RSTRING_LEN(data);
51
56
 
57
+ if (data_len == 0) return data;
58
+
52
59
  result = xxtea_decrypt(RSTRING_PTR(data), data_len, RSTRING_PTR(key), &out_len);
53
60
 
61
+ if (result == NULL) return rb_str_new(0,0);
62
+
54
63
  retval = rb_str_new((const char *)result, out_len);
55
64
 
56
65
  free(result);
@@ -58,17 +67,8 @@ VALUE rb_decrypt(VALUE mod, VALUE data, VALUE key) {
58
67
  return retval;
59
68
  }
60
69
 
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
70
  void Init_xxtea() {
68
71
  VALUE XXTEA = rb_define_module("XXTEA");
69
72
  rb_define_singleton_method(XXTEA, "encrypt", rb_encrypt, 2);
70
73
  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
74
  }
@@ -0,0 +1,38 @@
1
+ ############################################################
2
+ # #
3
+ # xxtea.rb #
4
+ # #
5
+ # XXTEA encryption algorithm library for Ruby. #
6
+ # #
7
+ # Encryption Algorithm Authors: #
8
+ # David J. Wheeler #
9
+ # Roger M. Needham #
10
+ # #
11
+ # Code Author: Ma Bingyao <mabingyao@gmail.com> #
12
+ # LastModified: Feb 12, 2016 #
13
+ # #
14
+ ############################################################
15
+
16
+ if File.exists?(File.dirname(__FILE__) + '/xxtea.' + (RUBY_PLATFORM=~/darwin/ ? 'dylib' : RUBY_PLATFORM=~/win32|w32/ ? 'dll' : 'so')) then
17
+ if !defined?(JRUBY_VERSION) then
18
+ require "xxtea/xxtea"
19
+ else
20
+ require "xxtea/xxtea_ffi"
21
+ end
22
+ else
23
+ require "xxtea/xxtea_ruby"
24
+ end
25
+
26
+ module XXTEA
27
+ extend self
28
+ VERSION = "1.3.0"
29
+ if RUBY_VERSION >= "1.9.0" then
30
+ def decrypt_utf8(data, key)
31
+ decrypt(data, key).force_encoding(Encoding::UTF_8)
32
+ end
33
+ else
34
+ def decrypt_utf8(data, key)
35
+ decrypt(data, key)
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,63 @@
1
+ ############################################################
2
+ # #
3
+ # xxtea_ffi.rb #
4
+ # #
5
+ # XXTEA encryption algorithm library for Ruby. #
6
+ # #
7
+ # Encryption Algorithm Authors: #
8
+ # David J. Wheeler #
9
+ # Roger M. Needham #
10
+ # #
11
+ # Code Author: Ma Bingyao <mabingyao@gmail.com> #
12
+ # LastModified: Feb 12, 2016 #
13
+ # #
14
+ ############################################################
15
+
16
+ require "ffi"
17
+
18
+ module FFI
19
+ class Pointer
20
+ type = FFI.find_type(:size_t)
21
+ type, _ = FFI::TypeDefs.find do |(name, t)|
22
+ method_defined? "read_#{name}" if t == type
23
+ end
24
+ alias_method :read_size_t, "read_#{type}" if type
25
+ end
26
+ end
27
+
28
+ module XXTEA
29
+ private
30
+ module LIB
31
+ extend FFI::Library
32
+ ffi_lib File.dirname(__FILE__) + '/xxtea.' + (RUBY_PLATFORM=~/darwin/ ? 'dylib' : RUBY_PLATFORM=~/win32|w32/ ? 'dll' : 'so')
33
+ attach_function :xxtea_encrypt, [:pointer, :size_t, :pointer, :pointer], :pointer
34
+ attach_function :xxtea_decrypt, [:pointer, :size_t, :pointer, :pointer], :pointer
35
+ attach_function :free, [:pointer], :void
36
+ end
37
+ public
38
+ extend self
39
+ def encrypt(data, key)
40
+ return nil if data.nil?
41
+ data_len = data.bytesize
42
+ data = FFI::MemoryPointer.from_string(data)
43
+ key = FFI::MemoryPointer.from_string(key)
44
+ out_len = FFI::MemoryPointer.new(:size_t, 1)
45
+ result = LIB.xxtea_encrypt(data, data_len, key, out_len)
46
+ return nil if result.null?
47
+ retval = result.read_bytes(out_len.read_size_t)
48
+ LIB.free(result)
49
+ return retval
50
+ end
51
+ def decrypt(data, key)
52
+ return nil if data.nil?
53
+ data_len = data.bytesize
54
+ data = FFI::MemoryPointer.from_string(data)
55
+ key = FFI::MemoryPointer.from_string(key)
56
+ out_len = FFI::MemoryPointer.new(:size_t, 1)
57
+ result = LIB.xxtea_decrypt(data, data_len, key, out_len)
58
+ return nil if result.null?
59
+ retval = result.read_bytes(out_len.read_size_t)
60
+ LIB.free(result)
61
+ return retval
62
+ end
63
+ end
@@ -0,0 +1,104 @@
1
+ ############################################################
2
+ # #
3
+ # xxtea_ruby.rb #
4
+ # #
5
+ # XXTEA encryption algorithm library for Ruby. #
6
+ # #
7
+ # Encryption Algorithm Authors: #
8
+ # David J. Wheeler #
9
+ # Roger M. Needham #
10
+ # #
11
+ # Code Author: Ma Bingyao <mabingyao@gmail.com> #
12
+ # LastModified: Feb 11, 2016 #
13
+ # #
14
+ ############################################################
15
+ module XXTEA
16
+
17
+ private
18
+ DELTA = 0x9e3779b9
19
+
20
+ extend self
21
+
22
+ def uint32_array_to_bytes(v, w)
23
+ n = (v.size - 1) << 2
24
+ if w then
25
+ m = v.last
26
+ return '' if (m < n - 3) or (m > n)
27
+ n = m
28
+ end
29
+ s = v.pack("V*")
30
+ w ? s[0, n] : s
31
+ end
32
+
33
+ def bytes_to_uint32_array(s, w)
34
+ n = s.bytesize
35
+ v = s.ljust((4 - (n & 3) & 3) + n, "\0").unpack("V*")
36
+ v[v.size] = n if w
37
+ v
38
+ end
39
+
40
+ if RUBY_VERSION >= "1.9.0" then
41
+ def str_to_uint32_array(s, w)
42
+ s = String.new(s).force_encoding(Encoding::BINARY) if (s.encoding != Encoding::BINARY)
43
+ bytes_to_uint32_array(s, w)
44
+ end
45
+ else
46
+ alias :str_to_uint32_array :bytes_to_uint32_array
47
+ end
48
+
49
+ def mx(sum, y, z, p, e, k)
50
+ ((z >> 5 ^ y << 2) + (y >> 3 ^ z << 4)) ^ ((sum ^ y) + (k[p & 3 ^ e] ^ z))
51
+ end
52
+
53
+ def encrypt_uint32_array(v, k)
54
+ n = v.size - 1
55
+ z = v[n]
56
+ y = 0
57
+ sum = 0
58
+ (6 + 52 / (n + 1)).downto(1) {
59
+ sum = (sum + DELTA) & 0xffffffff
60
+ e = (sum >> 2) & 3
61
+ for p in (0...n)
62
+ y = v[p + 1]
63
+ z = v[p] = (v[p] + mx(sum, y, z, p, e, k)) & 0xffffffff
64
+ end
65
+ y = v[0]
66
+ z = v[n] = (v[n] + mx(sum, y, z, n, e, k)) & 0xffffffff
67
+ }
68
+ return v
69
+ end
70
+
71
+ def decrypt_uint32_array(v, k)
72
+ n = v.size - 1
73
+ z = 0
74
+ y = v[0]
75
+ sum = ((6 + 52 / (n + 1)) * DELTA) & 0xffffffff
76
+ while (sum != 0)
77
+ e = sum >> 2 & 3
78
+ n.downto(1) { |p|
79
+ z = v[p - 1]
80
+ y = v[p] = (v[p] - mx(sum, y, z, p, e, k)) & 0xffffffff
81
+ }
82
+ z = v[n]
83
+ y = v[0] = (v[0] - mx(sum, y, z, 0, e, k)) & 0xffffffff
84
+ sum = (sum - DELTA) & 0xffffffff
85
+ end
86
+ return v
87
+ end
88
+
89
+ public
90
+
91
+ def encrypt(data, key)
92
+ return '' if data.nil? or data.empty?
93
+ v = str_to_uint32_array(data, true)
94
+ k = str_to_uint32_array(key.ljust(16, "\0"), false)
95
+ uint32_array_to_bytes(encrypt_uint32_array(v, k), false)
96
+ end
97
+
98
+ def decrypt(data, key)
99
+ return '' if data.nil? or data.empty?
100
+ v = str_to_uint32_array(data, false)
101
+ k = str_to_uint32_array(key.ljust(16, "\0"), false)
102
+ uint32_array_to_bytes(decrypt_uint32_array(v, k), true)
103
+ end
104
+ end
@@ -2,9 +2,9 @@
2
2
  require "minitest/autorun"
3
3
  require "xxtea"
4
4
 
5
- describe "XXTEA" do
5
+ describe "xxtea" do
6
6
  it "test xxtea encrypt and decrypt" do
7
- text = "Hello World! 你好,中国!"
7
+ text = "Hello World! \0你好,中国!"
8
8
  key = "1234567890"
9
9
  encrypt_data = XXTEA.encrypt(text, key)
10
10
  if RUBY_VERSION >= "1.9.0" then
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'xxtea-ruby'
3
- s.version = '1.2.0'
3
+ s.version = '1.3.0'
4
4
  s.license = 'MIT'
5
5
  s.author = 'Ma Bingyao ( andot )'
6
6
  s.email = 'mabingyao@gmail.com'
@@ -17,4 +17,7 @@ EOF
17
17
  s.test_files = s.files.grep(%r{^test/})
18
18
  s.require_path = 'lib'
19
19
  s.extensions = ["ext/xxtea/extconf.rb"]
20
- end
20
+
21
+ s.add_runtime_dependency 'ffi', '~> 1.9', '>= 1.9.10'
22
+ s.requirements << 'cmake'
23
+ end
metadata CHANGED
@@ -1,15 +1,35 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: xxtea-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ma Bingyao ( andot )
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-02-07 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2016-02-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ffi
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.9'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 1.9.10
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '1.9'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 1.9.10
13
33
  description: |2
14
34
  XXTEA is a fast and secure encryption algorithm. This is a XXTEA library for Ruby.
15
35
 
@@ -31,6 +51,9 @@ files:
31
51
  - ext/xxtea/ruby_xxtea.c
32
52
  - ext/xxtea/xxtea.c
33
53
  - ext/xxtea/xxtea.h
54
+ - lib/xxtea.rb
55
+ - lib/xxtea/xxtea_ffi.rb
56
+ - lib/xxtea/xxtea_ruby.rb
34
57
  - test/xxtea_test.rb
35
58
  - xxtea-ruby.gemspec
36
59
  homepage: https://github.com/xxtea/xxtea-ruby
@@ -51,7 +74,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
51
74
  - - ">="
52
75
  - !ruby/object:Gem::Version
53
76
  version: '0'
54
- requirements: []
77
+ requirements:
78
+ - cmake
55
79
  rubyforge_project:
56
80
  rubygems_version: 2.5.0
57
81
  signing_key: