crypt-tea 1.0.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.
- data.tar.gz.sig +3 -0
- data/History.txt +6 -0
- data/Manifest.txt +8 -0
- data/README.txt +41 -0
- data/Rakefile +7 -0
- data/bin/xxtea +17 -0
- data/lib/crypt_tea.rb +2 -0
- data/lib/crypt_tea/xxtea.rb +129 -0
- data/test/test_xxtea.rb +31 -0
- metadata +93 -0
- metadata.gz.sig +0 -0
data.tar.gz.sig
ADDED
data/History.txt
ADDED
data/Manifest.txt
ADDED
data/README.txt
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
= Crypt::XXTEA
|
2
|
+
|
3
|
+
* http://github.com/sprsquish/rubytea
|
4
|
+
|
5
|
+
== DESCRIPTION:
|
6
|
+
|
7
|
+
An implementation of the Tiny Encryption Algorithm that's
|
8
|
+
compatible with PHP's xxTEA
|
9
|
+
|
10
|
+
== REQUIREMENTS:
|
11
|
+
|
12
|
+
* rubygems
|
13
|
+
|
14
|
+
== INSTALL:
|
15
|
+
|
16
|
+
* sudo gem install crypt-tea
|
17
|
+
|
18
|
+
== LICENSE:
|
19
|
+
|
20
|
+
(The MIT License)
|
21
|
+
|
22
|
+
Copyright (c) 2008 Jeff Smick
|
23
|
+
|
24
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
25
|
+
a copy of this software and associated documentation files (the
|
26
|
+
'Software'), to deal in the Software without restriction, including
|
27
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
28
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
29
|
+
permit persons to whom the Software is furnished to do so, subject to
|
30
|
+
the following conditions:
|
31
|
+
|
32
|
+
The above copyright notice and this permission notice shall be
|
33
|
+
included in all copies or substantial portions of the Software.
|
34
|
+
|
35
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
36
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
37
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
38
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
39
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
40
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
41
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
data/bin/xxtea
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
unless (ARGV.length == 3) && (command = ARGV[0][/^enc/] ? :encrypt : (ARGV[0][/^dec/] ? :decrypt : nil))
|
4
|
+
puts ' Usage:'
|
5
|
+
puts " xxtea <command> <key> <plaintext/cyphertext>"
|
6
|
+
puts
|
7
|
+
puts " Commands:"
|
8
|
+
puts " enc, encrypt"
|
9
|
+
puts " dec, decrypt"
|
10
|
+
puts
|
11
|
+
else
|
12
|
+
require 'rubygems'
|
13
|
+
require 'crypt_tea'
|
14
|
+
|
15
|
+
k = Crypt::XXTEA.new(ARGV[1].dup)
|
16
|
+
puts k.send(command, ARGV[2].dup)
|
17
|
+
end
|
data/lib/crypt_tea.rb
ADDED
@@ -0,0 +1,129 @@
|
|
1
|
+
module Crypt
|
2
|
+
class XXTEA
|
3
|
+
VERSION = '1.0.0'
|
4
|
+
DELTA = 0x9E3779B9
|
5
|
+
|
6
|
+
def self.str_to_longs(s, include_count = false)
|
7
|
+
length = s.length
|
8
|
+
((4 - s.length % 4) & 3).times { s << "\0" } # Pad to a multiple of 4
|
9
|
+
unpacked = s.unpack('V*').collect { |n| int32 n }
|
10
|
+
unpacked << length if include_count
|
11
|
+
unpacked
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.longs_to_str(l, unpad = false) # convert array of longs back to string
|
15
|
+
s = l.pack('V*')
|
16
|
+
s = s.gsub(/[\000-\037]/,'') if unpad
|
17
|
+
s
|
18
|
+
end
|
19
|
+
|
20
|
+
|
21
|
+
def self.int32(n)
|
22
|
+
n -= 4294967296 while (n >= 2147483648)
|
23
|
+
n += 4294967296 while (n <= -2147483649)
|
24
|
+
n.to_i
|
25
|
+
end
|
26
|
+
|
27
|
+
|
28
|
+
def initialize(new_key)
|
29
|
+
@key = self.class.str_to_longs(new_key, false)
|
30
|
+
|
31
|
+
if @key.length > 4
|
32
|
+
raise 'Key is too long (no more than 16 chars)'
|
33
|
+
|
34
|
+
elsif @key.length == 0
|
35
|
+
raise 'Key cannot be empty'
|
36
|
+
|
37
|
+
elsif @key.length < 4
|
38
|
+
@key.length.upto(4) { |i| @key[i] = 0 }
|
39
|
+
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def encrypt(plaintext)
|
44
|
+
return '' if plaintext.length == 0
|
45
|
+
|
46
|
+
v = self.class.str_to_longs(plaintext, true)
|
47
|
+
v[1] = 0 if v.length == 1
|
48
|
+
|
49
|
+
n = v.length - 1
|
50
|
+
|
51
|
+
z = v[n]
|
52
|
+
y = v[0]
|
53
|
+
q = (6 + 52/ (n + 1)).floor
|
54
|
+
sum = 0
|
55
|
+
p = 0
|
56
|
+
|
57
|
+
while(0 <= (q -= 1)) do
|
58
|
+
sum = self.class.int32(sum + DELTA)
|
59
|
+
e = sum >> 2 & 3
|
60
|
+
|
61
|
+
(0...n).each do |p|
|
62
|
+
y = v[p + 1];
|
63
|
+
mx =
|
64
|
+
self.class.int32(
|
65
|
+
((z >> 5 & 0x07FFFFFF) ^ y << 2) +
|
66
|
+
((y >> 3 & 0x1FFFFFFF) ^ z << 4)
|
67
|
+
) ^ self.class.int32((sum ^ y) + (@key[p & 3 ^ e] ^ z))
|
68
|
+
|
69
|
+
z = v[p] = self.class.int32(v[p] + mx)
|
70
|
+
end
|
71
|
+
|
72
|
+
p += 1
|
73
|
+
y = v[0];
|
74
|
+
mx =
|
75
|
+
self.class.int32(
|
76
|
+
((z >> 5 & 0x07FFFFFF) ^ y << 2) +
|
77
|
+
((y >> 3 & 0x1FFFFFFF) ^ z << 4)
|
78
|
+
) ^ self.class.int32((sum ^ y) + (@key[p & 3 ^ e] ^ z))
|
79
|
+
|
80
|
+
z = v[p] = self.class.int32(v[p] + mx)
|
81
|
+
end
|
82
|
+
|
83
|
+
self.class.longs_to_str(v).unpack('a*').pack('m').delete("\n") # base64 encode it without newlines
|
84
|
+
end
|
85
|
+
|
86
|
+
#
|
87
|
+
# decrypt: Use Corrected Block TEA to decrypt ciphertext using password
|
88
|
+
#
|
89
|
+
def decrypt(ciphertext)
|
90
|
+
return '' if ciphertext.length == 0
|
91
|
+
|
92
|
+
v = self.class.str_to_longs(ciphertext.unpack('m').pack("a*")) # base64 decode and convert to array of 'longs'
|
93
|
+
n = v.length - 1
|
94
|
+
z = v[n]
|
95
|
+
y = v[0]
|
96
|
+
q = (6 + 52 / (n + 1)).floor
|
97
|
+
sum = self.class.int32(q * DELTA)
|
98
|
+
p = 0
|
99
|
+
|
100
|
+
while (sum != 0) do
|
101
|
+
e = sum >> 2 & 3
|
102
|
+
n.downto(1) do |p|
|
103
|
+
z = v[p - 1]
|
104
|
+
mx =
|
105
|
+
self.class.int32(
|
106
|
+
((z >> 5 & 0x07FFFFFF) ^ y << 2) +
|
107
|
+
((y >> 3 & 0x1FFFFFFF) ^ z << 4)
|
108
|
+
) ^ self.class.int32((sum ^ y) + (@key[p & 3 ^ e] ^ z))
|
109
|
+
|
110
|
+
y = v[p] = self.class.int32(v[p] - mx)
|
111
|
+
end
|
112
|
+
|
113
|
+
p -= 1
|
114
|
+
z = v[n]
|
115
|
+
mx =
|
116
|
+
self.class.int32(
|
117
|
+
((z >> 5 & 0x07FFFFFF) ^ y << 2) +
|
118
|
+
((y >> 3 & 0x1FFFFFFF) ^ z << 4)
|
119
|
+
) ^ self.class.int32((sum ^ y) + (@key[p & 3 ^ e] ^ z))
|
120
|
+
|
121
|
+
y = v[0] = self.class.int32(v[0] - mx)
|
122
|
+
sum = self.class.int32(sum - DELTA)
|
123
|
+
end
|
124
|
+
|
125
|
+
self.class.longs_to_str(v, true)
|
126
|
+
end
|
127
|
+
|
128
|
+
end
|
129
|
+
end
|
data/test/test_xxtea.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
$:.unshift File.dirname(__FILE__) + '/../lib'
|
2
|
+
|
3
|
+
require 'test/unit'
|
4
|
+
require 'crypt_tea'
|
5
|
+
|
6
|
+
class XXTEATest < Test::Unit::TestCase
|
7
|
+
def setup
|
8
|
+
@key = Crypt::XXTEA.new 'testkey'
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_str_to_long
|
12
|
+
assert_equal [1953719668, 6778473], Crypt::XXTEA.str_to_longs('testing')
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_longs_to_str
|
16
|
+
assert_equal 'testing', Crypt::XXTEA.longs_to_str([1953719668, 6778473], true)
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_key_length
|
20
|
+
assert_raise(RuntimeError) { Crypt::XXTEA.new '12345678901234567' }
|
21
|
+
assert_raise(RuntimeError) { Crypt::XXTEA.new '' }
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_encrypt
|
25
|
+
assert_equal 'qYkeGc3ignEvPGM7RS06iQ==', @key.encrypt('plaintext')
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_decrypt
|
29
|
+
assert_equal 'plaintext', @key.decrypt('qYkeGc3ignEvPGM7RS06iQ==')
|
30
|
+
end
|
31
|
+
end
|
metadata
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: crypt-tea
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jeff Smick
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain:
|
11
|
+
- |
|
12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2iSHLGkWPdtj1XbbVIG3X2YQ7qTz68A2U0Zh5s5+oXTp7lCWmM4s9
|
28
|
+
oSXucdvUZrG6FcqqtaDLz/fSjMCgUPBe6a3vcgvGV+B7AQ7MOABU58dPUCFXSQR/
|
29
|
+
uT42BT+P+4TaAbBCICCSIM0yVs8a3jJ8aVdwre3Xc0bvcdMpZ22wNeaB6Lo3/v51
|
30
|
+
M6zkPH2hkWs=
|
31
|
+
-----END CERTIFICATE-----
|
32
|
+
|
33
|
+
date: 2008-04-14 00:00:00 -07:00
|
34
|
+
default_executable:
|
35
|
+
dependencies:
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: hoe
|
38
|
+
version_requirement:
|
39
|
+
version_requirements: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 1.5.1
|
44
|
+
version:
|
45
|
+
description: An implementation of the Tiny Encryption Algorithm that's compatible with PHP's xxTEA
|
46
|
+
email:
|
47
|
+
- sprsquish@gmail.com
|
48
|
+
executables:
|
49
|
+
- xxtea
|
50
|
+
extensions: []
|
51
|
+
|
52
|
+
extra_rdoc_files:
|
53
|
+
- History.txt
|
54
|
+
- Manifest.txt
|
55
|
+
- README.txt
|
56
|
+
files:
|
57
|
+
- History.txt
|
58
|
+
- Manifest.txt
|
59
|
+
- README.txt
|
60
|
+
- Rakefile
|
61
|
+
- bin/xxtea
|
62
|
+
- lib/crypt_tea.rb
|
63
|
+
- lib/crypt_tea/xxtea.rb
|
64
|
+
- test/test_xxtea.rb
|
65
|
+
has_rdoc: true
|
66
|
+
homepage: http://github.com/sprsquish/rubytea
|
67
|
+
post_install_message:
|
68
|
+
rdoc_options:
|
69
|
+
- --main
|
70
|
+
- README.txt
|
71
|
+
require_paths:
|
72
|
+
- lib
|
73
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: "0"
|
78
|
+
version:
|
79
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: "0"
|
84
|
+
version:
|
85
|
+
requirements: []
|
86
|
+
|
87
|
+
rubyforge_project: crypt-tea
|
88
|
+
rubygems_version: 1.1.1
|
89
|
+
signing_key:
|
90
|
+
specification_version: 2
|
91
|
+
summary: An implementation of the Tiny Encryption Algorithm that's compatible with PHP's xxTEA
|
92
|
+
test_files:
|
93
|
+
- test/test_xxtea.rb
|
metadata.gz.sig
ADDED
Binary file
|