crypt-tea 1.2.0 → 1.3.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.
@@ -1,20 +1,24 @@
1
1
  = Crypt::XXTEA
2
2
 
3
- * http://github.com/sprsquish/crypt--xxtea
3
+ * http://crypt-tea.rubyforge.org/
4
4
 
5
5
  == DESCRIPTION:
6
6
 
7
- An implementation of the Tiny Encryption Algorithm that's
8
- compatible with PHP's xxTEA
9
-
10
- == REQUIREMENTS:
11
-
12
- * rubygems
7
+ An implementation of the Tiny Encryption Algorithm that's compatible with PHP's xxTEA
13
8
 
14
9
  == INSTALL:
15
10
 
16
11
  * sudo gem install crypt-tea
17
12
 
13
+ == USAGE:
14
+
15
+ require 'rubygems'
16
+ require 'crypt-tea'
17
+ key = Crypt::XXTEA.new('super secret key')
18
+ plaintext = 'some text that needs to be encrypted'
19
+ cyphertext = key.encrypt(plaintext)
20
+ key.decrypt(cyphertext) == plaintext
21
+
18
22
  == LICENSE:
19
23
 
20
24
  (The MIT License)
@@ -1,2 +1 @@
1
- $:.unshift File.dirname(__FILE__)
2
- require 'crypt_tea/xxtea'
1
+ require File.join(File.dirname(__FILE__), *%w[crypt_tea xxtea])
@@ -1,6 +1,5 @@
1
1
  module Crypt
2
2
  class XXTEA
3
- VERSION = '1.2.0'
4
3
  DELTA = 0x9E3779B9
5
4
 
6
5
  def initialize(new_key)
@@ -18,7 +17,8 @@ module Crypt
18
17
  end
19
18
  end
20
19
 
21
- def self.str_to_longs(s, include_count = false)
20
+ def self.str_to_longs(s, include_count = false) # :nodoc:
21
+ s = s.dup
22
22
  length = s.length
23
23
  ((4 - s.length % 4) & 3).times { s << "\0" } # Pad to a multiple of 4
24
24
  unpacked = s.unpack('V*').collect { |n| int32 n }
@@ -26,41 +26,49 @@ module Crypt
26
26
  unpacked
27
27
  end
28
28
 
29
- def str_to_longs(s, include_count = false)
29
+ def str_to_longs(s, include_count = false) # :nodoc:
30
30
  self.class.str_to_longs s, include_count
31
31
  end
32
32
 
33
33
 
34
34
  ##
35
35
  # convert array of longs back to string
36
- def self.longs_to_str(l, count_included = false)
36
+ def self.longs_to_str(l, count_included = false) # :nodoc:
37
37
  s = l.pack('V*')
38
38
  s = s[0...(l[-1])] if count_included
39
39
  s
40
40
  end
41
41
 
42
- def longs_to_str(l, count_included = false)
42
+ def longs_to_str(l, count_included = false) # :nodoc:
43
43
  self.class.longs_to_str l, count_included
44
44
  end
45
45
 
46
46
 
47
- def self.int32(n)
47
+ def self.int32(n) # :nodoc:
48
48
  n -= 4_294_967_296 while (n >= 2_147_483_648)
49
49
  n += 4_294_967_296 while (n <= -2_147_483_648)
50
50
  n.to_i
51
51
  end
52
52
 
53
- def int32(n)
53
+ def int32(n) # :nodoc:
54
54
  self.class.int32 n
55
55
  end
56
56
 
57
- def mx(z, y, sum, p, e)
57
+ def mx(z, y, sum, p, e) # :nodoc:
58
58
  int32(
59
59
  ((z >> 5 & 0x07FFFFFF) ^ (y << 2)) +
60
60
  ((y >> 3 & 0x1FFFFFFF) ^ (z << 4))
61
61
  ) ^ int32((sum ^ y) + (@key[(p & 3) ^ e] ^ z))
62
62
  end
63
63
 
64
+ ##
65
+ # Encrypt the +plaintext+ using the +key+
66
+ def self.encrypt(key, plaintext)
67
+ self.new(key).encrypt(plaintext)
68
+ end
69
+
70
+ ##
71
+ # Encrypt the +plaintext+
64
72
  def encrypt(plaintext)
65
73
  return '' if plaintext.length == 0
