rgpg 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of rgpg might be problematic. Click here for more details.
- data/MIT-LICENSE.txt +21 -0
- data/bin/rgpg +4 -7
- data/lib/rgpg.rb +2 -0
- data/lib/rgpg/gem_info.rb +12 -0
- data/lib/rgpg/gpg_helper.rb +130 -0
- metadata +8 -4
- data/lib/gpg_helper.rb +0 -128
data/MIT-LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
Copyright (c) 2013 Richard Cook - http://rcook.org/
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
21
|
+
|
data/bin/rgpg
CHANGED
@@ -2,12 +2,9 @@
|
|
2
2
|
|
3
3
|
require 'pathname'
|
4
4
|
require 'tempfile'
|
5
|
-
require '
|
5
|
+
require 'rgpg'
|
6
6
|
|
7
|
-
THIS_PATH = Pathname.new(__FILE__).realpath
|
8
|
-
THIS_DIR = THIS_PATH.dirname
|
9
7
|
THIS_BASE_NAME = File.basename($0, File.extname($0))
|
10
|
-
|
11
8
|
GENERATE_KEY_PAIR_USAGE = '--generate-key-pair <key-base-name> <recipient> <real-name>'
|
12
9
|
ENCRYPT_USAGE = '--encrypt <public-key-file-name> <input-file-name> <output-file-name>'
|
13
10
|
DECRYPT_USAGE = '--decrypt <public-key-file-name> <private-key-file-name> <input-file-name> <output-file-name>'
|
@@ -17,14 +14,14 @@ if ARGV[0] == '--generate-key-pair'
|
|
17
14
|
key_base_name = ARGV[1]
|
18
15
|
recipient = ARGV[2]
|
19
16
|
real_name = ARGV[3]
|
20
|
-
GpgHelper.generate_key_pair(key_base_name, recipient, real_name)
|
17
|
+
Rgpg::GpgHelper.generate_key_pair(key_base_name, recipient, real_name)
|
21
18
|
exit 0
|
22
19
|
elsif ARGV[0] == '--encrypt'
|
23
20
|
raise RuntimeError.new(ENCRYPT_USAGE) unless ARGV.size == 4
|
24
21
|
public_key_file_name = ARGV[1]
|
25
22
|
input_file_name = ARGV[2]
|
26
23
|
output_file_name = ARGV[3]
|
27
|
-
GpgHelper.encrypt_file(public_key_file_name, input_file_name, output_file_name)
|
24
|
+
Rgpg::GpgHelper.encrypt_file(public_key_file_name, input_file_name, output_file_name)
|
28
25
|
exit 0
|
29
26
|
elsif ARGV[0] == '--decrypt'
|
30
27
|
raise RuntimeError.new(DECRYPT_USAGE) unless ARGV.size == 5
|
@@ -32,7 +29,7 @@ elsif ARGV[0] == '--decrypt'
|
|
32
29
|
private_key_file_name = ARGV[2]
|
33
30
|
input_file_name = ARGV[3]
|
34
31
|
output_file_name = ARGV[4]
|
35
|
-
GpgHelper.decrypt_file(public_key_file_name, private_key_file_name, input_file_name, output_file_name)
|
32
|
+
Rgpg::GpgHelper.decrypt_file(public_key_file_name, private_key_file_name, input_file_name, output_file_name)
|
36
33
|
exit 0
|
37
34
|
else
|
38
35
|
$stderr.puts "Unsupported command \"#{ARGV[0]}\"" unless ARGV[0].nil? || ARGV[0].size == 0
|
data/lib/rgpg.rb
ADDED
@@ -0,0 +1,130 @@
|
|
1
|
+
require 'tempfile'
|
2
|
+
|
3
|
+
module Rgpg
|
4
|
+
module GpgHelper
|
5
|
+
def self.generate_key_pair(key_base_name, recipient, real_name)
|
6
|
+
public_key_file_name = "#{key_base_name}.pub"
|
7
|
+
private_key_file_name = "#{key_base_name}.sec"
|
8
|
+
script = generate_key_script(public_key_file_name, private_key_file_name, recipient, real_name)
|
9
|
+
script_file = Tempfile.new('gpg-script')
|
10
|
+
begin
|
11
|
+
script_file.write(script)
|
12
|
+
script_file.close
|
13
|
+
result = system("gpg --batch --gen-key #{script_file.path}")
|
14
|
+
raise RuntimeError.new('gpg failed') unless result
|
15
|
+
ensure
|
16
|
+
script_file.close
|
17
|
+
script_file.unlink
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.encrypt_file(public_key_file_name, input_file_name, output_file_name)
|
22
|
+
recipient = get_recipient(public_key_file_name)
|
23
|
+
with_temporary_encrypt_keyring(public_key_file_name) do |keyring_file_name|
|
24
|
+
run_gpg(
|
25
|
+
'--keyring', keyring_file_name,
|
26
|
+
'--output', output_file_name,
|
27
|
+
'--encrypt',
|
28
|
+
'--recipient', recipient,
|
29
|
+
'--yes',
|
30
|
+
'--trust-model', 'always',
|
31
|
+
input_file_name
|
32
|
+
)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.decrypt_file(public_key_file_name, private_key_file_name, input_file_name, output_file_name)
|
37
|
+
recipient = get_recipient(private_key_file_name)
|
38
|
+
with_temporary_decrypt_keyrings(public_key_file_name, private_key_file_name) do |keyring_file_name, secret_keyring_file_name|
|
39
|
+
run_gpg(
|
40
|
+
'--keyring', keyring_file_name,
|
41
|
+
'--secret-keyring', secret_keyring_file_name,
|
42
|
+
'--output', output_file_name,
|
43
|
+
'--decrypt',
|
44
|
+
'--yes',
|
45
|
+
'--trust-model', 'always',
|
46
|
+
input_file_name
|
47
|
+
)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
private
|
52
|
+
|
53
|
+
def self.run_gpg(*args)
|
54
|
+
fragments = [
|
55
|
+
'gpg',
|
56
|
+
'--no-default-keyring'
|
57
|
+
] + args
|
58
|
+
command_line = fragments.join(' ')
|
59
|
+
puts command_line
|
60
|
+
result = system(command_line)
|
61
|
+
raise RuntimeError.new('gpg failed') unless result
|
62
|
+
end
|
63
|
+
|
64
|
+
def self.generate_key_script(public_key_file_name, private_key_file_name, recipient, real_name)
|
65
|
+
<<-EOS
|
66
|
+
%echo Generating a standard key
|
67
|
+
Key-Type: DSA
|
68
|
+
Key-Length: 1024
|
69
|
+
Subkey-Type: ELG-E
|
70
|
+
Subkey-Length: 1024
|
71
|
+
Name-Real: #{real_name}
|
72
|
+
Name-Comment: Key automatically generated by rgpg
|
73
|
+
Name-Email: #{recipient}
|
74
|
+
Expire-Date: 0
|
75
|
+
%pubring #{public_key_file_name}
|
76
|
+
%secring #{private_key_file_name}
|
77
|
+
# Do a commit here, so that we can later print "done" :-)
|
78
|
+
%commit
|
79
|
+
%echo done
|
80
|
+
EOS
|
81
|
+
end
|
82
|
+
|
83
|
+
def self.get_recipient(key_file_name)
|
84
|
+
result = `gpg #{key_file_name}`.lines.first.chomp
|
85
|
+
raise RuntimeError.new('gpg failed') unless $?
|
86
|
+
result =~ /^(pub|sec)\s+\d+D\/([0-9a-fA-F]{8}).+<(.+)>/ or raise RuntimeError.new('Invalid output')
|
87
|
+
key_id = $2
|
88
|
+
recipient = $3
|
89
|
+
recipient
|
90
|
+
end
|
91
|
+
|
92
|
+
def self.with_temporary_encrypt_keyring(public_key_file_name)
|
93
|
+
with_temporary_keyring_file do |keyring_file_name|
|
94
|
+
run_gpg(
|
95
|
+
'--keyring', keyring_file_name,
|
96
|
+
'--import', public_key_file_name
|
97
|
+
)
|
98
|
+
yield keyring_file_name
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
def self.with_temporary_decrypt_keyrings(public_key_file_name, private_key_file_name)
|
103
|
+
with_temporary_keyring_file do |keyring_file_name|
|
104
|
+
with_temporary_keyring_file do |secret_keyring_file_name|
|
105
|
+
run_gpg(
|
106
|
+
'--keyring', keyring_file_name,
|
107
|
+
'--secret-keyring', secret_keyring_file_name,
|
108
|
+
'--import', private_key_file_name
|
109
|
+
)
|
110
|
+
yield keyring_file_name, secret_keyring_file_name
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
def self.with_temporary_keyring_file
|
116
|
+
keyring_file = Tempfile.new('gpg-key-ring')
|
117
|
+
begin
|
118
|
+
keyring_file_name = keyring_file.path
|
119
|
+
keyring_file.close
|
120
|
+
keyring_file.unlink
|
121
|
+
yield keyring_file_name
|
122
|
+
ensure
|
123
|
+
File.unlink(keyring_file_name) if File.exist?(keyring_file_name)
|
124
|
+
backup_keyring_file_name = "#{keyring_file_name}~"
|
125
|
+
File.unlink(backup_keyring_file_name) if File.exist?(backup_keyring_file_name)
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rgpg
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-07-
|
12
|
+
date: 2013-07-28 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: Simple Ruby wrapper around "gpg" command for file encryption
|
15
15
|
email: rcook@rcook.org
|
@@ -18,10 +18,14 @@ executables:
|
|
18
18
|
extensions: []
|
19
19
|
extra_rdoc_files: []
|
20
20
|
files:
|
21
|
-
-
|
21
|
+
- MIT-LICENSE.txt
|
22
|
+
- lib/rgpg.rb
|
23
|
+
- lib/rgpg/gem_info.rb
|
24
|
+
- lib/rgpg/gpg_helper.rb
|
22
25
|
- bin/rgpg
|
23
26
|
homepage: https://github.com/rcook/rgpg/
|
24
|
-
licenses:
|
27
|
+
licenses:
|
28
|
+
- MIT
|
25
29
|
post_install_message:
|
26
30
|
rdoc_options: []
|
27
31
|
require_paths:
|
data/lib/gpg_helper.rb
DELETED
@@ -1,128 +0,0 @@
|
|
1
|
-
require 'tempfile'
|
2
|
-
|
3
|
-
module GpgHelper
|
4
|
-
def self.generate_key_pair(key_base_name, recipient, real_name)
|
5
|
-
public_key_file_name = "#{key_base_name}.pub"
|
6
|
-
private_key_file_name = "#{key_base_name}.sec"
|
7
|
-
script = generate_key_script(public_key_file_name, private_key_file_name, recipient, real_name)
|
8
|
-
script_file = Tempfile.new('gpg-script')
|
9
|
-
begin
|
10
|
-
script_file.write(script)
|
11
|
-
script_file.close
|
12
|
-
result = system("gpg --batch --gen-key #{script_file.path}")
|
13
|
-
raise RuntimeError.new('gpg failed') unless result
|
14
|
-
ensure
|
15
|
-
script_file.close
|
16
|
-
script_file.unlink
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
def self.encrypt_file(public_key_file_name, input_file_name, output_file_name)
|
21
|
-
recipient = get_recipient(public_key_file_name)
|
22
|
-
with_temporary_encrypt_keyring(public_key_file_name) do |keyring_file_name|
|
23
|
-
run_gpg(
|
24
|
-
'--keyring', keyring_file_name,
|
25
|
-
'--output', output_file_name,
|
26
|
-
'--encrypt',
|
27
|
-
'--recipient', recipient,
|
28
|
-
'--yes',
|
29
|
-
'--trust-model', 'always',
|
30
|
-
input_file_name
|
31
|
-
)
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
def self.decrypt_file(public_key_file_name, private_key_file_name, input_file_name, output_file_name)
|
36
|
-
recipient = get_recipient(private_key_file_name)
|
37
|
-
with_temporary_decrypt_keyrings(public_key_file_name, private_key_file_name) do |keyring_file_name, secret_keyring_file_name|
|
38
|
-
run_gpg(
|
39
|
-
'--keyring', keyring_file_name,
|
40
|
-
'--secret-keyring', secret_keyring_file_name,
|
41
|
-
'--output', output_file_name,
|
42
|
-
'--decrypt',
|
43
|
-
'--yes',
|
44
|
-
'--trust-model', 'always',
|
45
|
-
input_file_name
|
46
|
-
)
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
50
|
-
private
|
51
|
-
|
52
|
-
def self.run_gpg(*args)
|
53
|
-
fragments = [
|
54
|
-
'gpg',
|
55
|
-
'--no-default-keyring'
|
56
|
-
] + args
|
57
|
-
command_line = fragments.join(' ')
|
58
|
-
puts command_line
|
59
|
-
result = system(command_line)
|
60
|
-
raise RuntimeError.new('gpg failed') unless result
|
61
|
-
end
|
62
|
-
|
63
|
-
def self.generate_key_script(public_key_file_name, private_key_file_name, recipient, real_name)
|
64
|
-
<<-EOS
|
65
|
-
%echo Generating a standard key
|
66
|
-
Key-Type: DSA
|
67
|
-
Key-Length: 1024
|
68
|
-
Subkey-Type: ELG-E
|
69
|
-
Subkey-Length: 1024
|
70
|
-
Name-Real: #{real_name}
|
71
|
-
Name-Comment: Key automatically generated by rgpg
|
72
|
-
Name-Email: #{recipient}
|
73
|
-
Expire-Date: 0
|
74
|
-
%pubring #{public_key_file_name}
|
75
|
-
%secring #{private_key_file_name}
|
76
|
-
# Do a commit here, so that we can later print "done" :-)
|
77
|
-
%commit
|
78
|
-
%echo done
|
79
|
-
EOS
|
80
|
-
end
|
81
|
-
|
82
|
-
def self.get_recipient(key_file_name)
|
83
|
-
result = `gpg #{key_file_name}`.lines.first.chomp
|
84
|
-
raise RuntimeError.new('gpg failed') unless $?
|
85
|
-
result =~ /^(pub|sec)\s+\d+D\/([0-9a-fA-F]{8}).+<(.+)>/ or raise RuntimeError.new('Invalid output')
|
86
|
-
key_id = $2
|
87
|
-
recipient = $3
|
88
|
-
recipient
|
89
|
-
end
|
90
|
-
|
91
|
-
def self.with_temporary_encrypt_keyring(public_key_file_name)
|
92
|
-
with_temporary_keyring_file do |keyring_file_name|
|
93
|
-
run_gpg(
|
94
|
-
'--keyring', keyring_file_name,
|
95
|
-
'--import', public_key_file_name
|
96
|
-
)
|
97
|
-
yield keyring_file_name
|
98
|
-
end
|
99
|
-
end
|
100
|
-
|
101
|
-
def self.with_temporary_decrypt_keyrings(public_key_file_name, private_key_file_name)
|
102
|
-
with_temporary_keyring_file do |keyring_file_name|
|
103
|
-
with_temporary_keyring_file do |secret_keyring_file_name|
|
104
|
-
run_gpg(
|
105
|
-
'--keyring', keyring_file_name,
|
106
|
-
'--secret-keyring', secret_keyring_file_name,
|
107
|
-
'--import', private_key_file_name
|
108
|
-
)
|
109
|
-
yield keyring_file_name, secret_keyring_file_name
|
110
|
-
end
|
111
|
-
end
|
112
|
-
end
|
113
|
-
|
114
|
-
def self.with_temporary_keyring_file
|
115
|
-
keyring_file = Tempfile.new('gpg-key-ring')
|
116
|
-
begin
|
117
|
-
keyring_file_name = keyring_file.path
|
118
|
-
keyring_file.close
|
119
|
-
keyring_file.unlink
|
120
|
-
yield keyring_file_name
|
121
|
-
ensure
|
122
|
-
File.unlink(keyring_file_name) if File.exist?(keyring_file_name)
|
123
|
-
backup_keyring_file_name = "#{keyring_file_name}~"
|
124
|
-
File.unlink(backup_keyring_file_name) if File.exist?(backup_keyring_file_name)
|
125
|
-
end
|
126
|
-
end
|
127
|
-
end
|
128
|
-
|