jruby-pgp 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env jruby
2
+
3
+ if $LOAD_PATH.grep('jruby-pgp').none?
4
+ lib_dir = File.expand_path('../../lib', __FILE__)
5
+ $LOAD_PATH.unshift lib_dir
6
+ end
7
+
8
+ require 'pgp/cli/runner'
9
+ require 'pathname'
10
+ require 'optparse'
11
+
12
+ PGP::CLI::Runner.go!(ARGV)
@@ -18,6 +18,8 @@ Gem::Specification.new do |gem|
18
18
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
19
19
  gem.require_paths = ['lib']
20
20
 
21
+ gem.add_dependency 'jruby-openssl'
22
+
21
23
  gem.add_development_dependency 'rake'
22
24
  gem.add_development_dependency 'rspec'
23
25
  gem.add_development_dependency 'rake-compiler'
data/lib/pgp.rb CHANGED
@@ -10,6 +10,7 @@ require 'pgp/private_key'
10
10
  module PGP
11
11
  autoload :VERSION, 'pgp/version'
12
12
  autoload :RubyDecryptor, 'pgp/ruby_decryptor'
13
+ autoload :CLI, 'pgp/cli'
13
14
 
14
15
  BC_Provider_Code = "BC"
15
16
 
@@ -0,0 +1,160 @@
1
+ require 'pgp'
2
+
3
+ module PGP
4
+ # Currently, this is pretty quick-and-dirty. I should expand options into accessor methods, I know.
5
+ class CLI
6
+ autoload :Runner, 'pgp/cli/runner'
7
+
8
+ attr_accessor :options, :opt_parser
9
+
10
+ Encrypted_Extension_Regexp = /\.(pgp|gpg|asc)$/i
11
+
12
+ def self.ensure_file_exists!(file)
13
+ raise "The file #{file.inspect} does not appear to exist!" unless File.exist?(file)
14
+ end
15
+
16
+ def self.ensure_dir_exists!(dir)
17
+ raise "The directory #{dir.inspect} does not appear to exist!" unless File.directory?(dir)
18
+ end
19
+
20
+ def initialize
21
+ self.options = {
22
+ :public_keys => [],
23
+ :private_keys => [],
24
+ :input_files => [],
25
+ :output_files => [],
26
+ :outdir => Pathname(Dir.pwd),
27
+ :same_dir => false,
28
+ :action => nil,
29
+ :signature => false, # We do not currently support signing or verifying signatures
30
+ }
31
+ end
32
+
33
+ def [](arg)
34
+ options[arg]
35
+ end
36
+
37
+ def []=(arg, val)
38
+ options[arg] = val
39
+ end
40
+
41
+ def validate_options!
42
+ raise "Input file(s) must be specified!" if input_files.none?
43
+
44
+ case action
45
+ when :encrypt then validate_encrypt_options!
46
+ when :decrypt then validate_decrypt_options!
47
+ else
48
+ raise "Valid actions are encrypt or decrypt. Action specified: #{options[:action]}"
49
+ end
50
+
51
+ rescue RuntimeError => e
52
+ $stderr.puts opt_parser
53
+ raise e
54
+ end
55
+
56
+ def run!
57
+ validate_options!
58
+
59
+ case options[:action]
60
+ when :encrypt then encrypt!
61
+ when :decrypt then decrypt!
62
+ end
63
+ end
64
+
65
+ def encrypt!
66
+ cryptor = Encryptor.new
67
+
68
+ public_keys.each {|pub| cryptor.add_keys_from_file(pub) }
69
+
70
+ input_files.each_with_index do |infile, idx|
71
+ outfile = output_files[idx]
72
+ output = cryptor.encrypt_file(infile)
73
+
74
+ File.open(outfile, "w") do |fi|
75
+ fi.write output
76
+ end
77
+ end
78
+ end
79
+
80
+ def decrypt!
81
+ cryptor = Decryptor.new
82
+
83
+ private_keys.each {|priv| cryptor.add_keys_from_file(priv) }
84
+
85
+ input_files.each_with_index do |infile, idx|
86
+ outfile = output_files[idx]
87
+ output = cryptor.decrypt_file(infile)
88
+
89
+ File.open(outfile, "w") do |fi|
90
+ fi.write output
91
+ end
92
+ end
93
+ end
94
+
95
+ def action
96
+ options[:action] ||= begin
97
+ if input_files.grep(Encrypted_Extension_Regexp).any?
98
+ :decrypt
99
+ else
100
+ :encrypt
101
+ end
102
+ end
103
+ end
104
+
105
+ def public_keys
106
+ options[:public_keys]
107
+ end
108
+
109
+ def private_keys
110
+ options[:private_keys]
111
+ end
112
+
113
+ def input_files
114
+ options[:input_files]
115
+ end
116
+
117
+ def output_files
118
+ options[:output_files]
119
+ end
120
+
121
+ def outdir
122
+ options[:outdir]
123
+ end
124
+
125
+ def same_dir?
126
+ options[:same_dir]
127
+ end
128
+
129
+ protected
130
+ def set_outfile_dir(file)
131
+ return file if same_dir?
132
+ outdir + File.basename(file)
133
+ end
134
+
135
+ def validate_encrypt_options!
136
+ raise "Public Keys are required for encryption" if options[:public_keys].none?
137
+ raise "Private Keys are required for signing files" if options[:signature] and options[:private_keys].none?
138
+
139
+ options[:input_files].each_with_index do |infile, idx|
140
+ next if options[:output_files][idx]
141
+ options[:output_files][idx] = set_outfile_dir("#{infile}.gpg")
142
+ end
143
+ end
144
+
145
+ def validate_decrypt_options!
146
+ raise "Private Keys are required for decryption" if options[:private_keys].none?
147
+ raise "Public Keys are required for verifying signatures" if options[:signature] and options[:public_keys].none?
148
+
149
+ options[:input_files].each_with_index do |infile, idx|
150
+ next if options[:output_files][idx]
151
+
152
+ outfile = infile.gsub(Encrypted_Extension_Regexp, '')
153
+ outfile = "#{infile} - decrypted" if outfile == infile
154
+
155
+ options[:output_files][idx] = set_outfile_dir(outfile)
156
+ end
157
+ end
158
+
159
+ end
160
+ end
@@ -0,0 +1,78 @@
1
+ require 'pgp/cli'
2
+
3
+ module PGP
4
+ # Currently, this is pretty quick-and-dirty. I should expand options into accessor methods, I know.
5
+ class CLI
6
+ module Runner
7
+
8
+ def self.go!(args)
9
+ cli = parse_args!(args)
10
+ cli.run!
11
+ end
12
+
13
+ def self.parse_args!(args)
14
+ cli = PGP::CLI.new
15
+
16
+ cli.opt_parser = OptionParser.new do |opts|
17
+ opts.banner = "Usage: jrpgp [options] file [file2] [file3] [...]"
18
+
19
+ opts.on("-e", "--encrypt", "Perform Encryption") do
20
+ cli[:action] = :encrypt
21
+ end
22
+
23
+ opts.on("-d", "--decrypt", "Perform Decryption") do
24
+ cli[:action] = :decrypt
25
+ end
26
+
27
+ opts.on("-p", "--pub-key [file]", String, "The file containing Public Key(s) to encrypt to") do |fi|
28
+ PGP::CLI.ensure_file_exists!(fi)
29
+ cli[:public_keys] << fi
30
+ end
31
+
32
+ opts.on("-P", "--priv-key [file]", String, "The file containing Private Key(s) to use for decryption / signing") do |fi|
33
+ PGP::CLI.ensure_file_exists!(fi)
34
+ cli[:private_keys] << fi
35
+ end
36
+
37
+ opts.on("-i", "--in [file]", String, "The file to encrypt/decrypt") do |fi|
38
+ PGP::CLI.ensure_file_exists!(fi)
39
+ cli[:input_files] << fi
40
+ end
41
+
42
+ opts.on("-o", "--out [file]", String, "The file to output") do |fi|
43
+ cli[:output_files] << fi
44
+ end
45
+
46
+ opts.on("-O", "--out-dir [dir]", String, "The directory where output files should be written") do |dir|
47
+ PGP::CLI.ensure_dir_exists!(dir)
48
+ cli[:outdir] = Pathname(dir).expand_path
49
+ end
50
+
51
+ opts.separator ""
52
+
53
+ opts.on_tail("-h", "--help", "Show this message") do
54
+ puts opts
55
+ exit
56
+ end
57
+
58
+ # Another typical switch to print the version.
59
+ opts.on_tail("--version", "Show version") do
60
+ puts PGP::VERSION.join('.')
61
+ exit
62
+ end
63
+ end
64
+
65
+ cli.opt_parser.parse!(args)
66
+
67
+ args.each do |file|
68
+ PGP::CLI.ensure_file_exists!(file)
69
+ cli[:input_files] << file
70
+ end
71
+
72
+ cli
73
+ end
74
+
75
+ end
76
+ end
77
+ end
78
+
@@ -16,6 +16,10 @@ module PGP
16
16
  String.from_java_bytes(decrypted_data)
