symmetric-encryption 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
data/lib/symmetric/encryption.rb
CHANGED
@@ -55,9 +55,8 @@ module Symmetric
|
|
55
55
|
self.key = symmetric_key
|
56
56
|
self.iv = symmetric_iv
|
57
57
|
else
|
58
|
-
load_keys(config['
|
58
|
+
load_keys(config['symmetric_key_filename'], config['symmetric_iv_filename'], config['private_rsa_key'])
|
59
59
|
end
|
60
|
-
|
61
60
|
end
|
62
61
|
|
63
62
|
# Load the symmetric key to use for encrypting and decrypting data
|
@@ -76,35 +75,39 @@ module Symmetric
|
|
76
75
|
nil
|
77
76
|
end
|
78
77
|
|
79
|
-
# Generate new random keys for use with this Encryption library
|
80
|
-
#
|
81
|
-
# Creates:
|
82
|
-
# 2048 bit Private Key private.key
|
83
|
-
# 2048 bit Public Key public.key
|
78
|
+
# Generate new random symmetric keys for use with this Encryption library
|
84
79
|
#
|
85
|
-
#
|
80
|
+
# Creates Symmetric Key .key
|
86
81
|
# and initilization vector .iv
|
87
82
|
# which is encrypted with the above Public key
|
88
83
|
#
|
89
84
|
# Note: Existing files will be overwritten
|
90
|
-
def self.
|
91
|
-
# Generate
|
92
|
-
new_key = OpenSSL::PKey::RSA.generate(2048)
|
85
|
+
def self.generate_symmetric_key_files(filename=nil, environment=nil)
|
86
|
+
# Temporary: Generate private key manually for now. Will automate soon.
|
87
|
+
#new_key = OpenSSL::PKey::RSA.generate(2048)
|
88
|
+
|
89
|
+
filename ||= File.join(Rails.root, "config", "symmetric-encryption.yml")
|
90
|
+
environment ||= (Rails.env || ENV['RAILS'])
|
91
|
+
config = YAML.load_file(filename)[environment]
|
92
|
+
|
93
|
+
raise "Missing mandatory 'key_filename' for environment:#{environment} in #{filename}" unless key_filename = config['symmetric_key_filename']
|
94
|
+
iv_filename = config['symmetric_iv_filename']
|
95
|
+
raise "Missing mandatory 'private_key' for environment:#{environment} in #{filename}" unless private_key = config['private_rsa_key']
|
96
|
+
rsa_key = OpenSSL::PKey::RSA.new(private_key)
|
97
|
+
|
93
98
|
# To ensure compatibility with C openssl code, remove RSA from pub file headers
|
94
|
-
|
95
|
-
File.open(File.join(rsa_keys_path, 'public.key'), 'w') {|file| file.write(pub_key)}
|
96
|
-
File.open(File.join(rsa_keys_path, 'private.key'), 'w') {|file| file.write(new_key.to_pem)}
|
99
|
+
#File.open(File.join(rsa_keys_path, 'private.key'), 'w') {|file| file.write(new_key.to_pem)}
|
97
100
|
|
98
101
|
# Generate Symmetric Key
|
99
|
-
|
100
|
-
|
101
|
-
@@key =
|
102
|
-
@@iv =
|
102
|
+
openssl_cipher = OpenSSL::Cipher::Cipher.new(config['cipher'] || 'aes-256-cbc')
|
103
|
+
openssl_cipher.encrypt
|
104
|
+
@@key = openssl_cipher.random_key
|
105
|
+
@@iv = openssl_cipher.random_iv if iv_filename
|
103
106
|
|
104
107
|
# Save symmetric key after encrypting it with the private asymmetric key
|
105
|
-
File.open(
|
106
|
-
File.open(
|
107
|
-
|
108
|
+
File.open(key_filename, 'wb') {|file| file.write( rsa_key.public_encrypt(@@key) ) }
|
109
|
+
File.open(iv_filename, 'wb') {|file| file.write( rsa_key.public_encrypt(@@iv) ) } if iv_filename
|
110
|
+
puts("Generated new Symmetric Key for encryption. Please copy #{key_filename} and #{iv_filename} to the other web servers in #{environment}.")
|
108
111
|
end
|
109
112
|
|
110
113
|
# Generate a 22 character random password
|
@@ -1,13 +1,13 @@
|
|
1
|
-
namespace
|
1
|
+
namespace :symmetric_encryption do
|
2
2
|
|
3
|
-
desc 'Decrypt the supplied string. Example: VALUE="
|
4
|
-
task :decrypt do
|
3
|
+
desc 'Decrypt the supplied string. Example: VALUE="_encrypted_string_" rake symmetric_encryption:decrypt'
|
4
|
+
task :decrypt => :environment do
|
5
5
|
puts "\nEncrypted: #{ENV['VALUE']}"
|
6
6
|
puts "Decrypted: #{Symmetric::Encryption.decrypt(ENV['VALUE'])}\n\n"
|
7
7
|
end
|
8
8
|
|
9
|
-
desc 'Encrypt a value, such as a password. Example: rake
|
10
|
-
task :encrypt do
|
9
|
+
desc 'Encrypt a value, such as a password. Example: rake symmetric_encryption:encrypt'
|
10
|
+
task :encrypt => :environment do
|
11
11
|
require 'highline'
|
12
12
|
password1 = nil
|
13
13
|
password2 = 0
|
@@ -23,8 +23,13 @@ namespace 'symmetric-encryption' do
|
|
23
23
|
puts "\nEncrypted: #{Symmetric::Encryption.encrypt(password1)}\n\n"
|
24
24
|
end
|
25
25
|
|
26
|
-
desc 'Generate
|
27
|
-
task :
|
26
|
+
desc 'Generate new Symmetric key and initialization vector. Example: RAILS_ENV=production rake symmetric_encryption:generate_symmetric_keys'
|
27
|
+
task :generate_symmetric_keys do
|
28
|
+
Symmetric::Encryption.generate_symmetric_key_files
|
29
|
+
end
|
30
|
+
|
31
|
+
desc 'Generate a random password and display its encrypted form. Example: rake symmetric_encryption:random_password'
|
32
|
+
task :random_password => :environment do
|
28
33
|
p = Symmetric::Encryption.random_password
|
29
34
|
puts "\nGenerated Password: #{p}"
|
30
35
|
puts "Encrypted: #{Symmetric::Encryption.encrypt(p)}\n\n"
|
data/lib/symmetric/version.rb
CHANGED
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: symmetric-encryption
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.1.
|
5
|
+
version: 0.1.1
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Reid Morrison
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2012-01-
|
13
|
+
date: 2012-01-19 00:00:00 Z
|
14
14
|
dependencies: []
|
15
15
|
|
16
16
|
description: Symmetric Encryption is a library to seamlessly enable symmetric encryption in a project, written in Ruby.
|