class Tome::Crypt

Public Class Methods

decrypt(opts = {}) click to toggle source
# File lib/tome/crypt.rb, line 10
def self.decrypt(opts = {})
  crypt :decrypt, opts
end
encrypt(opts = {}) click to toggle source
# File lib/tome/crypt.rb, line 6
def self.encrypt(opts = {})
  crypt :encrypt, opts
end
new_iv() click to toggle source
# File lib/tome/crypt.rb, line 14
def self.new_iv
  new_cipher.random_iv
end
new_salt() click to toggle source
# File lib/tome/crypt.rb, line 18
def self.new_salt
  SecureRandom.uuid
end

Private Class Methods

crypt(method, opts) click to toggle source
# File lib/tome/crypt.rb, line 27
def self.crypt(method, opts)
  raise ArgumentError if
    opts.nil? || opts.empty? || opts[:value].nil? ||
    opts[:password].nil? || opts[:password].empty? ||
    opts[:salt].nil? || opts[:salt].empty? ||
    opts[:iv].nil? || opts[:iv].empty? ||
    opts[:stretch].nil? || opts[:stretch].nil?

  cipher = new_cipher
  cipher.send(method)

  cipher.key = crypt_key(opts)
  cipher.iv = opts[:iv]

  result = cipher.update(opts[:value])
  result << cipher.final
  return result
end
crypt_key(opts) click to toggle source
# File lib/tome/crypt.rb, line 46
def self.crypt_key(opts)
  password = opts[:password]
  salt = opts[:salt]
  iterations = opts[:stretch]
  key_length = 32 # 256 bits
  hash = OpenSSL::Digest::SHA512.new

  return OpenSSL::PKCS5.pbkdf2_hmac(password, salt, iterations, key_length, hash)
end
new_cipher() click to toggle source
# File lib/tome/crypt.rb, line 23
def self.new_cipher
  OpenSSL::Cipher::AES.new(256, :CBC)
end