rcrypt 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,6 @@
1
+ *.sw?
2
+ *~
3
+ .DS_Store
4
+ coverage
5
+ rdoc
6
+ pkg
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Quin Hoxie
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,32 @@
1
+ RCrypt
2
+ ======
3
+ RCrypt is a simple RSA encryption library for your Ruby scripts. It aims at
4
+ being a no-frills encrypt-decrypt solution when that is all you actually need.
5
+
6
+ Installation
7
+ ------------
8
+ gem install qhoxie-rcrypt --source http://gems.github.com
9
+
10
+ Usage
11
+ -----
12
+ There are a few ways to set your keys:
13
+
14
+ >> RCrypt.private_key_file "path/to/id_rsa" # you can
15
+ >> RCrypt.public_key_file "path/to/id_rsa.pub" # specify the files
16
+ >> RCrypt.generate_key_pair # or generate the keys in RCrypt
17
+ => {:public => "...", :private => "..."}
18
+
19
+ Once the keys are set, you can encrypt and decrypt.
20
+
21
+ >> ciphertext = RCrypt.encrypt "secret"
22
+ => "xO0h20SZZ3US6BJGOyl0mnjF1FIVqax8=="
23
+ >> RCrypt.decrypt ciphertext
24
+ => "secret"
25
+
26
+ Comments/Suggestions/Requests
27
+ ----------------------------
28
+ Email me: qhoxie on gmail.com
29
+
30
+ Copyright
31
+ ---------
32
+ Copyright (c) 2009 Quin Hoxie. See LICENSE for details.
@@ -0,0 +1,57 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "rcrypt"
8
+ gem.summary = %Q{Simple and secure encryption for Ruby.}
9
+ gem.description = %Q{RCrypt is a simple RSA encryption library for your Ruby scripts. It aims at being a no-frills encrypt-decrypt solution when that is all you actually need.}
10
+ gem.email = "qhoxie@gmail.com"
11
+ gem.homepage = "http://github.com/qhoxie/rcrypt"
12
+ gem.authors = ["Quin Hoxie"]
13
+ gem.add_development_dependency "thoughtbot-shoulda"
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
19
+ end
20
+
21
+ require 'rake/testtask'
22
+ Rake::TestTask.new(:test) do |test|
23
+ test.libs << 'lib' << 'test'
24
+ test.pattern = 'test/**/*_test.rb'
25
+ test.verbose = true
26
+ end
27
+
28
+ begin
29
+ require 'rcov/rcovtask'
30
+ Rcov::RcovTask.new do |test|
31
+ test.libs << 'test'
32
+ test.pattern = 'test/**/*_test.rb'
33
+ test.verbose = true
34
+ end
35
+ rescue LoadError
36
+ task :rcov do
37
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
38
+ end
39
+ end
40
+
41
+ task :test => :check_dependencies
42
+
43
+ task :default => :test
44
+
45
+ require 'rake/rdoctask'
46
+ Rake::RDocTask.new do |rdoc|
47
+ if File.exist?('VERSION')
48
+ version = File.read('VERSION')
49
+ else
50
+ version = ""
51
+ end
52
+
53
+ rdoc.rdoc_dir = 'rdoc'
54
+ rdoc.title = "rcrypt #{version}"
55
+ rdoc.rdoc_files.include('README*')
56
+ rdoc.rdoc_files.include('lib/**/*.rb')
57
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,50 @@
1
+ require 'openssl'
2
+ require 'base64'
3
+
4
+ class RCrypt
5
+ class << self
6
+ %w{private public}.each do |type|
7
+ name = :"#{type}_key"
8
+ define_method(name) do
9
+ instance_variable_get "@#{name}"
10
+ end
11
+
12
+ define_method(:"#{name}=") do |key_text|
13
+ instance_variable_set "@#{name}", key_text
14
+ end
15
+
16
+ define_method(:"#{name}_file") do |key_file|
17
+ path = File.expand_path(key_file)
18
+ raise CryptException.new("Key file is not readable.") unless File.readable?(path)
19
+ send :"#{name}=", File.read(path)
20
+ end
21
+ end
22
+
23
+ def reset!
24
+ @key_pair = @private_key = @public_key = nil
25
+ end
26
+
27
+ def generate_key_pair
28
+ return @key_pair if @key_pair
29
+ pair = OpenSSL::PKey::RSA.generate(2048)
30
+ @private_key, @public_key = [pair.to_pem, pair.public_key.to_pem]
31
+ @key_pair = {:private => @private_key, :public => @public_key}
32
+ end
33
+
34
+ def decrypt(plaintext, priv_key = @private_key)
35
+ raise CryptException.new("No private key specified.") unless priv_key
36
+ key(priv_key).private_decrypt Base64.decode64(plaintext)
37
+ end
38
+
39
+ def encrypt(plaintext, pub_key = @public_key)
40
+ raise CryptException.new("No public key specified.") unless pub_key
41
+ Base64.encode64 key(pub_key).public_encrypt(plaintext)
42
+ end
43
+
44
+ def key(key_text)
45
+ OpenSSL::PKey::RSA.new key_text
46
+ end
47
+ end
48
+
49
+ class CryptException < Exception; end
50
+ end
@@ -0,0 +1,55 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{rcrypt}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Quin Hoxie"]
12
+ s.date = %q{2009-10-13}
13
+ s.description = %q{RCrypt is a simple RSA encryption library for your Ruby scripts. It aims at being a no-frills encrypt-decrypt solution when that is all you actually need.}
14
+ s.email = %q{qhoxie@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.md"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.md",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "lib/rcrypt.rb",
27
+ "rcrypt.gemspec",
28
+ "test/fixtures/id_rsa",
29
+ "test/fixtures/id_rsa.pub",
30
+ "test/rcrypt_test.rb",
31
+ "test/test_helper.rb"
32
+ ]
33
+ s.homepage = %q{http://github.com/qhoxie/rcrypt}
34
+ s.rdoc_options = ["--charset=UTF-8"]
35
+ s.require_paths = ["lib"]
36
+ s.rubygems_version = %q{1.3.5}
37
+ s.summary = %q{Simple and secure encryption for Ruby.}
38
+ s.test_files = [
39
+ "test/rcrypt_test.rb",
40
+ "test/test_helper.rb"
41
+ ]
42
+
43
+ if s.respond_to? :specification_version then
44
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
45
+ s.specification_version = 3
46
+
47
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
48
+ s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
49
+ else
50
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
51
+ end
52
+ else
53
+ s.add_dependency(%q<thoughtbot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YKxEMhMqUQGOH+1e7VflxaNbgzzOukscBf1r1+t/B/8COSWSiJneu
7
+ 5BHYZHY8aSo+eRupfZJBsJ30wo4oUM9uNQIDAQAB
8
+ -----END RSA PUBLIC KEY-----
@@ -0,0 +1,42 @@
1
+ require 'test_helper'
2
+
3
+ class RCryptTest < Test::Unit::TestCase
4
+ def from_generated
5
+ RCrypt.generate_key_pair
6
+ end
7
+
8
+ def from_file
9
+ RCrypt.public_key_file File.join(FIXTURES_DIR, "id_rsa.pub")
10
+ RCrypt.private_key_file File.join(FIXTURES_DIR, "id_rsa")
11
+ end
12
+
13
+ context "Encrypting and decrypting data" do
14
+ setup do
15
+ @plaintext = "secret"
16
+ end
17
+
18
+ %w{generated file}.each do |in_type|
19
+ context "with #{in_type} keys" do
20
+ setup do
21
+ RCrypt.reset!
22
+ send :"from_#{in_type}"
23
+ end
24
+
25
+ %w{private public}.each do |key_type|
26
+ should "have a #{key_type} key present" do
27
+ assert RCrypt.send(:"#{key_type}_key")
28
+ end
29
+ end
30
+
31
+ should "encrypt to ciphertext" do
32
+ assert RCrypt.encrypt(@plaintext).length
33
+ end
34
+
35
+ should "decrypt to plaintext" do
36
+ ciphertext = RCrypt.encrypt(@plaintext)
37
+ assert_equal RCrypt.decrypt(ciphertext), @plaintext
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,9 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'rcrypt'
8
+
9
+ FIXTURES_DIR = File.join File.dirname(__FILE__), "fixtures"
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rcrypt
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Quin Hoxie
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-13 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: thoughtbot-shoulda
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ description: RCrypt is a simple RSA encryption library for your Ruby scripts. It aims at being a no-frills encrypt-decrypt solution when that is all you actually need.
26
+ email: qhoxie@gmail.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - LICENSE
33
+ - README.md
34
+ files:
35
+ - .document
36
+ - .gitignore
37
+ - LICENSE
38
+ - README.md
39
+ - Rakefile
40
+ - VERSION
41
+ - lib/rcrypt.rb
42
+ - rcrypt.gemspec
43
+ - test/fixtures/id_rsa
44
+ - test/fixtures/id_rsa.pub
45
+ - test/rcrypt_test.rb
46
+ - test/test_helper.rb
47
+ has_rdoc: true
48
+ homepage: http://github.com/qhoxie/rcrypt
49
+ licenses: []
50
+
51
+ post_install_message:
52
+ rdoc_options:
53
+ - --charset=UTF-8
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: "0"
61
+ version:
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: "0"
67
+ version:
68
+ requirements: []
69
+
70
+ rubyforge_project:
71
+ rubygems_version: 1.3.5
72
+ signing_key:
73
+ specification_version: 3
74
+ summary: Simple and secure encryption for Ruby.
75
+ test_files:
76
+ - test/rcrypt_test.rb
77
+ - test/test_helper.rb