Class: RubeePass::Cipher

Inherits:
Object
  • Object
show all
Defined in:
lib/rubeepass/cipher.rb

Defined Under Namespace

Modules: ID

Instance Method Summary collapse

Constructor Details

#initialize(id, iv, key) ⇒ Cipher

Returns a new instance of Cipher.



52
53
54
55
56
# File 'lib/rubeepass/cipher.rb', line 52

def initialize(id, iv, key)
    @id = id
    @iv = iv
    @key = key
end

Instance Method Details

#decrypt(enc) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# 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