66
74
 
@@ -93,9 +101,14 @@ module Crypt
93
101
  longs_to_str(v).unpack('a*').pack('m').delete("\n") # base64 encode it without newlines
94
102
  end
95
103
 
96
- #
97
- # decrypt: Use Corrected Block TEA to decrypt ciphertext using password
98
- #
104
+ ##
105
+ # Decrypt the +ciphertext+ using the +key+
106
+ def self.decrypt(key, ciphertext)
107
+ self.new(key).decrypt(ciphertext)
108
+ end
109
+
110
+ ##
111
+ # Decrypt the +ciphertext+
99
112
  def decrypt(ciphertext)
100
113
  return '' if ciphertext.length == 0
101
114
 
@@ -0,0 +1,56 @@
1
+ require 'rubygems'
2
+ require 'benchmark'
3
+ require File.join(File.dirname(__FILE__), *%w[.. lib crypt_tea])
4
+
5
+ KEY = Crypt::XXTEA.new 'abigfattestkey'
6
+ STRINGS = {}
7
+
8
+ CHARS = [('a'..'z'),('A'..'Z'),('0'..'9')].map{|i| i.to_a}.flatten
9
+ def rand_string(size)
10
+ (0...size).map { CHARS[rand(CHARS.length)] }.join
11
+ end
12
+
13
+ Benchmark.bm do |x|
14
+ [1, 10, 100, 1_000, 10_000, 100_000].each do |size|
15
+ STRINGS[size] = rand_string(size)
16
+ x.report(size.to_s.rjust(6)) { KEY.decrypt(KEY.encrypt(STRINGS[size])) }
17
+ end
18
+ end
19
+
20
+ __END__
21
+
22
+ Ruby 1.8.6
23
+ user system total real
24
+ 1 0.000000 0.000000 0.000000 ( 0.002733)
25
+ 10 0.000000 0.000000 0.000000 ( 0.003002)
26
+ 100 0.010000 0.010000 0.020000 ( 0.007730)
27
+ 1000 0.070000 0.000000 0.070000 ( 0.077805)
28
+ 10000 0.720000 0.010000 0.730000 ( 0.738411)
29
+ 100000 7.780000 0.040000 7.820000 ( 7.932519)
30
+
31
+ Ruby 1.9.1p129
32
+ user system total real
33
+ 1 0.010000 0.000000 0.010000 ( 0.013437)
34
+ 10 0.000000 0.000000 0.000000 ( 0.001345)
35
+ 100 0.000000 0.000000 0.000000 ( 0.003279)
36
+ 1000 0.040000 0.000000 0.040000 ( 0.040562)
37
+ 10000 0.390000 0.010000 0.400000 ( 0.402591)
38
+ 100000 3.170000 0.020000 3.190000 ( 3.243170)
39
+
40
+ jRuby 1.2.0
41
+ user system total real
42
+ 1 0.026000 0.000000 0.026000 ( 0.026000)
43
+ 10 0.018000 0.000000 0.018000 ( 0.018000)
44
+ 100 0.040000 0.000000 0.040000 ( 0.040000)
45
+ 1000 0.220000 0.000000 0.220000 ( 0.220000)
46
+ 10000 0.682000 0.000000 0.682000 ( 0.682000)
47
+ 100000 1.281000 0.000000 1.281000 ( 1.281000)
48
+
49
+ jRuby 1.3.0RC2
50
+ user system total real
51
+ 1 0.026000 0.000000 0.026000 ( 0.025000)
52
+ 10 0.020000 0.000000 0.020000 ( 0.020000)
53
+ 100 0.040000 0.000000 0.040000 ( 0.041000)
54
+ 1000 0.255000 0.000000 0.255000 ( 0.254000)
55
+ 10000 0.499000 0.000000 0.499000 ( 0.500000)
56
+ 100000 1.805000 0.000000 1.805000 ( 1.806000)
@@ -1,42 +1,62 @@
1
- $:.unshift File.dirname(__FILE__) + '/../lib'
1
+ require 'rubygems'
2
+ require 'minitest/spec'
3
+ require File.join(File.dirname(__FILE__), *%w[.. lib crypt_tea])
2
4
 
3
- require 'test/unit'
4
- require 'crypt_tea'
5
+ MiniTest::Unit.autorun
5
6
 
