passw3rd 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/LICENSE ADDED
@@ -0,0 +1,10 @@
1
+ Open Source Initiative OSI - The MIT License (MIT):Licensing
2
+
3
+ The MIT License (MIT)
4
+ Copyright (c) 2011 Neil Matatall
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
7
+
8
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
9
+
10
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,27 @@
1
+ == DESCRIPTION:
2
+
3
+ * Programmatic Password Crytpo
4
+
5
+ == FEATURES/PROBLEMS:
6
+
7
+ * Simple mechanism to store encrypted values using keys on the system
8
+
9
+ == SYNOPSIS:
10
+
11
+ require 'password_service'
12
+
13
+ == REQUIREMENTS:
14
+
15
+ openssl
16
+ base64
17
+ optparse
18
+
19
+ == INSTALL:
20
+
21
+ * gem install programmatic_crypto
22
+
23
+ == DEVELOPERS:
24
+
25
+ == LICENSE:
26
+
27
+ MIT
data/bin/passw3rd ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby -w
2
+
3
+ require File.expand_path('../../lib/passw3rd', __FILE__)
4
+ # require File.expand_path('../../lib/passw3rd/password_client.rb', __FILE__)
5
+
@@ -0,0 +1,50 @@
1
+ require 'openssl'
2
+ module Passw3rd
3
+ class KeyLoader
4
+ KEY_FILE = "/.passw3rd-encryptionKey"
5
+ IV_FILE = "/.passw3rd-encryptionIV"
6
+
7
+
8
+ def self.load(path=nil)
9
+ if path.nil?
10
+ path = ENV['HOME']
11
+ end
12
+
13
+ begin
14
+ key = IO.readlines(File.expand_path(path + KEY_FILE))[0]
15
+ iv = IO.readlines(File.expand_path(path + IV_FILE))[0]
16
+ rescue Errno::ENOENT
17
+ puts "Couldn't read key/iv from #{path}. Have they been generated?\n"
18
+ raise $!
19
+ end
20
+
21
+ pair = KeyIVPair.new
22
+ pair.key = [key].pack("H*")
23
+ pair.iv = [iv].pack("H*")
24
+ pair
25
+ end
26
+
27
+ def self.create_key_iv_file(path=nil)
28
+ if path.nil?
29
+ path = ENV['HOME']
30
+ end
31
+
32
+ cipher = OpenSSL::Cipher::Cipher.new('aes-128-cbc')
33
+ iv = cipher.random_iv
34
+ key = cipher.random_key
35
+
36
+ begin
37
+ File.open(path + KEY_FILE, 'w') {|f| f.write(key.unpack("H*")) }
38
+ File.open(path + IV_FILE, 'w') {|f| f.write(iv.unpack("H*")) }
39
+ rescue
40
+ puts "Couldn't write key/IV to #{path}\n"
41
+ raise $!
42
+ end
43
+
44
+ end
45
+ end
46
+
47
+ class KeyIVPair
48
+ attr_accessor :key, :iv
49
+ end
50
+ end
@@ -0,0 +1,76 @@
1
+ require 'base64'
2
+ require 'openssl'
3
+ require 'optparse'
4
+
5
+ module Passw3rd
6
+ class PasswordClient
7
+ # def self.run argv = ARGV
8
+ password_file = nil
9
+ output_path = nil
10
+ gen_key_path = nil
11
+ key_path = nil
12
+
13
+ opts = OptionParser.new
14
+ opts.banner = 'Usage: password_client [options]'
15
+ opts.on('-d', '--decrypt PATH_TO_PASSWORD', 'Path to password file') do |opt|
16
+ password_file = opt
17
+ end
18
+ opts.on('-e', '--encrypt OUTPUT_PATH', 'Write the password to this location') do |opt|
19
+ output_path = opt
20
+ end
21
+ opts.on('-p', '--key-path KEY_PATH', 'Use the keys specificed in this directory for encryption or decryption') do |opt|
22
+ key_path = opt
23
+ if !File.directory?(File.expand_path(key_path))
24
+ raise "#{opt} must be a directory"
25
+ end
26
+ end
27
+ opts.on('-k', '--generate-key [PATH]', 'generate key/iv and store in PATH, defaults to the home directory') do |opt|
28
+ gen_key_path = opt
29
+ if gen_key_path.nil?
30
+ gen_key_path = ENV["HOME"]
31
+ end
32
+ if !File.directory?(File.expand_path(gen_key_path))
33
+ raise "#{opt} is not a directory"
34
+ end
35
+ end
36
+
37
+ opts.on_tail("-h", "--help", "Show this message") do
38
+ puts opts
39
+ exit
40
+ end
41
+
42
+ opts.parse(ARGV)
43
+
44
+ # generate key/IV
45
+ if gen_key_path
46
+ Passw3rd::KeyLoader.create_key_iv_file(gen_key_path)
47
+ puts "generated keys in #{gen_key_path}"
48
+ end
49
+
50
+ # default the key directory to the users home, can also be specified by -p [path]
51
+ if key_path.nil?
52
+ key_path = ENV["HOME"]
53
+ end
54
+
55
+ # decrypt password_file using the key/IV in key_path
56
+ if password_file
57
+ decrypted = Passw3rd::PasswordService.getPassword(password_file, key_path)
58
+
59
+ puts("The password is: #{decrypted}")
60
+ end
61
+
62
+ # encrypt password, store it in output path
63
+ if output_path
64
+ begin
65
+ system 'stty -echo'
66
+ print "Enter the password: "
67
+ password = STDIN.gets.chomp
68
+ ensure
69
+ system 'stty echo; echo ""'
70
+ end
71
+
72
+ Passw3rd::PasswordService.write_password_file(password, output_path, key_path)
73
+ puts "Wrote password to #{output_path}"
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,49 @@
1
+ module Passw3rd
2
+ class PasswordService
3
+ VERSION = '0.1.0'
4
+
5
+ def self.getPassword (password_file, key_path=nil)
6
+ encoded_password = IO.readlines(password_file).join
7
+ encrypted_password = Base64.decode64(encoded_password)
8
+ PasswordService.decrypt(encrypted_password, key_path)
9
+ end
10
+
11
+ def self.write_password_file password, output_path, key_path
12
+ enc_password = PasswordService.encrypt(password, key_path)
13
+ base64pw = Base64.encode64(enc_password)
14
+ File.open(output_path, 'w') { |f| f.write base64pw }
15
+ end
16
+
17
+ def self.encrypt(password, key_path = nil)
18
+ pair = KeyLoader.load(key_path)
19
+ cipher = OpenSSL::Cipher::Cipher.new('aes-128-cbc')
20
+ cipher.encrypt
21
+ cipher.key = pair.key
22
+ cipher.iv = pair.iv
23
+ begin
24
+ e = cipher.update(password)
25
+ e << cipher.final
26
+ rescue OpenSSL::Cipher::CipherError=>err
27
+ puts "Couldn't encrypt password."
28
+ raise err
29
+ end
30
+
31
+ end
32
+
33
+ def self.decrypt(password, key_path = nil)
34
+ pair = KeyLoader.load(key_path)
35
+ cipher = OpenSSL::Cipher::Cipher.new('aes-128-cbc')
36
+ cipher.decrypt
37
+ cipher.key = pair.key
38
+ cipher.iv = pair.iv
39
+ begin
40
+ d = cipher.update(password)
41
+ d << cipher.final
42
+ rescue OpenSSL::Cipher::CipherError => err
43
+ puts "Coudln't decrypt password. Are you using the right keys?"
44
+ raise err
45
+ end
46
+
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,3 @@
1
+ module Passw3rd
2
+ Version = VERSION = '0.0.1'
3
+ end
data/lib/passw3rd.rb ADDED
@@ -0,0 +1,7 @@
1
+ require 'base64'
2
+ require 'openssl'
3
+ require 'optparse'
4
+ require File.expand_path('../passw3rd/key_loader', __FILE__)
5
+ require File.expand_path('../passw3rd/password_service', __FILE__)
6
+ require File.expand_path('../passw3rd/password_client', __FILE__)
7
+
@@ -0,0 +1,28 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'tmpdir'
4
+ require 'SecureRandom'
5
+ require File.expand_path('../../lib/passw3rd.rb', __FILE__)
6
+
7
+ class PasswordServiceTest < Test::Unit::TestCase
8
+ def setup
9
+ random_num = SecureRandom.random_number(5000)
10
+ @random_string = SecureRandom.random_bytes(5000 + random_num)
11
+ end
12
+
13
+ def test_enc_dec
14
+ enc = ::Passw3rd::PasswordService.encrypt(@random_string)
15
+ dec = ::Passw3rd::PasswordService.decrypt(enc)
16
+
17
+ assert_equal(@random_string, dec)
18
+ end
19
+
20
+ def test_gen_key
21
+ ::Passw3rd::KeyLoader.create_key_iv_file(Dir.tmpdir)
22
+
23
+ enc = ::Passw3rd::PasswordService.encrypt(@random_string, Dir.tmpdir)
24
+ dec = ::Passw3rd::PasswordService.decrypt(enc, Dir.tmpdir)
25
+
26
+ assert_equal(@random_string, dec)
27
+ end
28
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: passw3rd
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Neil Matatall
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-09-23 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: openssl
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ hash: 3
29
+ segments:
30
+ - 0
31
+ version: "0"
32
+ type: :runtime
33
+ version_requirements: *id001
34
+ - !ruby/object:Gem::Dependency
35
+ name: optparse
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ hash: 3
43
+ segments:
44
+ - 0
45
+ version: "0"
46
+ type: :runtime
47
+ version_requirements: *id002
48
+ description: Generate a key/iv file, generate passwords, and store encrypted files in source control, keep the key/iv safe!
49
+ email:
50
+ - neil.matatall@gmail.com
51
+ executables:
52
+ - passw3rd
53
+ extensions: []
54
+
55
+ extra_rdoc_files: []
56
+
57
+ files:
58
+ - README
59
+ - LICENSE
60
+ - lib/passw3rd/key_loader.rb
61
+ - lib/passw3rd/password_client.rb
62
+ - lib/passw3rd/password_service.rb
63
+ - lib/passw3rd/version.rb
64
+ - lib/passw3rd.rb
65
+ - bin/passw3rd
66
+ - test/test_password_service.rb
67
+ homepage: https://github.com/oreoshake/passw3rd
68
+ licenses: []
69
+
70
+ post_install_message:
71
+ rdoc_options: []
72
+
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ hash: 3
81
+ segments:
82
+ - 0
83
+ version: "0"
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ none: false
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ hash: 3
90
+ segments:
91
+ - 0
92
+ version: "0"
93
+ requirements: []
94
+
95
+ rubyforge_project:
96
+ rubygems_version: 1.8.10
97
+ signing_key:
98
+ specification_version: 3
99
+ summary: A simple "keep the passwords out of source code and config files".
100
+ test_files: []
101
+