gibberish 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/README.markdown +8 -3
- data/lib/gibberish.rb +1 -0
- data/lib/gibberish/rsa.rb +52 -0
- data/lib/gibberish/version.rb +1 -1
- data/spec/rsa_spec.rb +43 -0
- metadata +8 -4
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
*.gem
|
data/README.markdown
CHANGED
@@ -37,6 +37,11 @@ Defaults to 256 bit digest
|
|
37
37
|
Gibberish::HMAC("key", "some data")
|
38
38
|
#=> 521677c580722c5c52fa15d978e8656341c4f3c5
|
39
39
|
|
40
|
-
##
|
41
|
-
|
42
|
-
|
40
|
+
## RSA
|
41
|
+
|
42
|
+
k = Gibberish::RSA.generate_keypair(1024)
|
43
|
+
cipher = Gibberish::RSA.new(k.public_key, k.private_key)
|
44
|
+
enc = cipher.encrypt("Some data")
|
45
|
+
# Defaults to Base64 output
|
46
|
+
#=> "JKm98wKyJljqmpx7kP8ZsdeXiShllEMcRHVnjUjc4ecyYK/doKAkVTLho1Gp\ng697qrljyClF0AcIH+XZmeF/TrqYUuCEUyhOD6OL1bs5dn8vFQefS5KdaC5Y\ndLADvh3mSfE/w/gs4vaf/OtbZNBeSl6ROCZasWTfRewp4n1RDmE=\n"
|
47
|
+
dec = cipher.decrypt(enc)
|
data/lib/gibberish.rb
CHANGED
@@ -0,0 +1,52 @@
|
|
1
|
+
module Gibberish
|
2
|
+
class RSA
|
3
|
+
|
4
|
+
class KeyPair
|
5
|
+
def self.generate(bits=2048)
|
6
|
+
self.new(OpenSSL::PKey::RSA.generate(bits))
|
7
|
+
end
|
8
|
+
|
9
|
+
def initialize(key)
|
10
|
+
@key = key
|
11
|
+
end
|
12
|
+
|
13
|
+
def public_key
|
14
|
+
@key.public_key
|
15
|
+
end
|
16
|
+
|
17
|
+
def private_key
|
18
|
+
@key.to_pem
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
def RSA.generate_keypair(bits=2048)
|
24
|
+
KeyPair.generate(bits)
|
25
|
+
end
|
26
|
+
|
27
|
+
# Expects a public key at the minumum
|
28
|
+
#
|
29
|
+
def initialize(public_key, private_key=nil)
|
30
|
+
@pub_key = OpenSSL::PKey::RSA.new(public_key)
|
31
|
+
@priv_key = OpenSSL::PKey::RSA.new(private_key)
|
32
|
+
end
|
33
|
+
|
34
|
+
def encrypt(data, opts={})
|
35
|
+
enc = @pub_key.public_encrypt(data)
|
36
|
+
if opts[:binary]
|
37
|
+
enc
|
38
|
+
else
|
39
|
+
Base64.encode64(enc)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def decrypt(data, opts={})
|
44
|
+
raise "No private key set!" unless @priv_key
|
45
|
+
unless opts[:binary]
|
46
|
+
data = Base64.decode64(data)
|
47
|
+
end
|
48
|
+
@priv_key.private_decrypt(data)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
data/lib/gibberish/version.rb
CHANGED
data/spec/rsa_spec.rb
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "RSA key generation" do
|
4
|
+
it "should generate a key" do
|
5
|
+
keypair = Gibberish::RSA.generate_keypair
|
6
|
+
keypair.should be_instance_of(Gibberish::RSA::KeyPair)
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should generate a key with custom bits" do
|
10
|
+
keypair = Gibberish::RSA.generate_keypair(1024)
|
11
|
+
keypair.should be_instance_of(Gibberish::RSA::KeyPair)
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should answer to public and private key methods" do
|
15
|
+
keypair = Gibberish::RSA.generate_keypair(1024)
|
16
|
+
keypair.should be_instance_of(Gibberish::RSA::KeyPair)
|
17
|
+
keypair.public_key.should_not be_nil
|
18
|
+
keypair.private_key.should_not be_nil
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
describe "RSA" do
|
24
|
+
before do
|
25
|
+
k = Gibberish::RSA.generate_keypair(1024)
|
26
|
+
@cipher = Gibberish::RSA.new(k.public_key, k.private_key)
|
27
|
+
@pub_cipher = Gibberish::RSA.new(k.public_key, k.private_key)
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should encrypt/decrypt with a keypair" do
|
31
|
+
encrypted = @cipher.encrypt("Some data")
|
32
|
+
p encrypted
|
33
|
+
decrypted = @cipher.decrypt(encrypted)
|
34
|
+
encrypted.should match(/^[a-zA-Z0-9\+\\\n=]+$/) # Be base64
|
35
|
+
decrypted.should eql("Some data")
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should work without private key" do
|
39
|
+
enc = @pub_cipher.encrypt("Some data")
|
40
|
+
enc.should_not be_nil
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: gibberish
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 27
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 2
|
10
|
+
version: 0.0.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Mark Percival
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-03-
|
18
|
+
date: 2011-03-29 00:00:00 -07:00
|
19
19
|
default_executable:
|
20
20
|
dependencies: []
|
21
21
|
|
@@ -29,6 +29,7 @@ extensions: []
|
|
29
29
|
extra_rdoc_files: []
|
30
30
|
|
31
31
|
files:
|
32
|
+
- .gitignore
|
32
33
|
- Gemfile
|
33
34
|
- Gemfile.lock
|
34
35
|
- README.markdown
|
@@ -37,10 +38,12 @@ files:
|
|
37
38
|
- lib/gibberish/aes.rb
|
38
39
|
- lib/gibberish/digest.rb
|
39
40
|
- lib/gibberish/hmac.rb
|
41
|
+
- lib/gibberish/rsa.rb
|
40
42
|
- lib/gibberish/version.rb
|
41
43
|
- spec/aes_spec.rb
|
42
44
|
- spec/digest_spec.rb
|
43
45
|
- spec/hmac_spec.rb
|
46
|
+
- spec/rsa_spec.rb
|
44
47
|
- spec/spec_helper.rb
|
45
48
|
has_rdoc: true
|
46
49
|
homepage: http://github.com/mdp/gibberish
|
@@ -80,4 +83,5 @@ test_files:
|
|
80
83
|
- spec/aes_spec.rb
|
81
84
|
- spec/digest_spec.rb
|
82
85
|
- spec/hmac_spec.rb
|
86
|
+
- spec/rsa_spec.rb
|
83
87
|
- spec/spec_helper.rb
|