class Symmetric::Safe

Public Instance Methods

close(pwd) click to toggle source
# File lib/cryptrb/safe.rb, line 10
def close(pwd)
        cipher = OpenSSL::Cipher::AES256.new(:CBC)
        cipher.encrypt
        @iv = cipher.random_iv

        @salt = OpenSSL::Random.random_bytes 16
        @iter = 20000
        key_len = cipher.key_len
        digest = OpenSSL::Digest::SHA256.new

        key = OpenSSL::PKCS5.pbkdf2_hmac(pwd, @salt, @iter, key_len, digest)
        cipher.key = key
        @data = cipher.update(@data) + cipher.final
end
fill(data) click to toggle source
# File lib/cryptrb/safe.rb, line 6
def fill(data)
        @data = data
end
open(pwd) click to toggle source
# File lib/cryptrb/safe.rb, line 25
def open(pwd)
        decipher = OpenSSL::Cipher::AES256.new(:CBC)
        decipher.decrypt
        decipher.iv = @iv

        key_len = decipher.key_len
        digest = OpenSSL::Digest::SHA256.new

        key = OpenSSL::PKCS5.pbkdf2_hmac(pwd, @salt, @iter, key_len, digest)
        decipher.key = key
        @data = decipher.update(@data) + decipher.final
end