technoweenie-pki 1.0.0
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/LICENSE +20 -0
- data/README.rdoc +25 -0
- data/VERSION.yml +4 -0
- data/lib/pki.rb +92 -0
- data/test/pki_test.rb +78 -0
- data/test/test_helper.rb +16 -0
- metadata +60 -0
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 rick
|
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.
|
data/README.rdoc
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
= pki
|
2
|
+
|
3
|
+
Amazingly simple pki encryption, using undocumented OpenSSL functions. Use
|
4
|
+
public keys to encrypt data, and private keys to decrypt them.
|
5
|
+
|
6
|
+
# uses a randomly generated RSA private key
|
7
|
+
pki = Pki.new
|
8
|
+
encrypted = pki.encrypt('monkey')
|
9
|
+
pki.decrypt(encrypted) == 'monkey'
|
10
|
+
|
11
|
+
# load a private key from a stream.
|
12
|
+
# if no public key is specified, a new one is created using the existing
|
13
|
+
# private key.
|
14
|
+
pki = Pki.new(:private_key => File.open('my_priv_key'))
|
15
|
+
|
16
|
+
# load private/public keys
|
17
|
+
pki = Pki.new :private_key => "-----BEGIN RSA PRIVATE KEY-----\n..."
|
18
|
+
pki.public_key = OpenSSL::PKey::RSA.new(...)
|
19
|
+
|
20
|
+
# you can pretty much load private keys from strings, from streams, or PKey
|
21
|
+
objects that respond to #public_encrypt and #private_decrypt.
|
22
|
+
|
23
|
+
== Copyright
|
24
|
+
|
25
|
+
Copyright (c) 2009 rick. See LICENSE for details.
|
data/VERSION.yml
ADDED
data/lib/pki.rb
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
require 'openssl'
|
2
|
+
class Pki
|
3
|
+
class KeyTypeError < StandardError; end
|
4
|
+
|
5
|
+
class << self
|
6
|
+
attr_accessor :default_cert_type
|
7
|
+
attr_accessor :default_cert_size
|
8
|
+
end
|
9
|
+
|
10
|
+
attr_writer :cert_type
|
11
|
+
attr_writer :cert_size
|
12
|
+
|
13
|
+
self.default_cert_type = OpenSSL::PKey::RSA
|
14
|
+
self.default_cert_size = 512
|
15
|
+
|
16
|
+
def initialize(options = {})
|
17
|
+
@private_key = @public_key = @cert_key = @cert_size = nil
|
18
|
+
if options.key?(:private_key)
|
19
|
+
self.private_key = options[:private_key]
|
20
|
+
end
|
21
|
+
if options.key?(:public_key)
|
22
|
+
self.public_key = options[:public_key]
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def encrypt(data)
|
27
|
+
public_key.public_encrypt(data)
|
28
|
+
end
|
29
|
+
|
30
|
+
def decrypt(data)
|
31
|
+
private_key.private_decrypt(data)
|
32
|
+
end
|
33
|
+
|
34
|
+
begin
|
35
|
+
require 'base64'
|
36
|
+
def encrypt64(data)
|
37
|
+
Base64.encode64(encrypt(data))
|
38
|
+
end
|
39
|
+
|
40
|
+
def decrypt64(data)
|
41
|
+
decrypt(Base64.decode64(data))
|
42
|
+
end
|
43
|
+
rescue LoadError
|
44
|
+
end
|
45
|
+
|
46
|
+
def private_key
|
47
|
+
@private_key ||= load_key(:private)
|
48
|
+
end
|
49
|
+
|
50
|
+
def private_key=(key = nil)
|
51
|
+
@private_key = load_key(:private, key)
|
52
|
+
end
|
53
|
+
|
54
|
+
def public_key
|
55
|
+
@public_key ||= load_key(:public)
|
56
|
+
end
|
57
|
+
|
58
|
+
def public_key=(key = nil)
|
59
|
+
@public_key = load_key(:public, key)
|
60
|
+
end
|
61
|
+
|
62
|
+
def cert_type
|
63
|
+
@cert_type ||= self.class.default_cert_type
|
64
|
+
end
|
65
|
+
|
66
|
+
def cert_size
|
67
|
+
@cert_size ||= self.class.default_cert_size
|
68
|
+
end
|
69
|
+
|
70
|
+
private
|
71
|
+
def load_key(type, key = nil)
|
72
|
+
key = case key
|
73
|
+
when String
|
74
|
+
cert_type.new(key)
|
75
|
+
when nil
|
76
|
+
if type == :private
|
77
|
+
cert_type.new(cert_size)
|
78
|
+
else
|
79
|
+
private_key.public_key
|
80
|
+
end
|
81
|
+
else
|
82
|
+
if key.respond_to?(:read)
|
83
|
+
load_key(type, key.read)
|
84
|
+
end
|
85
|
+
end
|
86
|
+
if !key || !key.send("#{type}?")
|
87
|
+
raise KeyTypeError, "#{type} key should be a valid PKey, String, IO stream, or nil: #{key.inspect}"
|
88
|
+
else
|
89
|
+
key
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
data/test/pki_test.rb
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
$private_key = "-----BEGIN RSA PRIVATE KEY-----\nMIIBOQIBAAJBAOLftrUhAdZpodQ8OczSgcudLAogxjuh5834rHD7bAs51UTSCoAQ\nOPHaadlTPd+1XEogvTqIP7KYZL5w83RbH58CAwEAAQJANcrbdFjuKZuELmFVRxZG\nhjOvBHu+5Na/spuar3M9q+9JIXAMmenFQbQkedR+utpvUUGxacCFCIWxuqmzeEKc\nUQIhAPnMyvb0tRePVbfFjc2kXkN7Yr2bmZ5SVAD9h84NDjnHAiEA6IFAIKRw11s2\npvDYbKrjNhJapSriuQQ6RSNB6NOnK2kCIHERIU1mthFT75ie8vCB1aj10FvCcmYX\nHa7VEwNRJX9BAiB0xpYCzxSt1W45orXQvnOn8Mf+NO/ypSDvIKo12jgYIQIgcSfd\nlw7dCdeaJraI98Sh6bhZUVvo2z9Yyel6lpMLZN0=\n-----END RSA PRIVATE KEY-----\n"
|
4
|
+
$public_key = "-----BEGIN RSA PUBLIC KEY-----\nMEgCQQDi37a1IQHWaaHUPDnM0oHLnSwKIMY7oefN+Kxw+2wLOdVE0gqAEDjx2mnZ\nUz3ftVxKIL06iD+ymGS+cPN0Wx+fAgMBAAE=\n-----END RSA PUBLIC KEY-----\n"
|
5
|
+
|
6
|
+
class PkiTest < Test::Unit::TestCase
|
7
|
+
def test_encrypts_with_public_key_and_decrypts_with_private_key
|
8
|
+
pki = Pki.new :private_key => $private_key, :public_key => $public_key
|
9
|
+
encrypted = pki.encrypt 'monkey'
|
10
|
+
assert_equal 'monkey', pki.decrypt(encrypted)
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_encrypts_with_public_key_and_decrypts_with_private_key_in_base64
|
14
|
+
pki = Pki.new :private_key => $private_key, :public_key => $public_key
|
15
|
+
encrypted = pki.encrypt64 'monkey'
|
16
|
+
assert_equal 'monkey', pki.decrypt64(encrypted)
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_creates_random_private_key
|
20
|
+
pki = Pki.new
|
21
|
+
assert_not_nil pki.private_key
|
22
|
+
assert pki.private_key.private?
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_initializes_with_private_key_string
|
26
|
+
pki = Pki.new :private_key => $private_key
|
27
|
+
assert pki.private_key.private?
|
28
|
+
assert_equal $private_key, pki.private_key.to_s
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_sets_private_key_string
|
32
|
+
pki = Pki.new
|
33
|
+
pki.private_key = $private_key
|
34
|
+
assert pki.private_key.private?
|
35
|
+
assert_equal $private_key, pki.private_key.to_s
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_sets_private_key_stream
|
39
|
+
pki = Pki.new
|
40
|
+
io = StringIO.new($private_key)
|
41
|
+
pki.private_key = io
|
42
|
+
assert pki.private_key.private?
|
43
|
+
assert_equal $private_key, pki.private_key.to_s
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_creates_random_public_key
|
47
|
+
pki = Pki.new
|
48
|
+
assert_not_nil pki.public_key
|
49
|
+
assert pki.public_key.public?
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_initializes_with_public_key_string
|
53
|
+
pki = Pki.new :public_key => $public_key
|
54
|
+
assert pki.public_key.public?
|
55
|
+
assert_equal $public_key, pki.public_key.to_s
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_sets_public_key_string
|
59
|
+
pki = Pki.new
|
60
|
+
pki.public_key = $public_key
|
61
|
+
assert pki.public_key.public?
|
62
|
+
assert_equal $public_key, pki.public_key.to_s
|
63
|
+
end
|
64
|
+
|
65
|
+
def test_sets_public_key_stream
|
66
|
+
pki = Pki.new
|
67
|
+
io = StringIO.new($public_key)
|
68
|
+
pki.public_key = io
|
69
|
+
assert pki.public_key.public?
|
70
|
+
assert_equal $public_key, pki.public_key.to_s
|
71
|
+
end
|
72
|
+
|
73
|
+
def test_raises_error_on_setting_public_key_to_private_key
|
74
|
+
assert_raises Pki::KeyTypeError do
|
75
|
+
Pki.new :private_key => $public_key
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'test/unit'
|
3
|
+
require 'stringio'
|
4
|
+
|
5
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
6
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
7
|
+
require 'pki'
|
8
|
+
|
9
|
+
begin
|
10
|
+
require 'ruby-debug'
|
11
|
+
Debugger.start
|
12
|
+
rescue LoadError
|
13
|
+
end
|
14
|
+
|
15
|
+
class Test::Unit::TestCase
|
16
|
+
end
|
metadata
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: technoweenie-pki
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- rick
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-05-31 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description:
|
17
|
+
email: technoweenie@gmail.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README.rdoc
|
24
|
+
- LICENSE
|
25
|
+
files:
|
26
|
+
- README.rdoc
|
27
|
+
- VERSION.yml
|
28
|
+
- lib/pki.rb
|
29
|
+
- test/pki_test.rb
|
30
|
+
- test/test_helper.rb
|
31
|
+
- LICENSE
|
32
|
+
has_rdoc: false
|
33
|
+
homepage: http://github.com/technoweenie/pki
|
34
|
+
post_install_message:
|
35
|
+
rdoc_options:
|
36
|
+
- --inline-source
|
37
|
+
- --charset=UTF-8
|
38
|
+
require_paths:
|
39
|
+
- lib
|
40
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
41
|
+
requirements:
|
42
|
+
- - ">="
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: "0"
|
45
|
+
version:
|
46
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: "0"
|
51
|
+
version:
|
52
|
+
requirements: []
|
53
|
+
|
54
|
+
rubyforge_project:
|
55
|
+
rubygems_version: 1.2.0
|
56
|
+
signing_key:
|
57
|
+
specification_version: 3
|
58
|
+
summary: TODO
|
59
|
+
test_files: []
|
60
|
+
|