block-tea 1.3.1 → 1.3.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/crypt/block_tea.rb +41 -25
  2. metadata +4 -4
@@ -7,13 +7,21 @@ module Crypt
7
7
  end
8
8
 
9
9
 
10
- # encrypt: Use Corrected Block TEA to encrypt plaintext using password
11
- # Return encrypted text as string
12
- def encrypt(plaintext)
13
- if plaintext.length == 0
14
- return('') # nothing to encrypt
15
- end
10
+ def encrypt(plaintext)
11
+ raise StandardError, "Nothing to encrypt" if plaintext.length == 0
12
+ encryption_algorithm(plaintext)
13
+ end
14
+
15
+
16
+ def decrypt(ciphertext)
17
+ raise StandardError, "Nothing to decrypt" if ciphertext.length == 0
18
+ decryption_algorithm(ciphertext)
19
+ end
20
+
16
21
 
22
+ private
23
+
24
+ def encryption_algorithm(plaintext)
17
25
  # 'escape' plaintext so chars outside ISO-8859-1 work in single-byte packing, but
18
26
  # keep spaces as spaces (not '%20') so encrypted text doesn't grow too long, and
19
27
  # convert result to longs
@@ -44,18 +52,12 @@ module Crypt
44
52
  z = v[n-1]
45
53
  }
46
54
 
47
- ciphertext = longs_to_str(v)
48
- return ciphertext.unpack('a*').pack('m').delete("\n") # base64 encode it without newlines
49
- end
50
-
55
+ return base64_encode(v)
56
+ end
51
57
 
52
- # decrypt: Use Corrected Block TEA to decrypt ciphertext using password
53
- def decrypt(ciphertext)
54
- if ciphertext.length == 0
55
- return('')
56
- end
57
58
 
58
- v = str_to_longs(ciphertext.unpack('m').pack("a*")) # base64 decode and convert to array of 'longs'
59
+ def decryption_algorithm(ciphertext)
60
+ v = base64_decode(ciphertext)
59
61
  k = str_to_longs(@password.ljust(16).slice(0,16))
60
62
  n = v.length
61
63
 
@@ -81,29 +83,43 @@ module Crypt
81
83
  plaintext = longs_to_str(v)
82
84
  # strip trailing null chars resulting from filling 4-char blocks:
83
85
  plaintext = plaintext.gsub(/\0+$/,'')
86
+
84
87
  return plaintext
85
- end
88
+ end
86
89
 
87
90
 
88
- private
91
+ def base64_encode(v)
92
+ ciphertext = longs_to_str(v)
93
+ ciphertext.unpack('a*').pack('m').delete("\n") # base64 encode it without newlines
94
+ end
95
+
96
+
97
+ def base64_decode(ciphertext)
98
+ v = ciphertext.unpack('m').pack("a*") # base64 decode and convert to array of 'longs'
99
+ str_to_longs(v)
100
+ end
89
101
 
90
102
 
91
103
  def mx(z, y, sum, p)
92
104
  ((z>>5 ^ ((y<<2)&0xffffffff)) + (y>>3 ^ ((z<<4)&0xffffffff)) ^ (sum^y) + (p ^ z)) & 0xffffffff
93
105
  end
94
106
 
95
- def self.str_to_longs(s, include_count = false)
96
- s << [0,0,0].pack('c*') # Pad with at most three nulls
97
- return s.unpack('L*')
98
- end
107
+
108
+ def self.str_to_longs(s, include_count = false)
109
+ s << [0,0,0].pack('c*') # Pad with at most three nulls
110
+ return s.unpack('L*')
111
+ end
112
+
99
113
 
100
114
  def str_to_longs(s, include_count = false) # :nodoc:
101
115
  self.class.str_to_longs s, include_count
102
116
  end
103
117
 
104
- def self.longs_to_str(l, include_count = false) # convert array of longs back to string
105
- l.pack('L*')
106
- end
118
+
119
+ def self.longs_to_str(l, include_count = false) # convert array of longs back to string
120
+ l.pack('L*')
121
+ end
122
+
107
123
 
108
124
  def longs_to_str(l, include_count = false) # :nodoc:
109
125
  self.class.longs_to_str l, include_count
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: block-tea
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 31
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
8
  - 3
9
- - 1
10
- version: 1.3.1
9
+ - 2
10
+ version: 1.3.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Mikhailov Anatoly
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-03-09 00:00:00 +06:00
18
+ date: 2011-03-10 00:00:00 +06:00
19
19
  default_executable: block_tea
20
20
  dependencies: []
21
21