easy_cipher 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.rspec +2 -0
- data/.travis.yml +3 -0
- data/Gemfile +4 -0
- data/README.md +61 -0
- data/Rakefile +2 -0
- data/easy_cipher.gemspec +22 -0
- data/lib/easy_cipher.rb +6 -0
- data/lib/easy_cipher/cipher.rb +93 -0
- data/lib/easy_cipher/version.rb +3 -0
- metadata +81 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: a45c8a563832ddfceed97b0ec41ec6000955d2c8
|
4
|
+
data.tar.gz: 8a723173ad1aeccf7d65345f1e6d94b30b3376e4
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f17be959ad6d87d67171cc04cf323cf20f71b3de55619ecfd8759163076d9e4bc083dee6d443227a2ada6f74f26fe7c4c1874def2227218da28e253f657aa2fb
|
7
|
+
data.tar.gz: 39eaf348a60ef22e1afda21ddd5c80ef15bbf61e930c79a8b0b0fc47c5899ebd04321f4a1abebaf0f147a8c26f219061ffffd8d409538590a23e10b66ee5af7e
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
# EasyCipher
|
2
|
+
|
3
|
+
A simple gem that uses ruby's OpenSSL package to simplify normal cipher use.
|
4
|
+
It does not rely on any external libraries.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem 'easy_cipher'
|
12
|
+
```
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install easy_cipher
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
### To create an entirely new cipher
|
25
|
+
```ruby
|
26
|
+
cipher = EasyCipher::Cipher.new
|
27
|
+
encrypted_data = cipher.encrypt "my data"
|
28
|
+
decrypted_data = cipher.decrypt encrypted_data
|
29
|
+
=> "my data"
|
30
|
+
|
31
|
+
cipher.key
|
32
|
+
=> "ZE\xD9\xE2B\n2J1\xDF\x10$\x03A\xB95\xE9P\xDF\xD5\xF6\xAA<\x06C\x82~\x06]\xBB\xE1G"
|
33
|
+
|
34
|
+
cipher.iv
|
35
|
+
=> "QHB\xE6\xEDpE\xDCha\x80\x02\xDB\xA5A\xAB"
|
36
|
+
```
|
37
|
+
|
38
|
+
### To create a cipher from a known key, iv
|
39
|
+
```ruby
|
40
|
+
known_cipher = EasyCipher::Cipher.new(known_key, known_iv)
|
41
|
+
|
42
|
+
# then use it as normal.
|
43
|
+
```
|
44
|
+
|
45
|
+
### You can also create from base64 encoded values
|
46
|
+
```ruby
|
47
|
+
key64 = my_first_cipher.key64
|
48
|
+
iv64 = my_first_cipher.iv64
|
49
|
+
new_cipher = EasyCipher::Cipher.new64(key64,iv64)
|
50
|
+
|
51
|
+
# then use it as normal.
|
52
|
+
# Base64 encodings work with SQL string fields.
|
53
|
+
```
|
54
|
+
|
55
|
+
## Contributing
|
56
|
+
|
57
|
+
1. Fork it ( https://github.com/malakai97/easy_cipher/fork )
|
58
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
59
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
60
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
61
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/easy_cipher.gemspec
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'easy_cipher/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "easy_cipher"
|
8
|
+
spec.version = EasyCipher::VERSION
|
9
|
+
spec.authors = ["Bryan Hockey"]
|
10
|
+
spec.email = ["bhock@umich.edu"]
|
11
|
+
|
12
|
+
spec.summary = %q{Simple gem that uses ruby's OpenSSL package to simplify normal cipher use. }
|
13
|
+
spec.homepage = "https://github.com/malakai97/easy_cipher"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
16
|
+
spec.bindir = "exe"
|
17
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_development_dependency "bundler", "~> 1.8"
|
21
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
22
|
+
end
|
data/lib/easy_cipher.rb
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
require "openssl"
|
2
|
+
require "base64"
|
3
|
+
|
4
|
+
# The cipher class
|
5
|
+
class EasyCipher::Cipher
|
6
|
+
attr_reader :key, :iv
|
7
|
+
|
8
|
+
# Create a new cipher instance.
|
9
|
+
# @param key [String] A 256bit random key, or nil.
|
10
|
+
# @param iv [String] An initialization vector, or nil.
|
11
|
+
def initialize(key = nil, iv = nil)
|
12
|
+
if (key && !iv) || (!key && iv)
|
13
|
+
raise ArgumentError, "Must supply key AND iv, or neither."
|
14
|
+
end
|
15
|
+
|
16
|
+
@key = key
|
17
|
+
@iv = iv
|
18
|
+
|
19
|
+
@encryptor = ::OpenSSL::Cipher::AES256.new(:CBC)
|
20
|
+
@encryptor.encrypt
|
21
|
+
|
22
|
+
if @key
|
23
|
+
@encryptor.key = @key
|
24
|
+
else
|
25
|
+
@key = @encryptor.random_key
|
26
|
+
end
|
27
|
+
|
28
|
+
if @iv
|
29
|
+
@encryptor.iv = @iv
|
30
|
+
else
|
31
|
+
@iv = @encryptor.random_iv
|
32
|
+
end
|
33
|
+
|
34
|
+
@decryptor = ::OpenSSL::Cipher::AES256.new(:CBC)
|
35
|
+
@decryptor.decrypt
|
36
|
+
@decryptor.key = @key
|
37
|
+
@decryptor.iv = @iv
|
38
|
+
end
|
39
|
+
|
40
|
+
# Create a new cipher instance.
|
41
|
+
# @param key [String] A 256bit random key, base64 encoded.
|
42
|
+
# @param iv [String] An initialization vector, base64 encoded.
|
43
|
+
def self.new64(key, iv)
|
44
|
+
return self.new(Base64.decode64(key), Base64.decode64(iv))
|
45
|
+
end
|
46
|
+
|
47
|
+
# Return the key in base64 (e.g. to store in string fields)
|
48
|
+
def key64
|
49
|
+
return Base64.encode64(@key)
|
50
|
+
end
|
51
|
+
|
52
|
+
# Return the iv in base64 (e.g. to store in string fields)
|
53
|
+
def iv64
|
54
|
+
return Base64.encode64(@iv)
|
55
|
+
end
|
56
|
+
|
57
|
+
# Encrypt the given data.
|
58
|
+
# @param data [String] The data to encrypt
|
59
|
+
# @return [String] Base64 representation of the encrypted data.
|
60
|
+
def encrypt(data)
|
61
|
+
return Base64.encode64(encrypt_line(data, true))
|
62
|
+
end
|
63
|
+
|
64
|
+
|
65
|
+
# Decrypt the given data.
|
66
|
+
# @param encrypted_data [String] Base64 representation of the encrypted data.
|
67
|
+
# @return [String] The decrypted data.
|
68
|
+
def decrypt(encrypted_data)
|
69
|
+
return decrypt_line(Base64.decode64(encrypted_data))
|
70
|
+
end
|
71
|
+
|
72
|
+
|
73
|
+
|
74
|
+
protected
|
75
|
+
def encrypt_line(line, final = true)
|
76
|
+
output = @encryptor.update(line)
|
77
|
+
if final
|
78
|
+
output << @encryptor.final
|
79
|
+
end
|
80
|
+
return output
|
81
|
+
end
|
82
|
+
|
83
|
+
|
84
|
+
def decrypt_line(line, final = true)
|
85
|
+
output = @decryptor.update(line)
|
86
|
+
if final
|
87
|
+
output << @decryptor.final
|
88
|
+
end
|
89
|
+
return output
|
90
|
+
end
|
91
|
+
|
92
|
+
|
93
|
+
end
|
metadata
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: easy_cipher
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.9.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Bryan Hockey
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-05-07 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.8'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.8'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
description:
|
42
|
+
email:
|
43
|
+
- bhock@umich.edu
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- ".rspec"
|
50
|
+
- ".travis.yml"
|
51
|
+
- Gemfile
|
52
|
+
- README.md
|
53
|
+
- Rakefile
|
54
|
+
- easy_cipher.gemspec
|
55
|
+
- lib/easy_cipher.rb
|
56
|
+
- lib/easy_cipher/cipher.rb
|
57
|
+
- lib/easy_cipher/version.rb
|
58
|
+
homepage: https://github.com/malakai97/easy_cipher
|
59
|
+
licenses: []
|
60
|
+
metadata: {}
|
61
|
+
post_install_message:
|
62
|
+
rdoc_options: []
|
63
|
+
require_paths:
|
64
|
+
- lib
|
65
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
requirements: []
|
76
|
+
rubyforge_project:
|
77
|
+
rubygems_version: 2.4.5
|
78
|
+
signing_key:
|
79
|
+
specification_version: 4
|
80
|
+
summary: Simple gem that uses ruby's OpenSSL package to simplify normal cipher use.
|
81
|
+
test_files: []
|