Sha256: b88f99a765d3806f23af49174a5e3f51c666dfd2de82203b59bae3010295a86f

Contents?: true

Size: 712 Bytes

Versions: 8

Compression:

Stored size: 712 Bytes

Contents

module Slosilo
  class Symmetric
    def initialize
      @cipher = OpenSSL::Cipher.new 'AES-256-CBC'
    end
    
    def encrypt plaintext, opts = {}
      @cipher.reset
      @cipher.encrypt
      @cipher.key = opts[:key]
      @cipher.iv = iv = random_iv
      ctxt = @cipher.update(plaintext)
      iv + ctxt + @cipher.final
    end
    
    def decrypt ciphertext, opts = {}
      @cipher.reset
      @cipher.decrypt
      @cipher.key = opts[:key]
      @cipher.iv, ctxt = ciphertext.unpack("a#{@cipher.iv_len}a*")
      ptxt = @cipher.update(ctxt)
      ptxt + @cipher.final
    end
    
    def random_iv
      @cipher.random_iv
    end
    
    def random_key
      @cipher.random_key
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
slosilo-1.1.0 lib/slosilo/symmetric.rb
slosilo-1.0.0 lib/slosilo/symmetric.rb
slosilo-0.4.1 lib/slosilo/symmetric.rb
slosilo-0.4.0 lib/slosilo/symmetric.rb
slosilo-0.2.4 lib/slosilo/symmetric.rb
slosilo-0.2.3 lib/slosilo/symmetric.rb
slosilo-0.2.2 lib/slosilo/symmetric.rb
slosilo-0.1.2 lib/slosilo/symmetric.rb