17
17
  end
18
18
 
19
+ def decrypt_file(file_path)
20
+ decrypt File.read(file_path)
21
+ end
22
+
19
23
  protected
20
24
  def keyring_from_file(filename)
21
25
  file = File.open(filename)
@@ -1,3 +1,3 @@
1
1
  module PGP
2
- VERSION = '0.1.2'
2
+ VERSION = '0.2.0'
3
3
  end
@@ -0,0 +1,41 @@
1
+ require 'spec_helper'
2
+ require 'fileutils'
3
+
4
+ describe PGP::CLI do
5
+ let(:private_key_path) { Fixtures_Path.join('private_key.asc').to_s }
6
+ let(:public_key_path) { Fixtures_Path.join('public_key.asc').to_s }
7
+
8
+ let(:decrypted_file) { Fixtures_Path.join('unencrypted_file.txt').to_s }
9
+ let(:encrypted_file) { Fixtures_Path.join('unencrypted_file.txt.asc').to_s }
10
+
11
+ after {
12
+ # These files are created in our current working directory
13
+ FileUtils.rm_rf('unencrypted_file.txt.gpg')
14
+ FileUtils.rm_rf('unencrypted_file.txt')
15
+ }
16
+
17
+
18
+ describe 'Encrypting' do
19
+ it "should encrypt a given file to the given public key" do
20
+ PGP::CLI::Runner.go!([decrypted_file, "-p", "spec/support/fixtures/public_key.asc"])
21
+
22
+ File.exist?('unencrypted_file.txt.gpg').should be_true
23
+ end
24
+ end
25
+
26
+ describe 'Decrypting' do
27
+ it "should decrypt a given file to the given private key" do
28
+ PGP::CLI::Runner.go!([encrypted_file, "-P", "spec/support/fixtures/private_key.asc", "-d"])
29
+
30
+ File.exist?('unencrypted_file.txt').should be_true
31
+ end
32
+ end
33
+
34
+ describe 'Public Keys'
35
+
36
+ describe 'Private Keys'
37
+
38
+ describe 'Input Files'
39
+
40
+ describe 'Output Files'
41
+ end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: jruby-pgp
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.1.2
5
+ version: 0.2.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Scott Gonyea
@@ -10,10 +10,10 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2012-11-22 00:00:00 Z
13
+ date: 2013-01-04 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
- name: rake
16
+ name: jruby-openssl
17
17
  prerelease: false