6
- class XXTEATest < Test::Unit::TestCase
7
- def setup
8
- @key = Crypt::XXTEA.new 'abigfattestkey'
7
+ describe Crypt::XXTEA do
8
+ before do
9
+ @key_text = 'abigfattestkey'
10
+ @key = Crypt::XXTEA.new @key_text
9
11
  @plaintext = "Oh say can you see, by the dawn's early light"
10
12
  @cyphertext = "V32cYZc5yLXepm9lxzr4kgGM/eSVurwV0yQWi4uFs0uB2UBlJ19ZRKKMkbMr7DLGc3n1XQ=="
11
13
  end
12
14
 
13
- def test_str_to_long
14
- assert_equal [1953719668, 6778473, 7], Crypt::XXTEA.str_to_longs('testing', true)
15
+ it 'converts strings to longs' do
16
+ Crypt::XXTEA.str_to_longs('testing', true).must_equal [1953719668, 6778473, 7]
15
17
  end
16
18
 
17
- def test_longs_to_str
18
- assert_equal 'testing', Crypt::XXTEA.longs_to_str([1953719668, 6778473, 7], true)
19
+ it 'converts longs to strings' do
20
+ Crypt::XXTEA.longs_to_str([1953719668, 6778473, 7], true).must_equal 'testing'
19
21
  end
20
22
 
21
- def test_key_length
22
- assert_raise(RuntimeError) { Crypt::XXTEA.new '12345678901234567' }
23
- assert_raise(RuntimeError) { Crypt::XXTEA.new '' }
23
+ it 'raises an error when the key is too long' do
24
+ proc { Crypt::XXTEA.new '12345678901234567' }.must_raise RuntimeError
24
25
  end
25
26
 
26
- def test_encrypt
27
- assert_equal @cyphertext, @key.encrypt(@plaintext)
27
+ it 'raises an error when the key is blank' do
28
+ proc { Crypt::XXTEA.new '' }.must_raise RuntimeError
28
29
  end
29
30
 
30
- def test_decrypt
31
- assert_equal @plaintext, @key.decrypt(@cyphertext)
31
+ it 'works properly with small keys' do
32
+ key = Crypt::XXTEA.new '123'
33
+ cyphertext = key.encrypt(@plaintext)
34
+ key.decrypt(cyphertext).must_equal @plaintext
32
35
  end
33
36
 
34
- def test_tiny_plaintext
35
- assert_equal '1', @key.decrypt(@key.encrypt('1'))
37
+ it 'properly encrypts when instantiated' do
38
+ @key.encrypt(@plaintext).must_equal @cyphertext
36
39
  end
37
40
 
38
- def test_huge_plaintext
41
+ it 'properly decrypts when instantiated' do
42
+ @key.decrypt(@cyphertext).must_equal @plaintext
43
+ end
44
+
45
+ it 'properly en/decrypts tiny text' do
46
+ txt = '1'
47
+ @key.decrypt(@key.encrypt(txt)).must_equal txt
48
+ end
49
+
50
+ it 'properly en/decrypts huge text' do
39
51
  str = '1234567890' * 1_000
40
- assert_equal str, @key.decrypt(@key.encrypt(str))
52
+ @key.decrypt(@key.encrypt(str)).must_equal str
53
+ end
54
+
55
+ it 'properly encrypts with a class method' do
56
+ Crypt::XXTEA.encrypt(@key_text, @plaintext).must_equal @cyphertext
57
+ end
58
+
59
+ it 'properly decrypts with a class method' do
60
+ Crypt::XXTEA.decrypt(@key_text, @cyphertext).must_equal @plaintext
41
61
  end
42
62
  end
metadata CHANGED
@@ -1,80 +1,38 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: crypt-tea
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
  - Jeff Smick
8
- - James Zhang
9
8
  autorequire:
10
9
  bindir: bin
11
- cert_chain:
12
- - |
13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aDLz/fSjMCgUPBe6a3vcgvGV+B7AQ7MOABU58dPUCFXSQR/
30
- uT42BT+P+4TaAbBCICCSIM0yVs8a3jJ8aVdwre3Xc0bvcdMpZ22wNeaB6Lo3/v51
31
- M6zkPH2hkWs=
32
- -----END CERTIFICATE-----
10
+ cert_chain: []
33
11
 
