rgpg 0.0.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/lib/gpg_helper.rb +128 -0
- metadata +45 -0
data/lib/gpg_helper.rb
ADDED
@@ -0,0 +1,128 @@
|
|
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
|
+
|
metadata
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rgpg
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Richard Cook
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-07-08 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Simple Ruby wrapper around "gpg" command for file encryption
|
15
|
+
email: rcook@rcook.org
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/gpg_helper.rb
|
21
|
+
homepage: https://github.com/rcook/rgpg/
|
22
|
+
licenses: []
|
23
|
+
post_install_message:
|
24
|
+
rdoc_options: []
|
25
|
+
require_paths:
|
26
|
+
- lib
|
27
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
34
|
+
none: false
|
35
|
+
requirements:
|
36
|
+
- - ! '>='
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
39
|
+
requirements: []
|
40
|
+
rubyforge_project:
|
41
|
+
rubygems_version: 1.8.23
|
42
|
+
signing_key:
|
43
|
+
specification_version: 3
|
44
|
+
summary: rgpg
|
45
|
+
test_files: []
|