class YamlZlibBlowfish

Constants

VERSION

Public Class Methods

new(passphrase) click to toggle source

yzb = YamlZlibBlowfish.new(passphrase)

# File lib/yaml_zlib_blowfish.rb, line 11
def initialize(passphrase)
  # Blowfish takes a variable length key from 32 to 448 bits.
  # Here we create a 256 bit key based on the pass-phrase:
  @key    = Digest::SHA256.digest(passphrase)
  @cipher = OpenSSL::Cipher::BF.new(:CBC)
end

Public Instance Methods

cipher(mode, data) click to toggle source

encrypted_string = yzb.cipher(:encrypt, plain_string) plain_string = yzb.cipher(:decrypt, encrypted_string)

# File lib/yaml_zlib_blowfish.rb, line 20
def cipher(mode, data)
  cipher         = @cipher.send(mode)
  cipher.key_len = @key.length
  cipher.key     = @key
  cipher.update(data) << cipher.final
end
decrypt(encrypted) click to toggle source

plain_structure = yzb.decrypt(encrypted_compressed_dump)

# File lib/yaml_zlib_blowfish.rb, line 28
def decrypt(encrypted)
  YAML.load(Zlib::Inflate.inflate(cipher(:decrypt, encrypted)))
end
dump(dumpfile, data) click to toggle source

yzb.dump(filename, plain_structure)

# File lib/yaml_zlib_blowfish.rb, line 43
def dump(dumpfile, data)
  File.write(dumpfile, encrypt(data))
end
encrypt(plain) click to toggle source

encrypted_compressed_dump = yzb.encrypt(plain_structure)

# File lib/yaml_zlib_blowfish.rb, line 33
def encrypt(plain)
  cipher(:encrypt, Zlib::Deflate.deflate(YAML.dump(plain)))
end
load(dumpfile) click to toggle source

plain_structure = yzb.load(filename)

# File lib/yaml_zlib_blowfish.rb, line 38
def load(dumpfile)
  decrypt(File.read(dumpfile))
end