fernet-cli 0.1 → 0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5bc1ba7e595b73d8bf6e87bbe0b6cfa458da9822
4
- data.tar.gz: eeb6d5e25640ddcbe6a393a13afac0cc7101ced4
3
+ metadata.gz: 5a654c836525e88646cbb7142cafaa905aab09a7
4
+ data.tar.gz: e2d9ab9a0450fb6e4127289b34836ea4cad8ab2f
5
5
  SHA512:
6
- metadata.gz: 6f9ab85bdca745f3a57119303e2341287eb534bf058e37820a402412b3a831babb846c9a4be18d9d9c135c79acadcf7dada2f737d660191ef5102b411f34cd9e
7
- data.tar.gz: 53c3d8341c19c1ffb707ef6f7c27e915ad3e9223452be71257f523bc7330f92f92d5ca2b9a6cfbc03ce724d4f1deefc2254a65f79a2dee8c8b2329937ad2e097
6
+ metadata.gz: 7b36d9e0360d9158ac682d4abf8e87d0a19c3b7bc742a50d78867b73fd8fc1ec876835477fce8a22e5b0d2f1176a79320d14a685ec5f09a73cee0f95017056b5
7
+ data.tar.gz: f011d30cb64e6898d97c2dd994c30de6e93b1de8e0a88559e29d7f40bc259d11a2f39087953149bc228e5b050f5d92078b2cf001327e04e26f3293f5838bd5da
data/README.md ADDED
@@ -0,0 +1,25 @@
1
+ fernet-cli
2
+ ==========
3
+
4
+ Now, the power of [Fernet](https://github.com/fernet/spec) is
5
+ available from the shell!
6
+
7
+ ```
8
+ tmaher@og-kush:~$ fernet-encrypt --help
9
+ Usage: fernet-encrypt [-p | -k <keyfile>] -i <infile> -o <outfile>
10
+ -p, --prompt Prompt for keys
11
+ -k, --keyfile KEYFILE
12
+ -i, --infile INPUTFILE
13
+ -o, --outfile OUTPUTFILE
14
+ ```
15
+
16
+ And there's a corresponding `fernet-decrypt` too. The key should be a
17
+ base64-encoded blob of 256-bits. If you'd rather not write it out to
18
+ a file or get promted for it, you can save it to shell environment
19
+ variable `FERNET_CLI_KEY`.
20
+
21
+ For `fernet-encrypt`, the infile is plaintext and the outfile is
22
+ ciphertext. For `fernet-decrypt`, the infile is ciphertext and the
23
+ outfile is plaintext.
24
+
25
+ For more information, see https://github.com/fernet/spec
data/bin/fernet-decrypt CHANGED
@@ -1,60 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require 'fernet'
4
- require 'optparse'
5
- require 'highline/import'
3
+ require 'fernet/cli'
6
4
 
7
- options = {}
8
- OptionParser.new do |opts|
9
- opts.banner = "Usage: fernet-decrypt [-p | -k <keyfile>] -i <ciphertext> -o <plaintext>"
10
-
11
- opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
12
- options[:verbose] = v
13
- end
14
- opts.on("-p", "--prompt", "Prompt for keys") do |prompt|
15
- options[:prompt] = prompt
16
- end
17
- opts.on("-k", "--keyfile KEYFILE") do |keyfile|
18
- options[:keyfile] = keyfile
19
- end
20
- opts.on("-i", "--infile CIPHERFILE") do |cipherfile|
21
- options[:cipherfile] = cipherfile
22
- end
23
- opts.on("-o", "--outfile PLAINFILE") do |plainfile|
24
- options[:plainfile] = plainfile
25
- end
26
- opts.on("--enforce-ttl") do |ttl|
27
- options[:ttl] = true
28
- end
29
- end.parse!
30
-
31
- if ENV["FERNET_CLI_KEY"].nil?
32
- if options[:prompt]
33
- KEY = ask("Enter Key: ") {|q| q.echo = false}
34
- elsif options[:keyfile]
35
- KEY = File.read(options[:keyfile]).chomp
36
- end
37
- else
38
- KEY=ENV["FERNET_CLI_KEY"]
39
- end
40
-
41
- if KEY.nil?
42
- puts "i have no key"
43
- exit(1)
44
- end
45
-
46
- if options[:cipherfile].nil? or (! File.readable?(options[:cipherfile]))
47
- puts "can't read ciphertext file"
48
- exit(1)
49
- end
50
-
51
- plaintext = Fernet.verifier(KEY.chomp, File.read(options[:cipherfile]),
52
- enforce_ttl: options[:ttl])
53
- if ! plaintext.valid?
54
- puts "ciphertext corrupt"
55
- exit(1)
56
- end
57
-
58
- File.open(options[:plainfile], "w+") do |f|
59
- f.write(plaintext.message)
60
- end
5
+ Fernet::CLI.new().decrypt
data/bin/fernet-encrypt CHANGED
@@ -1,50 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require 'fernet'
4
- require 'optparse'
5
- require 'highline/import'
3
+ require 'fernet/cli'
6
4
 
7
- options = {}
8
- OptionParser.new do |opts|
9
- opts.banner = "Usage: fernet-encrypt [-p | -k <keyfile>] -i <plaintext> -o <ciphertext>"
10
-
11
- opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
12
- options[:verbose] = v
13
- end
14
- opts.on("-p", "--prompt", "Prompt for keys") do |prompt|
15
- options[:prompt] = prompt
16
- end
17
- opts.on("-k", "--keyfile KEYFILE") do |keyfile|
18
- options[:keyfile] = keyfile
19
- end
20
- opts.on("-i", "--infile PLAINFILE") do |plainfile|
21
- options[:plainfile] = plainfile
22
- end
23
- opts.on("-o", "--outfile CIPHERFILE") do |cipherfile|
24
- options[:cipherfile] = cipherfile
25
- end
26
- end.parse!
27
-
28
- if ENV["FERNET_CLI_KEY"].nil?
29
- if options[:prompt]
30
- KEY = ask("Enter Key: ") {|q| q.echo = false}
31
- elsif options[:keyfile]
32
- KEY = File.read(options[:keyfile]).chomp
33
- end
34
- else
35
- KEY=ENV["FERNET_CLI_KEY"]
36
- end
37
-
38
- if KEY.nil?
39
- puts "i have no key"
40
- exit(1)
41
- end
42
-
43
- if options[:plainfile].nil? or (! File.readable?(options[:plainfile]))
44
- puts "can't read plaintext file"
45
- exit(1)
46
- end
47
-
48
- File.open(options[:cipherfile], "w+") do |f|
49
- f.write(Fernet.generate(KEY.chomp, File.read(options[:plainfile])))
50
- end
5
+ Fernet::CLI.new().encrypt
data/fernet-cli.gemspec CHANGED
@@ -17,5 +17,6 @@ Gem::Specification.new do |gem|
17
17
  gem.license = "Apache-2.0"
18
18
 
19
19
  gem.add_runtime_dependency "fernet", "~> 2.0"
20
+ gem.add_runtime_dependency "highline", "~> 1.6"
20
21
  gem.add_development_dependency "rspec", "~> 2.14"
21
22
  end
@@ -1,5 +1,5 @@
1
1
  module Fernet
2
2
  module CLI
3
- VERSION = "0.1"
3
+ VERSION = "0.3"
4
4
  end
5
5
  end
data/lib/fernet/cli.rb ADDED
@@ -0,0 +1,92 @@
1
+ require 'fernet'
2
+ require 'optparse'
3
+ require 'highline/import'
4
+
5
+ module Fernet
6
+ class CLI
7
+ def initialize
8
+ @options = {}
9
+ @op_obj = parse_args
10
+ @key = get_key
11
+ (@in_fd, @out_fd) = set_file_descriptors
12
+ end
13
+
14
+ def abort msg="unspecified error"
15
+ $stderr.puts "FAIL: #{msg}", @op_obj.banner, @op_obj.summarize
16
+ exit(1)
17
+ end
18
+
19
+ def parse_args
20
+ op = OptionParser.new do |opts|
21
+ opts.banner = "Usage: #{$0} [-p | -k <keyfile>] -i <infile> -o <outfile>"
22
+
23
+ opts.on("-p", "--prompt", "Prompt for keys") do |prompt|
24
+ @options[:prompt] = prompt
25
+ end
26
+ opts.on("-k", "--keyfile KEYFILE") do |keyfile|
27
+ @options[:keyfile] = keyfile
28
+ end
29
+ opts.on("-i", "--infile INPUTFILE") do |infile|
30
+ @options[:infile] = infile
31
+ end
32
+ opts.on("-o", "--outfile OUTPUTFILE") do |outfile|
33
+ @options[:outfile] = outfile
34
+ end
35
+ end
36
+ op.parse!
37
+ op
38
+ end
39
+
40
+ def get_key
41
+ if ENV["FERNET_CLI_KEY"].nil?
42
+ if @options[:prompt]
43
+ key = ask("Enter Key: ") {|q| q.echo = false}
44
+ elsif @options[:keyfile]
45
+ key = File.read(@options[:keyfile])
46
+ end
47
+ else
48
+ key=ENV["FERNET_CLI_KEY"]
49
+ end
50
+
51
+ abort("please specify a key") if key.nil?
52
+
53
+ key.chomp
54
+ end
55
+
56
+ def set_file_descriptors
57
+ if @options[:infile].nil? or @options[:outfile].nil?
58
+ abort "must specify input & output files. Use '-' for stdin/stdout"
59
+ end
60
+
61
+ if @options[:infile] == "-"
62
+ in_fd = $stdin
63
+ else
64
+ abort("can't read input file") unless File.readable?(@options[:infile])
65
+ in_fd = File.open(@options[:infile])
66
+ end
67
+
68
+ if @options[:outfile] == "-"
69
+ out_fd = $stdout
70
+ else
71
+ out_fd = File.open(@options[:outfile], "w+")
72
+ end
73
+
74
+ [in_fd, out_fd]
75
+ end
76
+
77
+ def encrypt
78
+ @out_fd.write(Fernet.generate(@key, @in_fd.read))
79
+ @out_fd.close
80
+ end
81
+
82
+ def decrypt
83
+ plaintext = Fernet.verifier(@key, @in_fd.read,
84
+ enforce_ttl: false)
85
+ abort("ciphertext corrupt") unless plaintext.valid?
86
+
87
+ @out_fd.write(plaintext.message)
88
+ @out_fd.close
89
+ end
90
+
91
+ end
92
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fernet-cli
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.1'
4
+ version: '0.3'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tom Maher
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: highline
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.6'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.6'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: rspec
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -49,10 +63,12 @@ extra_rdoc_files: []
49
63
  files:
50
64
  - ".gitignore"
51
65
  - Gemfile
66
+ - README.md
52
67
  - Rakefile
53
68
  - bin/fernet-decrypt
54
69
  - bin/fernet-encrypt
55
70
  - fernet-cli.gemspec
71
+ - lib/fernet/cli.rb
56
72
  - lib/fernet/cli/version.rb
57
73
  - version
58
74
  homepage: https://github.com/tmaher/fernet-cli