class Eaesy::Cipher

Public Class Methods

new(key_plain) click to toggle source
# File lib/eaesy.rb, line 9
def initialize(key_plain)
  @cipherAlg = "aes-256-cbc"
  @key = sha256(key_plain)
end

Public Instance Methods

decrypt(encrypted_secret, iv) click to toggle source
# File lib/eaesy.rb, line 37
def decrypt(encrypted_secret, iv)
  # now we create a cipher for decrypting
  cipher = OpenSSL::Cipher.new(@cipherAlg)
  cipher.decrypt
  cipher.key = @key
  cipher.iv = iv.unpack('m')[0]

  # and decrypt it
  decrypted = cipher.update(encrypted_secret.unpack('m')[0])
  decrypted << cipher.final

  decrypted
end
encrypt(secret, iv = nil) click to toggle source
# File lib/eaesy.rb, line 14
def encrypt(secret, iv = nil)
  cipher = OpenSSL::Cipher.new(@cipherAlg)
  cipher.encrypt

  # you will need to store this and key for later, in order to decrypt your data
  iv = iv.unpack('m')[0] if iv
  iv = cipher.random_iv unless iv

  # load them into the cipher
  cipher.key = @key
  cipher.iv = iv

  secret = ' ' if (secret || '').length == 0
  # encrypt the message
  encrypted = cipher.update(secret)
  encrypted << cipher.final

  {
    encrypted_secret: [encrypted].pack('m'),
    iv: [iv].pack('m')
  }
end

Private Instance Methods

sha256(string_to_SHA) click to toggle source
# File lib/eaesy.rb, line 53
def sha256(string_to_SHA)
  sha256 = Digest::SHA256.new().digest(string_to_SHA)
end