cript 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.travis.yml CHANGED
@@ -3,5 +3,5 @@ rvm:
3
3
  - 1.9.3
4
4
  - 2.0.0
5
5
  - jruby-19mode
6
- - rbx-19mode
6
+ - rbx-2.1.1
7
7
  script: bundle exec rspec spec
data/README.md CHANGED
@@ -66,6 +66,9 @@ c2 = Cript::Simple.new({
66
66
  encrypted = c.encrypt("More secret stuff!")
67
67
  ```
68
68
 
69
+ You can use Cript::Simple from bash with the `cript` executable.
70
+ Type `cript --help` after gem installation for usage.
71
+
69
72
  ### Cript::Hidr
70
73
 
71
74
  The Hidr class allows you to obscure strings in other strings by facilitating a two-way conversion
@@ -83,6 +86,9 @@ After running this code, result will contain "zzzazazazzzzazzazzzazzzazaaaazaa".
83
86
  It comes with a few commonly used mappings already setup: ascii, unicode, orly.
84
87
  Call the class method of the same name to get these hidrs.
85
88
 
89
+ You can use Cript::Hidr from bash with the `hidr` executable.
90
+ Type `hidr --help` after gem installation for usage.
91
+
86
92
  ## Contributing
87
93
 
88
94
  1. Fork it
data/bin/cript ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ lib = File.expand_path('../../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) if File.directory?(lib) && !$LOAD_PATH.include?(lib)
5
+ require 'cript/cript_command'
6
+ Cript::CriptCommand.new(ARGV).run!
data/bin/hidr ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ lib = File.expand_path('../../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) if File.directory?(lib) && !$LOAD_PATH.include?(lib)
5
+ require 'cript/hidr_command'
6
+ Cript::HidrCommand.new(ARGV).run!
@@ -0,0 +1,125 @@
1
+ require 'cript'
2
+ require 'optparse'
3
+
4
+ module Cript
5
+ class CriptCommand
6
+
7
+ attr_reader :options
8
+
9
+ def initialize(argv)
10
+ @argv = argv.dup
11
+
12
+ @options = {
13
+ mode: nil,
14
+ infile: '-',
15
+ outfile: '-',
16
+ public: File.join(ENV['HOME'], '.ssh/id_rsa.pub'),
17
+ private: File.join(ENV['HOME'], '.ssh/id_rsa'),
18
+ force: false,
19
+ debug: false
20
+ }
21
+
22
+ parse!
23
+ end
24
+
25
+ def run!
26
+ cripter = build_cripter
27
+ data = read_data
28
+ get_writer do |writer|
29
+ if @options[:mode] == 'encrypt'
30
+ writer.print cripter.encrypt(data)
31
+ else
32
+ writer.print cripter.decrypt(data)
33
+ end
34
+ end
35
+ end
36
+
37
+ private
38
+
39
+ def parser
40
+ @parser ||= OptionParser.new do |opts|
41
+ opts.banner = "Usage: cript [options]"
42
+
43
+ opts.separator ""
44
+ opts.separator "Mode:"
45
+ opts.on("-e", "--encrypt", "encrypt mode") { @options[:mode] = 'encrypt' }
46
+ opts.on("-d", "--decrypt", "decrypt mode") { @options[:mode] = 'decrypt' }
47
+
48
+ opts.separator ""
49
+ opts.separator "Keys:"
50
+ opts.on("--private PRIVATE", "private key path (default: #{@options[:private]})") { |priv| @options[:private] = priv }
51
+ opts.on("--public PUBLIC", "public key path (default: #{@options[:public]})") { |pub| @options[:public] = pub }
52
+
53
+ opts.separator ""
54
+ opts.separator "Files:"
55
+ opts.on("-i", "--infile FILE", "infile (default: '#{@options[:infile]}')") { |file| @options[:infile] = file }
56
+ opts.on("-o", "--outfile FILE", "outfile (default: '#{@options[:outfile]}')") { |file| @options[:outfile] = file }
57
+ opts.on("-f", "--force", "force overwrite outfile (default: #{@options[:force]})") { @options[:force] = true }
58
+
59
+ opts.separator ""
60
+ opts.separator "Common options:"
61
+
62
+ opts.on_tail("-D", "--debug", "Set debugging on") { @options[:debug] = true }
63
+ opts.on_tail("-h", "--help", "Show this message") { puts opts; exit }
64
+ opts.on_tail('-v', '--version', "Show version") { puts Cript::VERSION; exit }
65
+ end
66
+ end
67
+
68
+ def parse!
69
+ parser.parse!(@argv)
70
+ end
71
+
72
+ # run methods
73
+
74
+ def build_cripter
75
+ if @options[:mode] == 'encrypt'
76
+ Cript::Simple.new(public_key_path: @options[:public])
77
+ elsif @options[:mode] == 'decrypt'
78
+ Cript::Simple.new(private_key_path: @options[:private])
79
+ else
80
+ STDERR.puts "Mode is required (encrypt or decrypt)"
81
+ exit 1
82
+ end
83
+ end
84
+
85
+ def read_data
86
+ if @options[:infile] == '-'
87
+ # requires ARGV clear
88
+ STDIN.read
89
+ elsif File.file?(@options[:infile])
90
+ File.read(@options[:infile])
91
+ else
92
+ STDERR.puts "Invalid infile: #{@options[:infile]}"
93
+ exit 1
94
+ end
95
+ end
96
+
97
+ def get_writer
98
+ begin
99
+ if @options[:outfile] == '-'
100
+ writer = STDOUT
101
+ elsif File.file?(@options[:outfile])
102
+ if @options[:force]
103
+ writer = File.open(@options[:outfile], 'wb')
104
+ else
105
+ STDERR.puts "Not overwriting file without force"
106
+ exit 1
107
+ end
108
+ else
109
+ writer = File.open(@options[:outfile], 'wb')
110
+ end
111
+
112
+ yield(writer)
113
+ rescue => e
114
+ STDERR.puts "Error writing data: #{e}"
115
+ exit 1
116
+ ensure
117
+ # clean-up
118
+ if writer && @options[:outfile] != '-'
119
+ writer.close
120
+ end
121
+ end
122
+ end
123
+
124
+ end
125
+ end
data/lib/cript/hidr.rb CHANGED
@@ -8,9 +8,11 @@ module Cript
8
8
  class Hidr
9
9
 
10
10
  CHARS = {
11
+ binary: ["0", "1"],
11
12
  ascii: ["\s","\t"],
12
13
  unicode: ["\u200B","\uFEFF"],
13
- orly: ["\u0CA0", "\u005F"]
14
+ orly: ["\u0CA0", "\u005F"],
15
+ niceday: ["\s", "\u263A"]
14
16
  }
15
17
 
16
18
  class << self
@@ -0,0 +1,136 @@
1
+ require 'cript'
2
+ require 'optparse'
3
+
4
+ module Cript
5
+ class HidrCommand
6
+
7
+ attr_reader :options
8
+
9
+ def initialize(argv)
10
+ @argv = argv.dup
11
+
12
+ @options = {
13
+ mode: nil,
14
+ infile: '-',
15
+ outfile: '-',
16
+ force: false,
17
+ b0: '0',
18
+ b1: '1',
19
+ debug: false
20
+ }
21
+
22
+ parse!
23
+ end
24
+
25
+ def run!
26
+ validate_mode
27
+ hidr = build_hidr
28
+ data = read_data
29
+ get_writer do |writer|
30
+ if @options[:mode] == 'hide'
31
+ writer.print hidr.hide(data)
32
+ else
33
+ writer.print hidr.show(data)
34
+ end
35
+ end
36
+ end
37
+
38
+ private
39
+
40
+ def parser
41
+ @parser ||= OptionParser.new do |opts|
42
+ opts.banner = "Usage: hidr [options]"
43
+
44
+ opts.separator ""
45
+ opts.separator "Mode:"
46
+ opts.on("-h", "--hide", "hide mode") { @options[:mode] = 'hide' }
47
+ opts.on("-s", "--show", "show mode") { @options[:mode] = 'show' }
48
+
49
+ opts.separator ""
50
+ opts.separator "Bits:"
51
+ opts.on("-0", "--zero CHAR", "zero char (default: '#{@options[:b0]}')") { |char| @options[:b0] = char }
52
+ opts.on("-1", "--one CHAR", "one char (default: '#{@options[:b1]}')") { |char| @options[:b1] = char }
53
+ opts.on("-b", "--builtin BUILTIN", "built-in binary set (overrides zero and one, options: #{Cript::Hidr::CHARS.keys.join(', ')})") { |builtin| @options[:builtin] = builtin.to_s.strip.to_sym }
54
+
55
+ opts.separator ""
56
+ opts.separator "Files:"
57
+ opts.on("-i", "--infile FILE", "infile (default: '#{@options[:infile]}')") { |file| @options[:infile] = file }
58
+ opts.on("-o", "--outfile FILE", "outfile (default: '#{@options[:outfile]}')") { |file| @options[:outfile] = file }
59
+ opts.on("-f", "--force", "force overwrite outfile (default: #{@options[:force]})") { @options[:force] = true }
60
+
61
+ opts.separator ""
62
+ opts.separator "Common options:"
63
+
64
+ opts.on_tail("-D", "--debug", "Set debugging on") { @options[:debug] = true }
65
+ opts.on_tail("-H", "--help", "Show this message") { puts opts; exit }
66
+ opts.on_tail('-v', '--version', "Show version") { puts Cript::VERSION; exit }
67
+ end
68
+ end
69
+
70
+ def parse!
71
+ parser.parse!(@argv)
72
+ end
73
+
74
+ # run methods
75
+
76
+ def validate_mode
77
+ unless %w{ hide show }.include?(@options[:mode])
78
+ STDERR.puts "Mode (show or hide) is required."
79
+ exit 1
80
+ end
81
+ end
82
+
83
+ def build_hidr
84
+ if @options[:builtin]
85
+ if Cript::Hidr::CHARS.keys.include?(@options[:builtin])
86
+ Cript::Hidr.send(@options[:builtin])
87
+ else
88
+ STDERR.puts "Invalid builtin: #{@options[:builtin]}"
89
+ exit 1
90
+ end
91
+ else
92
+ Cript::Hidr.new(b0: @options[:b0], b1: @options[:b1])
93
+ end
94
+ end
95
+
96
+ def read_data
97
+ if @options[:infile] == '-'
98
+ # requires ARGV clear
99
+ STDIN.read
100
+ elsif File.file?(@options[:infile])
101
+ File.read(@options[:infile])
102
+ else
103
+ STDERR.puts "Invalid infile: #{@options[:infile]}"
104
+ exit 1
105
+ end
106
+ end
107
+
108
+ def get_writer
109
+ begin
110
+ if @options[:outfile] == '-'
111
+ writer = STDOUT
112
+ elsif File.file?(@options[:outfile])
113
+ if @options[:force]
114
+ writer = File.open(@options[:outfile], 'wb')
115
+ else
116
+ STDERR.puts "Not overwriting file without force"
117
+ exit 1
118
+ end
119
+ else
120
+ writer = File.open(@options[:outfile], 'wb')
121
+ end
122
+
123
+ yield(writer)
124
+ rescue => e
125
+ STDERR.puts "Error writing data: #{e}"
126
+ exit 1
127
+ ensure
128
+ # clean-up
129
+ if writer && @options[:outfile] != '-'
130
+ writer.close
131
+ end
132
+ end
133
+ end
134
+
135
+ end
136
+ end
data/lib/cript/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Cript
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -0,0 +1,27 @@
1
+ require 'spec_helper'
2
+ require 'cript/cript_command'
3
+
4
+ describe Cript::CriptCommand do
5
+
6
+ context 'parse' do
7
+ it 'should parse encrypt options' do
8
+ c = Cript::CriptCommand.new("-e --public /tmp/fake_key.pub -i /tmp/in.txt -o /tmp/out.txt -f -D".split(" "))
9
+ c.options[:mode].should eql('encrypt')
10
+ c.options[:public].should eql('/tmp/fake_key.pub')
11
+ c.options[:infile].should eql('/tmp/in.txt')
12
+ c.options[:outfile].should eql('/tmp/out.txt')
13
+ c.options[:force].should be_true
14
+ c.options[:debug].should be_true
15
+ end
16
+
17
+ it 'should parse decrypt options' do
18
+ c = Cript::CriptCommand.new("-d --private /tmp/fake_key -i /tmp/in.txt -o /tmp/out.txt -f -D".split(" "))
19
+ c.options[:mode].should eql('decrypt')
20
+ c.options[:private].should eql('/tmp/fake_key')
21
+ c.options[:infile].should eql('/tmp/in.txt')
22
+ c.options[:outfile].should eql('/tmp/out.txt')
23
+ c.options[:force].should be_true
24
+ c.options[:debug].should be_true
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,37 @@
1
+ require 'spec_helper'
2
+ require 'cript/hidr_command'
3
+
4
+ describe Cript::HidrCommand do
5
+
6
+ context 'parse' do
7
+ it 'should parse hide options' do
8
+ c = Cript::HidrCommand.new("-h -0 a -1 b -i /tmp/in.txt -o /tmp/out.txt -f -D".split(" "))
9
+ c.options[:mode].should eql('hide')
10
+ c.options[:b0].should eql('a')
11
+ c.options[:b1].should eql('b')
12
+ c.options[:infile].should eql('/tmp/in.txt')
13
+ c.options[:outfile].should eql('/tmp/out.txt')
14
+ c.options[:force].should be_true
15
+ c.options[:debug].should be_true
16
+ end
17
+
18
+ it 'should parse show options' do
19
+ c = Cript::HidrCommand.new("-s -0 a -1 b -i /tmp/in.txt -o /tmp/out.txt -f -D".split(" "))
20
+ c.options[:mode].should eql('show')
21
+ c.options[:b0].should eql('a')
22
+ c.options[:b1].should eql('b')
23
+ c.options[:infile].should eql('/tmp/in.txt')
24
+ c.options[:outfile].should eql('/tmp/out.txt')
25
+ c.options[:force].should be_true
26
+ c.options[:debug].should be_true
27
+ end
28
+
29
+ it 'should recognize built-in hidrs' do
30
+ c = Cript::HidrCommand.new("-s -b orly -i /tmp/in.txt -o /tmp/out.txt -f -D".split(" "))
31
+ hidr = c.send(:build_hidr)
32
+ opts = hidr.instance_variable_get(:@o)
33
+ opts[:b0].should eql("\u0CA0")
34
+ opts[:b1].should eql("\u005F")
35
+ end
36
+ end
37
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cript
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
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-10-27 00:00:00.000000000 Z
12
+ date: 2013-11-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -62,7 +62,9 @@ dependencies:
62
62
  description: Simple Encryption Tools for Ruby
63
63
  email:
64
64
  - atongen@gmail.com
65
- executables: []
65
+ executables:
66
+ - cript
67
+ - hidr
66
68
  extensions: []
67
69
  extra_rdoc_files: []
68
70
  files:
@@ -72,16 +74,22 @@ files:
72
74
  - LICENSE.txt
73
75
  - README.md
74
76
  - Rakefile
77
+ - bin/cript
78
+ - bin/hidr
75
79
  - cript.gemspec
76
80
  - lib/cript.rb
81
+ - lib/cript/cript_command.rb
77
82
  - lib/cript/cripter.rb
78
83
  - lib/cript/ehash.rb
79
84
  - lib/cript/hidr.rb
85
+ - lib/cript/hidr_command.rb
80
86
  - lib/cript/naive.rb
81
87
  - lib/cript/simple.rb
82
88
  - lib/cript/store.rb
83
89
  - lib/cript/version.rb
90
+ - spec/cript_command_spec.rb
84
91
  - spec/ehash_spec.rb
92
+ - spec/hidr_command_spec.rb
85
93
  - spec/hidr_spec.rb
86
94
  - spec/naive_spec.rb
87
95
  - spec/simple_spec.rb
@@ -112,7 +120,9 @@ signing_key:
112
120
  specification_version: 3
113
121
  summary: Simple Encryption Tools for Ruby
114
122
  test_files:
123
+ - spec/cript_command_spec.rb
115
124
  - spec/ehash_spec.rb
125
+ - spec/hidr_command_spec.rb
116
126
  - spec/hidr_spec.rb
117
127
  - spec/naive_spec.rb
118
128
  - spec/simple_spec.rb