conceal 0.1.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/.gitignore +22 -0
- data/.rspec +2 -0
- data/.travis.yml +4 -0
- data/Gemfile +6 -0
- data/LICENSE +22 -0
- data/README.md +33 -0
- data/Rakefile +7 -0
- data/bin/decrypt +42 -0
- data/bin/encrypt +42 -0
- data/conceal.gemspec +26 -0
- data/lib/conceal/version.rb +3 -0
- data/lib/conceal.rb +91 -0
- data/spec/conceal_spec.rb +26 -0
- data/spec/spec_helper.rb +2 -0
- metadata +115 -0
data/.gitignore
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Ben Scott
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# Conceal
|
2
|
+
|
3
|
+
Simple OpenSSL-based string encryption using a shared secret. The algorithm, initialization vector, salt, crypttext, and HMAC are all encoded into a single string
|
4
|
+
so it is easy to copy around.
|
5
|
+
|
6
|
+
## Requirements
|
7
|
+
|
8
|
+
* Ruby 1.9.3 or newer
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
Add this line to your application's Gemfile:
|
13
|
+
|
14
|
+
gem 'conceal'
|
15
|
+
|
16
|
+
And then execute:
|
17
|
+
|
18
|
+
$ bundle
|
19
|
+
|
20
|
+
Or install it yourself as:
|
21
|
+
|
22
|
+
$ gem install conceal
|
23
|
+
|
24
|
+
## Usage
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
encrypted = Conceal.encrypt('some plaintext', key: 'your shared secret', algorithm: 'aes-256-cbc')
|
28
|
+
decrypted = Conceal.decrypt(encrypted, key: 'your shared secret')
|
29
|
+
```
|
30
|
+
|
31
|
+
## Authors
|
32
|
+
|
33
|
+
* Ben Scott (<gamepoet@gmail.com>)
|
data/Rakefile
ADDED
data/bin/decrypt
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'conceal'
|
3
|
+
require 'optparse'
|
4
|
+
|
5
|
+
class Options < Struct.new(:trailing_newline)
|
6
|
+
end
|
7
|
+
|
8
|
+
def main
|
9
|
+
opts = Options.new
|
10
|
+
opts.trailing_newline = true
|
11
|
+
|
12
|
+
OptionParser.new do |o|
|
13
|
+
o.banner = 'Usage: decrypt KEY_FILE'
|
14
|
+
o.on('-h', '--help', 'show this message') do
|
15
|
+
puts o
|
16
|
+
exit(0)
|
17
|
+
end
|
18
|
+
o.on('-n', "don't print the trailing newline") do
|
19
|
+
opts.trailing_newline = false
|
20
|
+
end
|
21
|
+
o.on('-v', '--version', 'print the version') do
|
22
|
+
puts Conceal::VERSION
|
23
|
+
end
|
24
|
+
end.parse!(ARGV)
|
25
|
+
|
26
|
+
# read the key file
|
27
|
+
if ARGV.empty?
|
28
|
+
$stderr.puts "Missing KEY_FILE argument"
|
29
|
+
exit(1)
|
30
|
+
end
|
31
|
+
key_file = ARGV.shift
|
32
|
+
key = IO.read(key_file)
|
33
|
+
|
34
|
+
# decrypt from stdin
|
35
|
+
encrypted_data = $stdin.read
|
36
|
+
plaintext = Conceal.decrypt(encrypted_data, key: key)
|
37
|
+
|
38
|
+
$stdout.write(plaintext)
|
39
|
+
$stdout.write("\n") if opts.trailing_newline
|
40
|
+
end
|
41
|
+
|
42
|
+
main
|
data/bin/encrypt
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'conceal'
|
3
|
+
require 'optparse'
|
4
|
+
|
5
|
+
class Options < Struct.new(:trailing_newline)
|
6
|
+
end
|
7
|
+
|
8
|
+
def main
|
9
|
+
opts = Options.new
|
10
|
+
opts.trailing_newline = true
|
11
|
+
|
12
|
+
OptionParser.new do |o|
|
13
|
+
o.banner = 'Usage: encrypt KEY_FILE'
|
14
|
+
o.on('-h', '--help', 'show this message') do
|
15
|
+
puts o
|
16
|
+
exit(0)
|
17
|
+
end
|
18
|
+
o.on('-n', "don't print the trailing newline") do
|
19
|
+
opts.trailing_newline = false
|
20
|
+
end
|
21
|
+
o.on('-v', '--version', 'print the version') do
|
22
|
+
puts Conceal::VERSION
|
23
|
+
end
|
24
|
+
end.parse!(ARGV)
|
25
|
+
|
26
|
+
# read the key file
|
27
|
+
if ARGV.empty?
|
28
|
+
$stderr.puts "Missing KEY_FILE argument"
|
29
|
+
exit(1)
|
30
|
+
end
|
31
|
+
key_file = ARGV.shift
|
32
|
+
key = IO.read(key_file)
|
33
|
+
|
34
|
+
# encrypt from stdin
|
35
|
+
plaintext = $stdin.read
|
36
|
+
encrypted_data = Conceal.encrypt(plaintext, key: key)
|
37
|
+
|
38
|
+
$stdout.write(encrypted_data)
|
39
|
+
$stdout.write("\n") if opts.trailing_newline
|
40
|
+
end
|
41
|
+
|
42
|
+
main
|
data/conceal.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'conceal/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'conceal'
|
8
|
+
spec.version = Conceal::VERSION
|
9
|
+
spec.authors = ['Ben Scott']
|
10
|
+
spec.email = ['gamepoet@gmail.com']
|
11
|
+
spec.summary = 'Simple OpenSSL-based string encryption.'
|
12
|
+
spec.description = 'Encrypts and decrypts strings using OpenSSL.'
|
13
|
+
spec.homepage = 'https://github.com/gamepoet/conceal'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ['lib']
|
20
|
+
|
21
|
+
spec.required_ruby_version = '>= 1.9.3'
|
22
|
+
|
23
|
+
spec.add_development_dependency 'bundler', '~> 1.6'
|
24
|
+
spec.add_development_dependency 'rake', '>= 0.8.7'
|
25
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
26
|
+
end
|
data/lib/conceal.rb
ADDED
@@ -0,0 +1,91 @@
|
|
1
|
+
require 'conceal/version'
|
2
|
+
require 'base64'
|
3
|
+
require 'openssl'
|
4
|
+
require 'securerandom'
|
5
|
+
|
6
|
+
module Conceal
|
7
|
+
class << self
|
8
|
+
FORMAT_VERSION = 1
|
9
|
+
FIELD_SEPARATOR = ':'
|
10
|
+
|
11
|
+
# Encrypts the given plaintext string.
|
12
|
+
#
|
13
|
+
# @param plaintext [String] the plaintext string to encrypt
|
14
|
+
# @param opts [Hash] additional options
|
15
|
+
#
|
16
|
+
# @option opts [String] :algorithm the cipher algorithm to use (defaults to 'aes-25c-cbc')
|
17
|
+
# @option opts [String] :key the secret shared key
|
18
|
+
def encrypt(plaintext, opts = {})
|
19
|
+
opts = {
|
20
|
+
algorithm: 'aes-256-cbc',
|
21
|
+
}.merge(opts)
|
22
|
+
key = opts[:key]
|
23
|
+
algorithm = opts[:algorithm]
|
24
|
+
raise ArgumentError.new(':key option missing') if key.to_s.empty?
|
25
|
+
salt = SecureRandom.hex(128)
|
26
|
+
|
27
|
+
# setup the cipher
|
28
|
+
cipher = OpenSSL::Cipher::Cipher.new(algorithm)
|
29
|
+
cipher.encrypt
|
30
|
+
iv = cipher.random_iv
|
31
|
+
cipher.iv = iv
|
32
|
+
cipher.key = OpenSSL::PKCS5.pbkdf2_hmac_sha1(key, salt, 2000, cipher.key_len)
|
33
|
+
|
34
|
+
# encrypt
|
35
|
+
ciphertext = cipher.update(plaintext)
|
36
|
+
ciphertext << cipher.final
|
37
|
+
|
38
|
+
# MAC
|
39
|
+
digest = OpenSSL::Digest.new('sha256')
|
40
|
+
hmac = OpenSSL::HMAC.digest(digest, key, ciphertext)
|
41
|
+
|
42
|
+
[
|
43
|
+
FORMAT_VERSION,
|
44
|
+
algorithm,
|
45
|
+
encode64(iv),
|
46
|
+
encode64(salt),
|
47
|
+
encode64(hmac),
|
48
|
+
encode64(ciphertext),
|
49
|
+
].join(FIELD_SEPARATOR)
|
50
|
+
end
|
51
|
+
|
52
|
+
# Decrypts the given encrypted string.
|
53
|
+
#
|
54
|
+
# @param data [String] the encrypted string to decrypt
|
55
|
+
# @param opts [Hash] additional options
|
56
|
+
#
|
57
|
+
# @option opts [String] :key the secret shared key
|
58
|
+
def decrypt(data, opts = {})
|
59
|
+
key = opts[:key]
|
60
|
+
raise ArgumentError.new(':key option missing') if key.to_s.empty?
|
61
|
+
|
62
|
+
ver, algorithm, iv64, salt64, hmac64, ciphertext64 = data.split(FIELD_SEPARATOR, 6)
|
63
|
+
raise ArgumentError.new('ciphertext has unknown version') unless ver == FORMAT_VERSION.to_s
|
64
|
+
|
65
|
+
iv = Base64.decode64(iv64)
|
66
|
+
salt = Base64.decode64(salt64)
|
67
|
+
hmac = Base64.decode64(hmac64)
|
68
|
+
ciphertext = Base64.decode64(ciphertext64)
|
69
|
+
|
70
|
+
# validate the hmac
|
71
|
+
digest = OpenSSL::Digest.new('sha256')
|
72
|
+
actual_hmac = OpenSSL::HMAC.digest(digest, key, ciphertext)
|
73
|
+
raise ArgumentError.new('HMAC mismatch') unless actual_hmac == hmac
|
74
|
+
|
75
|
+
# decrypt
|
76
|
+
cipher = OpenSSL::Cipher::Cipher.new(algorithm)
|
77
|
+
cipher.decrypt
|
78
|
+
cipher.iv = iv
|
79
|
+
cipher.key = OpenSSL::PKCS5.pbkdf2_hmac_sha1(key, salt, 2000, cipher.key_len)
|
80
|
+
|
81
|
+
plaintext = cipher.update(ciphertext)
|
82
|
+
plaintext << cipher.final
|
83
|
+
plaintext
|
84
|
+
end
|
85
|
+
|
86
|
+
private
|
87
|
+
def encode64(value)
|
88
|
+
Base64.encode64(value).gsub(/[\r\n]/, '')
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Conceal do
|
4
|
+
let(:key) { SecureRandom.hex(128) }
|
5
|
+
|
6
|
+
it 'has a version number' do
|
7
|
+
expect(Conceal::VERSION).not_to be nil
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'encrypt then decrypt returns the same original plaintext' do
|
11
|
+
expect(Conceal.decrypt(Conceal.encrypt('hello', key: key), key: key)).to eq('hello')
|
12
|
+
end
|
13
|
+
|
14
|
+
describe '#encrypt' do
|
15
|
+
it 'does not return the plaintext' do
|
16
|
+
expect(Conceal.encrypt('hello', key: key)).not_to eq('hello')
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'outputs different values each time (different iv/salt)' do
|
20
|
+
first = Conceal.encrypt('hello', key: key)
|
21
|
+
second = Conceal.encrypt('hello', key: key)
|
22
|
+
|
23
|
+
expect(first).not_to eq(second)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: conceal
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Ben Scott
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2014-08-26 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: bundler
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.6'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.6'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: 0.8.7
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 0.8.7
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rspec
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ~>
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '3.0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.0'
|
62
|
+
description: Encrypts and decrypts strings using OpenSSL.
|
63
|
+
email:
|
64
|
+
- gamepoet@gmail.com
|
65
|
+
executables:
|
66
|
+
- decrypt
|
67
|
+
- encrypt
|
68
|
+
extensions: []
|
69
|
+
extra_rdoc_files: []
|
70
|
+
files:
|
71
|
+
- .gitignore
|
72
|
+
- .rspec
|
73
|
+
- .travis.yml
|
74
|
+
- Gemfile
|
75
|
+
- LICENSE
|
76
|
+
- README.md
|
77
|
+
- Rakefile
|
78
|
+
- bin/decrypt
|
79
|
+
- bin/encrypt
|
80
|
+
- conceal.gemspec
|
81
|
+
- lib/conceal.rb
|
82
|
+
- lib/conceal/version.rb
|
83
|
+
- spec/conceal_spec.rb
|
84
|
+
- spec/spec_helper.rb
|
85
|
+
homepage: https://github.com/gamepoet/conceal
|
86
|
+
licenses:
|
87
|
+
- MIT
|
88
|
+
post_install_message:
|
89
|
+
rdoc_options: []
|
90
|
+
require_paths:
|
91
|
+
- lib
|
92
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
93
|
+
none: false
|
94
|
+
requirements:
|
95
|
+
- - ! '>='
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: 1.9.3
|
98
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
99
|
+
none: false
|
100
|
+
requirements:
|
101
|
+
- - ! '>='
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
segments:
|
105
|
+
- 0
|
106
|
+
hash: 3065879142632505148
|
107
|
+
requirements: []
|
108
|
+
rubyforge_project:
|
109
|
+
rubygems_version: 1.8.23.2
|
110
|
+
signing_key:
|
111
|
+
specification_version: 3
|
112
|
+
summary: Simple OpenSSL-based string encryption.
|
113
|
+
test_files:
|
114
|
+
- spec/conceal_spec.rb
|
115
|
+
- spec/spec_helper.rb
|