class CredStash::Cipher

Public Class Methods

new(key) click to toggle source
# File lib/cred_stash/cipher.rb, line 4
def initialize(key)
  @key = key
end

Public Instance Methods

decrypt(value) click to toggle source
# File lib/cred_stash/cipher.rb, line 12
def decrypt(value)
  run(mode: :decrypt, value: value).force_encoding("UTF-8")
end
encrypt(value) click to toggle source
# File lib/cred_stash/cipher.rb, line 8
def encrypt(value)
  run(mode: :encrypt, value: value)
end

Private Instance Methods

run(mode:, value:) click to toggle source
# File lib/cred_stash/cipher.rb, line 18
def run(mode:, value:)
  cipher = OpenSSL::Cipher::AES.new(256, "CTR")

  case mode
  when :encrypt
    cipher.encrypt
  when :decrypt
    cipher.decrypt
  else
    raise ArgumentError, "Unknown mode: #{mode}"
  end

  cipher.key = @key
  # FIXME It is better to generate and store initial counter
  cipher.iv = %w(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1).map(&:hex).pack('C' * 16)
  cipher.update(value) + cipher.final
end