18
18
  requirement: &id001 !ruby/object:Gem::Requirement
19
19
  none: false
@@ -21,10 +21,10 @@ dependencies:
21
21
  - - ">="
22
22
  - !ruby/object:Gem::Version
23
23
  version: "0"
24
- type: :development
24
+ type: :runtime
25
25
  version_requirements: *id001
26
26
  - !ruby/object:Gem::Dependency
27
- name: rspec
27
+ name: rake
28
28
  prerelease: false
29
29
  requirement: &id002 !ruby/object:Gem::Requirement
30
30
  none: false
@@ -35,7 +35,7 @@ dependencies:
35
35
  type: :development
36
36
  version_requirements: *id002
37
37
  - !ruby/object:Gem::Dependency
38
- name: rake-compiler
38
+ name: rspec
39
39
  prerelease: false
40
40
  requirement: &id003 !ruby/object:Gem::Requirement
41
41
  none: false
@@ -45,11 +45,22 @@ dependencies:
45
45
  version: "0"
46
46
  type: :development
47
47
  version_requirements: *id003
48
+ - !ruby/object:Gem::Dependency
49
+ name: rake-compiler
50
+ prerelease: false
51
+ requirement: &id004 !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: "0"
57
+ type: :development
58
+ version_requirements: *id004
48
59
  description: PGP for JRuby
49
60
  email:
50
61
  - me@sgonyea.com
51
- executables: []
52
-
62
+ executables:
63
+ - jrpgp
53
64
  extensions: []
54
65
 
55
66
  extra_rdoc_files: []
@@ -61,11 +72,14 @@ files:
61
72
  - LICENSE.txt
62
73
  - README.md
63
74
  - Rakefile
75
+ - bin/jrpgp
64
76
  - ext/org/sgonyea/pgp/Decryptor.java
65
77
  - ext/org/sgonyea/pgp/Encryptor.java
66
78
  - jruby-pgp.gemspec
67
79
  - lib/jruby-pgp.rb
68
80
  - lib/pgp.rb
81
+ - lib/pgp/cli.rb
82
+ - lib/pgp/cli/runner.rb
69
83
  - lib/pgp/decryptor.rb
70
84
  - lib/pgp/encryptor.rb
71
85
  - lib/pgp/jars/bcpg-jdk15on-147.jar
@@ -73,6 +87,7 @@ files:
73
87
  - lib/pgp/private_key.rb
74
88
  - lib/pgp/ruby_decryptor.rb
75
89
  - lib/pgp/version.rb
90
+ - spec/lib/pgp/cli_spec.rb
76
91
  - spec/lib/pgp/decryptor_spec.rb
77
92
  - spec/lib/pgp/encryptor_spec.rb
78
93
  - spec/spec_helper.rb
@@ -104,11 +119,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
104
119
  requirements: []
105
120
 
106
121
  rubyforge_project:
107
- rubygems_version: 1.8.24
122
+ rubygems_version: 1.8.9
108
123
  signing_key:
109
124
  specification_version: 3
110
125
  summary: This is a Java+JRuby wrapper around the Bouncy Castle PGP APIs
111
126
  test_files:
127
+ - spec/lib/pgp/cli_spec.rb
112
128
  - spec/lib/pgp/decryptor_spec.rb
113
129
  - spec/lib/pgp/encryptor_spec.rb
114
130
  - spec/spec_helper.rb