module Noteshred::Crypto::V4

Constants

ITERATIONS

Public Class Methods

decrypt(content,pass,salt,iv) click to toggle source

Expects UTF-8 encoded strings from the encrypt method

# File lib/noteshred/crypto.rb, line 29
def self.decrypt(content,pass,salt,iv)
  content    = Noteshred::Tools.decode_utf8(content)
  cipher     = OpenSSL::Cipher.new('AES-256-CBC')
  cipher.decrypt
  cipher.iv  = Noteshred::Tools.decode_utf8(iv)
  cipher.key = OpenSSL::PKCS5.pbkdf2_hmac_sha1(pass, salt, ITERATIONS, cipher.key_len)
  result     = cipher.update(content)
  result << cipher.final
end
encrypt(content,pass) click to toggle source

Outputs UTF-8 encoded object for storing in database

# File lib/noteshred/crypto.rb, line 11
def self.encrypt(content,pass)
  raise ArgumentError, 'Content and password required' if content.empty? || pass.empty?
  cipher     = OpenSSL::Cipher.new('AES-256-CBC')
  cipher.encrypt
  iv         = cipher.random_iv
  salt       = SecureRandom.hex(16)
  cipher.key = OpenSSL::PKCS5.pbkdf2_hmac_sha1(pass, salt, ITERATIONS, cipher.key_len)
  result     = cipher.update(content)
  result << cipher.final
  return {
    :content => Noteshred::Tools.encode_utf8(result),
    :iv      => Noteshred::Tools.encode_utf8(iv),
    :salt    => salt,
    :version => 4
  }
end
hash(pass,salt) click to toggle source
# File lib/noteshred/crypto.rb, line 39
def self.hash(pass,salt)
  return OpenSSL::PKCS5::pbkdf2_hmac_sha1(pass, salt, ITERATIONS, 32)
end