class FunWith::Passwords::Crypt

Constants

IV

Public Class Methods

decrypt( encrypted_message, key ) click to toggle source
# File lib/fun_with/passwords/crypt.rb, line 6
def self.decrypt( encrypted_message, key )
  cipher = OpenSSL::Cipher::AES256.new( :CBC )
  cipher.decrypt
  cipher.key = self.stretch_key(key)
  cipher.iv  = IV
  
  msg = cipher.update( encrypted_message )
  msg << cipher.final
  msg
end
encrypt( plaintext, key ) click to toggle source
# File lib/fun_with/passwords/crypt.rb, line 17
def self.encrypt( plaintext, key )
  cipher = OpenSSL::Cipher::AES256.new( :CBC )
  cipher.encrypt
  cipher.key = self.stretch_key(key)
  cipher.iv  = IV
  
  encrypted_message = cipher.update( plaintext )
  encrypted_message << cipher.final
  encrypted_message
end

Protected Class Methods

stretch_key( key ) click to toggle source

Only advantage of doing this is lengthening short, insecure master passwords to randomish-looking key of length needed by the crypto cipher. Short passwords? Still insecure. Film at 11.

# File lib/fun_with/passwords/crypt.rb, line 33
def self.stretch_key( key )
  (Digest::MD5.hexdigest(key) + Digest::MD5.hexdigest(key.reverse) ).to_i(16).to_s(36)
end