credstore 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,24 @@
1
+ # Credstore
2
+ # - for working with encrypted data easily
3
+ #
4
+ # originated by Mike Heijmans
5
+ #
6
+ # Apache License
7
+ # Version 2.0, January 2004
8
+ # http://www.apache.org/licenses/
9
+ # Copyright 2013 Michael Heijmans
10
+
11
+ $:.unshift File.dirname(__FILE__)
12
+
13
+ require 'fileutils'
14
+ require 'openssl'
15
+ require 'Base64'
16
+ require 'pstore'
17
+ require 'credstore/crypt'
18
+ require 'credstore/storage'
19
+
20
+ module Credstore
21
+ def Credstore.version
22
+ return "0.0.1"
23
+ end
24
+ end
@@ -0,0 +1,53 @@
1
+ # Credstore
2
+ # - for working with encrypted data easily
3
+ #
4
+ # originated by Mike Heijmans
5
+ #
6
+ # Apache License
7
+ # Version 2.0, January 2004
8
+ # http://www.apache.org/licenses/
9
+ # Copyright 2013 Michael Heijmans
10
+
11
+ require 'openssl'
12
+ require 'Base64'
13
+
14
+ module Credstore
15
+ class Crypt
16
+ def initialize(opts={})
17
+ opts[:keys_dir] ||= "./"
18
+ opts[:public_key] ||= "id_rsa.pub"
19
+ @data_path = opts[:keys_dir]
20
+ @public_path = File.join(@data_path, opts[:public_key])
21
+ @public = get_key opts[:public_key]
22
+ if opts[:private_key]
23
+ @private_path = File.join(@data_path, opts[:private_key])
24
+ @private = get_key opts[:private_key]
25
+ end
26
+ end
27
+
28
+ def encrypt_string message
29
+ Base64::encode64(@public.public_encrypt(message)).rstrip
30
+ end
31
+
32
+ def decrypt_string message
33
+ if message.nil?
34
+ return nil
35
+ end
36
+ @private.private_decrypt Base64::decode64(message)
37
+ end
38
+
39
+ def self.generate_keys(opts={:length=>2048, :keys_dir=>"#{$LIB_BASE_DIR}/tmp/", :public_key=>"id_rsa.pub", :private_key=>"id_rsa"})
40
+ unless File.exists?(File.join(opts[:keys_dir], opts[:private_key])) || File.exists?(File.join(opts[:keys_dir], opts[:public_key]))
41
+ keypair = OpenSSL::PKey::RSA.generate(opts[:length])
42
+ Dir.mkdir(opts[:keys_dir]) unless File.exist?(opts[:keys_dir])
43
+ File.open(File.join(opts[:keys_dir], opts[:private_key]), 'w') { |f| f.write keypair.to_pem } unless File.exists? File.join(opts[:keys_dir], opts[:private_key])
44
+ File.open(File.join(opts[:keys_dir], opts[:public_key]), 'w') { |f| f.write keypair.public_key.to_pem } unless File.exists? File.join(opts[:keys_dir], opts[:public_key])
45
+ end
46
+ end
47
+
48
+ private
49
+ def get_key filename
50
+ OpenSSL::PKey::RSA.new File.read(File.join(@data_path, filename))
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,48 @@
1
+ # Credstore
2
+ # - for working with encrypted data easily
3
+ #
4
+ # originated by Mike Heijmans
5
+ #
6
+ # Apache License
7
+ # Version 2.0, January 2004
8
+ # http://www.apache.org/licenses/
9
+ # Copyright 2013 Michael Heijmans
10
+
11
+ require 'pstore'
12
+ require 'credstore/crypt'
13
+
14
+ module Credstore
15
+ class Storage
16
+ attr_accessor :store
17
+ def initialize(opts={})
18
+ opts[:keys_dir] ||= "./"
19
+ opts[:database] ||= "credstore.db"
20
+ opts[:public_key] ||= "id_rsa.pub"
21
+ opts[:private_key] ||= "id_rsa"
22
+ @crypt = Credstore::Crypt.new({:keys_dir=>opts[:keys_dir], :public_key=>opts[:public_key], :private_key=>opts[:private_key]})
23
+ @store = PStore.new(File.join(opts[:keys_dir], opts[:database]), true)
24
+ end
25
+
26
+ def write_key key, value
27
+ @store.transaction do
28
+ if value.nil?
29
+ @store[key.to_sym] = nil
30
+ else
31
+ @store[key.to_sym] = @crypt.encrypt_string value
32
+ end
33
+ end
34
+ end
35
+
36
+ def read_key key
37
+ @store.transaction do
38
+ @crypt.decrypt_string @store[key.to_sym]
39
+ end
40
+ end
41
+
42
+ def method_missing(id, *args)
43
+ return self.write_key(id.id2name.gsub("=", ""), args.first) if id.id2name =~ /=.*/
44
+ return self.read_key(id.id2name) if id.id2name =~ /.*/
45
+ end
46
+
47
+ end
48
+ end
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: credstore
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Michael Heijmans
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-09-04 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: A gem that provides RSA based string encryption and decryption as well
15
+ as storage of those strings
16
+ email: parabuzzle@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/credstore.rb
22
+ - lib/credstore/crypt.rb
23
+ - lib/credstore/storage.rb
24
+ homepage: https://github.com/parabuzzle/credstore
25
+ licenses: []
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ! '>='
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ required_rubygems_version: !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ! '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubyforge_project:
44
+ rubygems_version: 1.8.25
45
+ signing_key:
46
+ specification_version: 3
47
+ summary: A gem that makes it easy to encrypt and decrypt strings using RSA
48
+ test_files: []