34
- date: 2009-05-27 00:00:00 -07:00
35
- default_executable:
36
- dependencies:
37
- - !ruby/object:Gem::Dependency
38
- name: hoe
39
- type: :development
40
- version_requirement:
41
- version_requirements: !ruby/object:Gem::Requirement
42
- requirements:
43
- - - ">="
44
- - !ruby/object:Gem::Version
45
- version: 1.12.2
46
- version:
47
- description: |-
48
- An implementation of the Tiny Encryption Algorithm that's
49
- compatible with PHP's xxTEA
50
- email:
51
- - sprsquish@gmail.com
52
- - james@aftershocksf.com
12
+ date: 2009-06-05 00:00:00 -07:00
13
+ default_executable: xxtea
14
+ dependencies: []
15
+
16
+ description: An implementation of the Tiny Encryption Algorithm that's compatible with PHP's xxTEA
17
+ email: sprsquish@gmail.com
53
18
  executables:
54
19
  - xxtea
55
20
  extensions: []
56
21
 
57
22
  extra_rdoc_files:
58
- - History.txt
59
- - Manifest.txt
60
- - README.txt
23
+ - README.rdoc
61
24
  files:
62
- - History.txt
63
- - Manifest.txt
64
- - README.txt
65
- - Rakefile
66
25
  - bin/xxtea
67
26
  - lib/crypt_tea.rb
68
27
  - lib/crypt_tea/xxtea.rb
69
- - test/test_xxtea.rb
28
+ - README.rdoc
70
29
  has_rdoc: true
71
- homepage: http://github.com/sprsquish/crypt--xxtea
30
+ homepage: http://crypt-tea.rubyforge.org
72
31
  licenses: []
73
32
 
74
33
  post_install_message:
75
34
  rdoc_options:
76
- - --main
77
- - README.txt
35
+ - --charset=UTF-8
78
36
  require_paths:
79
37
  - lib
80
38
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -92,9 +50,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
92
50
  requirements: []
93
51
 
94
52
  rubyforge_project: crypt-tea
95
- rubygems_version: 1.3.3
53
+ rubygems_version: 1.3.4
96
54
  signing_key:
97
55
  specification_version: 3
98
- summary: An implementation of the Tiny Encryption Algorithm that's compatible with PHP's xxTEA
56
+ summary: xxTEA implemented in pure ruby
99
57
  test_files:
58
+ - test/benchmark.rb
100
59
  - test/test_xxtea.rb
data.tar.gz.sig DELETED
@@ -1,3 +0,0 @@
1
- �2=}4���Y��|�q�����~U�+-��L VK6�=p���-dRs����j�'�_c�)��P;�,n��7?��������ǓEjK�$�j�f%'�Cl�l<�C��.��T�
2
- Ō�*J�����Ѷ�((�F�1p����h<��ۘh"w��<��Z`%N(-Q� ϧ^�u��f4��!�s��$
3
- ����b!H ���}��N�QEO8���&(��ġ���R-]5�*,���r�6��D��{=F�0��
@@ -1,18 +0,0 @@
1
- === 1.2.0 / 2009-05-27
2
-
3
- * Ruby 1.9 Compliant!
4
-
5
- === 1.1.0 / 2009-04-06
6
-
7
- * BUGFIX
8
-
9
- * ensure extra characters on the end of decrypted strings are removed
10
- * Thanks to James Zhang for reporting this
11
-
12
-
13
- === 1.0.0 / 2008-04-02
14
-
15
- * 1 major enhancement
16
-
17
- * Birthday!
18
-
@@ -1,8 +0,0 @@
1
- History.txt
2
- Manifest.txt
3
- README.txt
4
- Rakefile
5
- bin/xxtea
6
- lib/crypt_tea.rb
7
- lib/crypt_tea/xxtea.rb
8
- test/test_xxtea.rb
data/Rakefile DELETED
@@ -1,8 +0,0 @@
1
- require 'rubygems'
2
- require 'hoe'
3
- require './lib/crypt_tea.rb'
4
-
5
- Hoe.new('crypt-tea', Crypt::XXTEA::VERSION) do |p|
6
- p.developer('Jeff Smick', 'sprsquish@gmail.com')
7
- p.developer('James Zhang', 'james@aftershocksf.com')
8
- end
metadata.gz.sig DELETED
Binary file