gibberish 0.0.1

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/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+ gemspec
3
+ gem 'rspec'
@@ -0,0 +1,24 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ gibberish (0.0.1)
5
+
6
+ GEM
7
+ remote: http://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.1.2)
10
+ rspec (2.5.0)
11
+ rspec-core (~> 2.5.0)
12
+ rspec-expectations (~> 2.5.0)
13
+ rspec-mocks (~> 2.5.0)
14
+ rspec-core (2.5.1)
15
+ rspec-expectations (2.5.0)
16
+ diff-lcs (~> 1.1.2)
17
+ rspec-mocks (2.5.0)
18
+
19
+ PLATFORMS
20
+ ruby
21
+
22
+ DEPENDENCIES
23
+ gibberish!
24
+ rspec
@@ -0,0 +1,42 @@
1
+ # Gibberish - Stop looking up encryption code snippets!
2
+
3
+ Gibberish is an opinionated cryptography library for Ruby. Its objective is easy to use
4
+ encryption in Ruby while remaining secure.
5
+
6
+ ## Digests
7
+
8
+ Gibberish::MD5("somedata")
9
+ #=> aefaf7502d52994c3b01957636a3cdd2
10
+
11
+ Gibberish::SHA1("somedata")
12
+ #=> efaa311ae448a7374c122061bfed952d940e9e37
13
+
14
+ Gibberish::SHA256("somedata")
15
+ #=> 87d149cb424c0387656f211d2589fb5b1e16229921309e98588419ccca8a7362
16
+
17
+ ## AES
18
+
19
+ Defaults to 256 bit CBC encryption
20
+
21
+ cipher = Gibberish::AES.new("p4ssw0rd")
22
+ cipher.enc("Some top secret data")
23
+ #=> U2FsdGVkX187oKRbgDkUcMKaFfB5RsXQj/X4mc8X3lsUVgwb4+S55LQo6f6N\nIDMX
24
+
25
+ cipher.dec("U2FsdGVkX187oKRbgDkUcMKaFfB5RsXQj/X4mc8X3lsUVgwb4+S55LQo6f6N\nIDMX")
26
+ #=> "Some top secret data"
27
+
28
+ Gibberish AES is fully compatible with default OpenSSL on the command line
29
+
30
+ echo "U2FsdGVkX187oKRbgDkUcMKaFfB5RsXQj/X4mc8X3lsUVgwb4+S55LQo6f6N\nIDMX\n" | \
31
+ openssl enc -d -aes-256-cbc -a -k p4ssw0rd
32
+
33
+ ## HMAC
34
+
35
+ Defaults to 256 bit digest
36
+
37
+ Gibberish::HMAC("key", "some data")
38
+ #=> 521677c580722c5c52fa15d978e8656341c4f3c5
39
+
40
+ ## PKI
41
+
42
+ Coming soon
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "gibberish/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "gibberish"
7
+ s.version = Gibberish::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Mark Percival"]
10
+ s.email = ["mark@markpercival.us"]
11
+ s.homepage = "http://github.com/mdp/gibberish"
12
+ s.summary = %q{An opinionated ruby encryption library}
13
+ s.description = %q{Supports OpenSSL compatible AES, HMAC, and RSA encryption}
14
+
15
+ s.rubyforge_project = "gibberish"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+ end
@@ -0,0 +1,5 @@
1
+ require 'openssl'
2
+ require 'base64'
3
+ require 'gibberish/aes'
4
+ require 'gibberish/digest'
5
+ require 'gibberish/hmac'
@@ -0,0 +1,48 @@
1
+ module Gibberish
2
+ class AES
3
+
4
+ attr_reader :password, :size, :cipher
5
+ def initialize(password, size=256)
6
+ @password = password
7
+ @size = size
8
+ @cipher = OpenSSL::Cipher::Cipher.new("aes-#{size}-cbc")
9
+ end
10
+
11
+ def encrypt(data, opts={})
12
+ salt = generate_salt
13
+ setup_cipher(:encrypt, salt)
14
+ e = cipher.update(data) + cipher.final
15
+ e = "Salted__#{salt}#{e}" #OpenSSL compatible
16
+ if opts[:binary]
17
+ e
18
+ else
19
+ Base64.encode64(e)
20
+ end
21
+ end
22
+ alias :enc :encrypt
23
+ alias :e :encrypt
24
+
25
+ def decrypt(data, opts={})
26
+ data = Base64.decode64(data)
27
+ salt = data[8..15]
28
+ data = data[16..-1]
29
+ setup_cipher(:decrypt, salt)
30
+ cipher.update(data) + cipher.final
31
+ end
32
+ alias :dec :decrypt
33
+ alias :d :decrypt
34
+
35
+ private
36
+
37
+ def generate_salt
38
+ s = ''
39
+ 8.times {s << rand(255).chr}
40
+ s
41
+ end
42
+
43
+ def setup_cipher(method, salt)
44
+ cipher.send(method)
45
+ cipher.pkcs5_keyivgen(password, salt, 1)
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,41 @@
1
+ module Gibberish
2
+ class Digest
3
+
4
+ def self.sha1(val, opts={})
5
+ if opts[:binary]
6
+ OpenSSL::Digest::SHA1.digest(val)
7
+ else
8
+ OpenSSL::Digest::SHA1.hexdigest(val)
9
+ end
10
+ end
11
+
12
+ def self.sha256(val, opts={})
13
+ if opts[:binary]
14
+ OpenSSL::Digest::SHA256.digest(val)
15
+ else
16
+ OpenSSL::Digest::SHA256.hexdigest(val)
17
+ end
18
+ end
19
+
20
+ def self.md5(val, opts={})
21
+ if opts[:binary]
22
+ OpenSSL::Digest::MD5.digest(val)
23
+ else
24
+ OpenSSL::Digest::MD5.hexdigest(val)
25
+ end
26
+ end
27
+ end
28
+
29
+ def self.SHA1(val, opts={})
30
+ Digest.sha1(val,opts)
31
+ end
32
+
33
+ def self.SHA256(val, opts={})
34
+ Digest.sha256(val,opts)
35
+ end
36
+
37
+ def self.MD5(val, opts={})
38
+ Digest.md5(val,opts)
39
+ end
40
+
41
+ end
@@ -0,0 +1,21 @@
1
+ module Gibberish
2
+ class HMAC
3
+ DIGEST = {
4
+ :sha1 => OpenSSL::Digest::Digest.new('sha1'),
5
+ :sha256 => OpenSSL::Digest::Digest.new('sha256')
6
+ }
7
+
8
+ def self.digest(key, data, opts={})
9
+ digest_type = opts[:digest] || :sha1
10
+ if opts[:binary]
11
+ OpenSSL::HMAC.digest(DIGEST[digest_type], key, data)
12
+ else
13
+ OpenSSL::HMAC.hexdigest(DIGEST[digest_type], key, data)
14
+ end
15
+ end
16
+ end
17
+
18
+ def self.HMAC(key, data, opts={})
19
+ Gibberish::HMAC.digest(key, data, opts)
20
+ end
21
+ end
@@ -0,0 +1,3 @@
1
+ module Gibberish
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+
3
+ describe "the aes cipher" do
4
+
5
+ before do
6
+ @cipher = Gibberish::AES.new("password")
7
+ end
8
+
9
+ it "should encrypt text and be compatible with OpenSSL CLI" do
10
+ secret_text = "Made with Gibberish"
11
+ encrypted = @cipher.e(secret_text)
12
+ from_openssl = `echo "#{encrypted}" | openssl enc -d -aes-256-cbc -a -k password`
13
+ from_openssl.should eql(secret_text)
14
+ end
15
+
16
+ it "should decrypt base64 encoded data from the OpenSSL CLI" do
17
+ secret_text = "Made with Gibberish"
18
+ from_openssl = `echo #{secret_text} | openssl enc -aes-256-cbc -a -k password`
19
+ decrypted_text = @cipher.d(from_openssl).chomp
20
+ decrypted_text.should eql(secret_text)
21
+ end
22
+
23
+ end
@@ -0,0 +1,17 @@
1
+ require 'spec_helper'
2
+
3
+ describe "A variety of digest methods" do
4
+
5
+ it "should work with MD5" do
6
+ Gibberish::MD5("password").should eql("5f4dcc3b5aa765d61d8327deb882cf99")
7
+ end
8
+
9
+ it "should work with SHA1" do
10
+ Gibberish::SHA1("password").should eql("5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8")
11
+ end
12
+
13
+ it "should work with SHA256" do
14
+ Gibberish::SHA256("password").should eql("5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8")
15
+ end
16
+
17
+ end
@@ -0,0 +1,9 @@
1
+ require 'spec_helper'
2
+
3
+ describe "HMAC" do
4
+
5
+ it "should hopefully work" do
6
+ Gibberish::HMAC("password", "data").should eql("08d13c72bed7ace5efadc09df109a78a5d713097")
7
+ end
8
+
9
+ end
@@ -0,0 +1,8 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+
4
+ require 'gibberish'
5
+
6
+ RSpec.configure do |config|
7
+ # some (optional) config here
8
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gibberish
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Mark Percival
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-03-28 00:00:00 -07:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: Supports OpenSSL compatible AES, HMAC, and RSA encryption
23
+ email:
24
+ - mark@markpercival.us
25
+ executables: []
26
+
27
+ extensions: []
28
+
29
+ extra_rdoc_files: []
30
+
31
+ files:
32
+ - Gemfile
33
+ - Gemfile.lock
34
+ - README.markdown
35
+ - gibberish.gemspec
36
+ - lib/gibberish.rb
37
+ - lib/gibberish/aes.rb
38
+ - lib/gibberish/digest.rb
39
+ - lib/gibberish/hmac.rb
40
+ - lib/gibberish/version.rb
41
+ - spec/aes_spec.rb
42
+ - spec/digest_spec.rb
43
+ - spec/hmac_spec.rb
44
+ - spec/spec_helper.rb
45
+ has_rdoc: true
46
+ homepage: http://github.com/mdp/gibberish
47
+ licenses: []
48
+
49
+ post_install_message:
50
+ rdoc_options: []
51
+
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ hash: 3
60
+ segments:
61
+ - 0
62
+ version: "0"
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ hash: 3
69
+ segments:
70
+ - 0
71
+ version: "0"
72
+ requirements: []
73
+
74
+ rubyforge_project: gibberish
75
+ rubygems_version: 1.6.2
76
+ signing_key:
77
+ specification_version: 3
78
+ summary: An opinionated ruby encryption library
79
+ test_files:
80
+ - spec/aes_spec.rb
81
+ - spec/digest_spec.rb
82
+ - spec/hmac_spec.rb
83
+ - spec/spec_helper.rb