class RubeePass::Cipher

Public Class Methods

new(id, iv, key) click to toggle source
# File lib/rubeepass/cipher.rb, line 52
def initialize(id, iv, key)
    @id = id
    @iv = iv
    @key = key
end

Public Instance Methods

decrypt(enc) click to toggle source
# File lib/rubeepass/cipher.rb, line 12
def decrypt(enc)
    # Setup
    case @id
    when ID::AES
        cipher = OpenSSL::Cipher::AES.new(256, :CBC)
    when ID::CHACHA20
        cipher = OpenSSL::Cipher.new("chacha20")
    when ID::TWOFISH
        cipher = Twofish.new(
            @key,
            {
                :iv => @iv,
                :mode => :cbc,
                :padding => :none
            }
        )
    else
        raise RubeePass::Error::NotSupported.new
    end

    # Decrypt
    case @id
    when ID::AES, ID::CHACHA20
        begin
            cipher.decrypt
            cipher.key = @key
            cipher.iv = @iv
            return StringIO.new(cipher.update(enc) + cipher.final)
        rescue OpenSSL::Cipher::CipherError
            raise RubeePass::Error::InvalidPassword.new
        end
    when ID::TWOFISH
        begin
            return StringIO.new(cipher.decrypt(enc))
        rescue ArgumentError
            raise RubeePass::Error::InvalidPassword.new
        end
    end
end