block64 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/block64.gemspec +2 -2
- data/lib/block64.rb +120 -65
- metadata +4 -4
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.1.
|
1
|
+
0.1.4
|
data/block64.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{block64}
|
8
|
-
s.version = "0.1.
|
8
|
+
s.version = "0.1.4"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Martin Kozák"]
|
12
|
-
s.date = %q{2011-01-
|
12
|
+
s.date = %q{2011-01-18}
|
13
13
|
s.email = %q{martinkozak@martinkozak.net}
|
14
14
|
s.extra_rdoc_files = [
|
15
15
|
"LICENSE.txt",
|
data/lib/block64.rb
CHANGED
@@ -1,76 +1,131 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
#
|
3
|
+
# Copyright (c) 2007 Bart Teeuwisse
|
4
|
+
# Copyright (c) 2010 - 2011 Martin Kozák
|
5
|
+
#
|
6
|
+
|
1
7
|
require 'openssl'
|
2
8
|
require 'base64'
|
3
9
|
|
4
|
-
|
5
|
-
|
10
|
+
module OpenSSL
|
11
|
+
module PKey
|
12
|
+
|
13
|
+
##
|
14
|
+
# Encrypts and decrypts data of arbitrary length using RSA
|
15
|
+
# cyphers. Fixed and much faster fork of "crypto64" gem.
|
16
|
+
#
|
17
|
+
|
18
|
+
class RSA
|
6
19
|
|
7
|
-
|
20
|
+
##
|
21
|
+
# Matches the OpenSSH key bit count indicator.
|
22
|
+
#
|
23
|
+
|
24
|
+
BIT_MATCHER = /(\d+) bit/
|
25
|
+
|
26
|
+
##
|
27
|
+
# Holds size of the key.
|
28
|
+
#
|
29
|
+
|
30
|
+
@size
|
31
|
+
|
32
|
+
##
|
33
|
+
# Holds encrypting block size.
|
34
|
+
#
|
35
|
+
|
36
|
+
@encrypt_block_size
|
37
|
+
|
38
|
+
##
|
39
|
+
# Holds decrypting block size.
|
40
|
+
#
|
41
|
+
|
42
|
+
@decrypt_block_size
|
43
|
+
|
44
|
+
##
|
45
|
+
# Read the length of the private key.
|
46
|
+
# @return [Integer] size (length) of the private key
|
47
|
+
#
|
48
|
+
|
49
|
+
def size
|
50
|
+
if @size.nil?
|
51
|
+
@size = self.class::BIT_MATCHER.match(self.to_text)[1].to_i
|
52
|
+
end
|
8
53
|
|
9
|
-
|
10
|
-
|
11
|
-
@size
|
12
|
-
@encrypt_block_size
|
13
|
-
@decrypt_block_size
|
14
|
-
|
15
|
-
# Read the length of the private key.
|
16
|
-
def size
|
17
|
-
if @size.nil?
|
18
|
-
@size = self.class::BIT_MATCHER.match(self.to_text)[1].to_i
|
19
|
-
end
|
20
|
-
|
21
|
-
return @size
|
22
|
-
end
|
54
|
+
return @size
|
55
|
+
end
|
23
56
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
57
|
+
##
|
58
|
+
# Calculate the block size when encrypting.
|
59
|
+
# @return [Integer] block size
|
60
|
+
#
|
61
|
+
|
62
|
+
def encrypt_block_size
|
63
|
+
if @encrypt_block_size.nil?
|
64
|
+
@encrypt_block_size = (self.size / 8) - 14
|
65
|
+
end
|
66
|
+
|
67
|
+
return @encrypt_block_size
|
68
|
+
end
|
32
69
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
70
|
+
##
|
71
|
+
# Calculate the block size when decrypting.
|
72
|
+
# @return [Integer] block size
|
73
|
+
#
|
74
|
+
|
75
|
+
def decrypt_block_size
|
76
|
+
if @decrypt_block_size.nil?
|
77
|
+
@decrypt_block_size = (self.size / 8) - 1
|
78
|
+
end
|
79
|
+
|
80
|
+
return @decrypt_block_size
|
81
|
+
end
|
41
82
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
83
|
+
##
|
84
|
+
# Encrypt and base64 encode data of arbitrary length.
|
85
|
+
#
|
86
|
+
# @param [String] str string for encrypting
|
87
|
+
# @return [String] encrypted string
|
88
|
+
#
|
89
|
+
|
90
|
+
def encrypt64(str)
|
91
|
+
enc = ''
|
92
|
+
str = str.dup
|
93
|
+
encrypt_block_size = self.encrypt_block_size
|
94
|
+
|
95
|
+
while str.length > encrypt_block_size
|
96
|
+
enc << self.public_encrypt(str[0..encrypt_block_size])
|
97
|
+
str.replace(str[encrypt_block_size + 1..-1])
|
98
|
+
end
|
99
|
+
|
100
|
+
if str.length > 0
|
101
|
+
enc << self.public_encrypt(str[0..encrypt_block_size])
|
102
|
+
end
|
103
|
+
|
104
|
+
return Base64.encode64(enc)
|
105
|
+
end
|
59
106
|
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
107
|
+
##
|
108
|
+
# Decrypt and base64 decode data of arbitrary length.
|
109
|
+
#
|
110
|
+
# @param [String] str string for decrypting
|
111
|
+
# @return [String] decrypted string
|
112
|
+
#
|
113
|
+
|
114
|
+
def decrypt64(str)
|
115
|
+
dec = ''
|
116
|
+
str = Base64.decode64(str)
|
117
|
+
decrypt_block_size = self.decrypt_block_size
|
118
|
+
|
119
|
+
while str.length > 0
|
120
|
+
dec << self.private_decrypt(str[0..decrypt_block_size])
|
121
|
+
if str.length > decrypt_block_size
|
122
|
+
str.replace(str[decrypt_block_size + 1..-1])
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
return dec
|
127
|
+
end
|
128
|
+
|
129
|
+
end
|
71
130
|
end
|
72
|
-
|
73
|
-
return dec
|
74
|
-
end
|
75
|
-
|
76
131
|
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 1
|
8
|
-
-
|
9
|
-
version: 0.1.
|
8
|
+
- 4
|
9
|
+
version: 0.1.4
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- "Martin Koz\xC3\xA1k"
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2011-01-
|
17
|
+
date: 2011-01-18 00:00:00 +01:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -80,7 +80,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
80
80
|
requirements:
|
81
81
|
- - ">="
|
82
82
|
- !ruby/object:Gem::Version
|
83
|
-
hash:
|
83
|
+
hash: 3737735988129475837
|
84
84
|
segments:
|
85
85
|
- 0
|
86
86
|
version: "0"
|