diff options
| author | Jesse Morgan <jesse@jesterpm.net> | 2015-01-22 20:35:20 -0800 | 
|---|---|---|
| committer | Jesse Morgan <jesse@jesterpm.net> | 2015-01-22 20:35:20 -0800 | 
| commit | 0c360e09842489b076ee8e6835532715f0b2424f (patch) | |
| tree | 83c0f2077537c7684f16031a109b2872ae71bb3c /lib/insomniaircd_conf | |
| parent | ddcf81162f68b17f5184104109c54f27d3a7d5a9 (diff) | |
Adding lib/
Diffstat (limited to 'lib/insomniaircd_conf')
| -rw-r--r-- | lib/insomniaircd_conf/secure.rb | 35 | 
1 files changed, 35 insertions, 0 deletions
| diff --git a/lib/insomniaircd_conf/secure.rb b/lib/insomniaircd_conf/secure.rb new file mode 100644 index 0000000..91ab9e7 --- /dev/null +++ b/lib/insomniaircd_conf/secure.rb @@ -0,0 +1,35 @@ +require 'base64' + +class Secure + +  def initialize(key) +    @key = Base64.decode64(key) +  end + +  def self.random_key +    cipher = OpenSSL::Cipher::AES256.new(:CBC) +    Base64.encode64(cipher.random_key).gsub("\n", "") +  end + +  def encrypt(secret) +    cipher = OpenSSL::Cipher::AES256.new(:CBC) +    cipher.encrypt +    cipher.key = @key +    iv = cipher.random_iv +    encrypted = cipher.update(secret) + cipher.final + +    Base64.encode64(iv + encrypted).gsub("\n", "") +  end + +  def decrypt(input) +    data = Base64.decode64(input) +    cipher = OpenSSL::Cipher::AES256.new(:CBC) +    cipher.decrypt +    cipher.key = @key +    cipher.iv = data[0..15] + +    cipher.update(data[16..-1]) + cipher.final +  end + +end + | 
