cerebus 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +4 -0
- data/README.md +33 -0
- data/Rakefile +1 -0
- data/cerebus.gemspec +19 -0
- data/decrypt.rb +7 -0
- data/encrypt.rb +12 -0
- data/lib/cerebus.rb +42 -0
- data/make_keys.sh +5 -0
- data/test/cerebus_spec.rb +50 -0
- data/test/keys/private.pem +30 -0
- data/test/keys/public.pem +9 -0
- metadata +70 -0
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# Cerebus
|
2
|
+
|
3
|
+
## Summary
|
4
|
+
|
5
|
+
This is a simple library which wraps openssl to provide a (hopefully)
|
6
|
+
secure encryption system for arbitrary strings. The aim was to remove
|
7
|
+
all the hard stuff and make it blindingly easy to encrypt and decrypt
|
8
|
+
strings. Of course, this doesn't mean you can ignore basic crypographic
|
9
|
+
best practices. If you store all your keys insecurely then you might as
|
10
|
+
well use rot13.
|
11
|
+
|
12
|
+
## Examples
|
13
|
+
|
14
|
+
For regular ruby:
|
15
|
+
|
16
|
+
```
|
17
|
+
require 'cerebus'
|
18
|
+
cleartext = 'It is a secret to everybody!'
|
19
|
+
encrypted = Cerebus.encrypt cleartext, 'test/keys/public.pem'
|
20
|
+
```
|
21
|
+
|
22
|
+
For rails it's the same, just first add it to the Gemfile and bundle install to get openssl.
|
23
|
+
|
24
|
+
## Making Keys
|
25
|
+
|
26
|
+
You can generate some new keys with the included `make_keys.sh` script.
|
27
|
+
Be sure to use a good passphrase and keep your private.pem file stored
|
28
|
+
safely (whatever that means for the application in question.)
|
29
|
+
|
30
|
+
## Bugs
|
31
|
+
|
32
|
+
Please fix them and send me a pull request. Or create an issue. Or email
|
33
|
+
me at jonathan (a.t) blazingdev , com.
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/cerebus.gemspec
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require 'cerebus'
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "cerebus"
|
7
|
+
s.version = Cerebus::VERSION
|
8
|
+
s.summary = "Simple and secure RSA/Blowfish encryption."
|
9
|
+
s.description = "An ecryption library which wraps openssl to give users an easy interface to encrypt any file using a publc and private key."
|
10
|
+
s.authors = ["Jonathan Jeffus"]
|
11
|
+
s.email = 'jonathan@blazingdev.com'
|
12
|
+
s.homepage = 'http://github.com/jjeffus/cerebus'
|
13
|
+
s.rubyforge_project = "cerebus"
|
14
|
+
s.files = `git ls-files`.split("\n")
|
15
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
16
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
17
|
+
s.require_paths = ["lib"]
|
18
|
+
s.add_runtime_dependency "openssl"
|
19
|
+
end
|
data/decrypt.rb
ADDED
data/encrypt.rb
ADDED
data/lib/cerebus.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'openssl'
|
4
|
+
|
5
|
+
module Cerebus
|
6
|
+
VERSION = "0.0.1"
|
7
|
+
|
8
|
+
def Cerebus.make_key
|
9
|
+
OpenSSL::Random.random_bytes(56)
|
10
|
+
end
|
11
|
+
def Cerebus.decrypt_blowfish(data, key)
|
12
|
+
cipher = OpenSSL::Cipher::Cipher.new('bf-cbc').decrypt
|
13
|
+
cipher.key = Digest::MD5.digest key.to_s
|
14
|
+
cipher.update(data) << cipher.final
|
15
|
+
end
|
16
|
+
def Cerebus.decrypt_rsa(data, key_filename, passphrase)
|
17
|
+
opri = OpenSSL::PKey::RSA.new( File.read(key_filename), passphrase )
|
18
|
+
opri.private_decrypt data
|
19
|
+
end
|
20
|
+
def Cerebus.encrypt_blowfish(data, key)
|
21
|
+
cipher = OpenSSL::Cipher::Cipher.new('bf-cbc').encrypt
|
22
|
+
cipher.key = Digest::MD5.digest key
|
23
|
+
cipher.update(data) << cipher.final
|
24
|
+
end
|
25
|
+
def Cerebus.encrypt_rsa(data, key_filename)
|
26
|
+
opri = OpenSSL::PKey::RSA.new File.read key_filename
|
27
|
+
opri.public_encrypt data
|
28
|
+
end
|
29
|
+
def Cerebus.encrypt(data, key_filename)
|
30
|
+
key = Cerebus.make_key
|
31
|
+
encrypted_key = Cerebus.encrypt_rsa(key, key_filename).unpack("H*")[0]
|
32
|
+
encrypted_data = Cerebus.encrypt_blowfish(data, key).unpack("H*")[0]
|
33
|
+
(encrypted_key.to_s + encrypted_data.to_s)
|
34
|
+
end
|
35
|
+
def Cerebus.decrypt(incoming, key_filename, passphrase)
|
36
|
+
data = StringIO.new(incoming)
|
37
|
+
encrypted_key = [data.read(512)].pack("H*")
|
38
|
+
encrypted_data = [data.read].pack("H*")
|
39
|
+
decrypted_key = Cerebus.decrypt_rsa(encrypted_key, key_filename, passphrase)
|
40
|
+
decrypted_data = Cerebus.decrypt_blowfish(encrypted_data, decrypted_key)
|
41
|
+
end
|
42
|
+
end
|
data/make_keys.sh
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
require_relative '../lib/cerebus'
|
2
|
+
require 'rspec'
|
3
|
+
|
4
|
+
describe Cerebus do
|
5
|
+
KEY = '01234567890123456789012345678901234567890123456789012345'
|
6
|
+
TEXT = 'It is a secret to everybody!'
|
7
|
+
CRYPTED = 'c7829fcbf4ae6ff33a22576863f3170a6526e3ab8c786412b8250efc41e4771f'
|
8
|
+
PUBLIC = 'keys/public.pem'
|
9
|
+
PRIVATE = 'keys/private.pem'
|
10
|
+
PHRASE = 'test'
|
11
|
+
|
12
|
+
describe :make_key do
|
13
|
+
it "should return a key that's 56 bytes long" do
|
14
|
+
Cerebus.make_key.length.should eq(56)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe :encrypt_blowfix do
|
19
|
+
it "should encrypt to the cipher data" do
|
20
|
+
Cerebus.encrypt_blowfish(TEXT, KEY).unpack("H*")[0].should eq(CRYPTED)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
describe :decrypt_blowfix do
|
25
|
+
it "should decrypt to the clear text" do
|
26
|
+
Cerebus.decrypt_blowfish([CRYPTED].pack("H*"), KEY).should eq(TEXT)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
describe :rsa do
|
31
|
+
it "should be reversible" do
|
32
|
+
encrypted = Cerebus.encrypt_rsa(KEY, PUBLIC)
|
33
|
+
decrypted = Cerebus.decrypt_rsa(encrypted, PRIVATE, PHRASE)
|
34
|
+
decrypted.should eq(KEY)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
describe :round_trip do
|
39
|
+
it 'should encrypt and decrypt small strings' do
|
40
|
+
encrypted = Cerebus.encrypt('small', PUBLIC)
|
41
|
+
decrypted = Cerebus.decrypt(encrypted, PRIVATE, PHRASE)
|
42
|
+
decrypted.should eq('small')
|
43
|
+
end
|
44
|
+
it 'should encrypt and decrypt big strings' do
|
45
|
+
encrypted = Cerebus.encrypt( TEXT*100, PUBLIC)
|
46
|
+
decrypted = Cerebus.decrypt(encrypted, PRIVATE, PHRASE)
|
47
|
+
decrypted.should eq(TEXT*100)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
-----BEGIN RSA PRIVATE KEY-----
|
2
|
+
Proc-Type: 4,ENCRYPTED
|
3
|
+
DEK-Info: DES-EDE3-CBC,2AF2AB7CCA8363EC
|
4
|
+
|
5
|
+
dpPIoXzeTqOdLU8jdDdrNlNF8BeSXXgXJraIi1Dh25hiisIJQDL6tupez8rzNgca
|
6
|
+
bKi7gL/31ac72PEM2jofuvIYLUGz1OHy4/+7e93gTS0cYP3tBPxIci7KXVhaCSKO
|
7
|
+
3pzBrxrdv2Ngck438MyIYqSMZD6SJggMt+IAztlGFfPMC76QS83+ghy2/njQ/dAK
|
8
|
+
0218Ul+PBs2L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ata
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cerebus
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Jonathan Jeffus
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-04-15 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: openssl
|
16
|
+
requirement: &70344342031260 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70344342031260
|
25
|
+
description: An ecryption library which wraps openssl to give users an easy interface
|
26
|
+
to encrypt any file using a publc and private key.
|
27
|
+
email: jonathan@blazingdev.com
|
28
|
+
executables: []
|
29
|
+
extensions: []
|
30
|
+
extra_rdoc_files: []
|
31
|
+
files:
|
32
|
+
- Gemfile
|
33
|
+
- README.md
|
34
|
+
- Rakefile
|
35
|
+
- cerebus.gemspec
|
36
|
+
- decrypt.rb
|
37
|
+
- encrypt.rb
|
38
|
+
- lib/cerebus.rb
|
39
|
+
- make_keys.sh
|
40
|
+
- test/cerebus_spec.rb
|
41
|
+
- test/keys/private.pem
|
42
|
+
- test/keys/public.pem
|
43
|
+
homepage: http://github.com/jjeffus/cerebus
|
44
|
+
licenses: []
|
45
|
+
post_install_message:
|
46
|
+
rdoc_options: []
|
47
|
+
require_paths:
|
48
|
+
- lib
|
49
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
56
|
+
none: false
|
57
|
+
requirements:
|
58
|
+
- - ! '>='
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
61
|
+
requirements: []
|
62
|
+
rubyforge_project: cerebus
|
63
|
+
rubygems_version: 1.8.17
|
64
|
+
signing_key:
|
65
|
+
specification_version: 3
|
66
|
+
summary: Simple and secure RSA/Blowfish encryption.
|
67
|
+
test_files:
|
68
|
+
- test/cerebus_spec.rb
|
69
|
+
- test/keys/private.pem
|
70
|
+
- test/keys/public.pem
|