Class: YamlZlibBlowfish

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

Constant Summary collapse

VERSION =
'2.0.250921'

Instance Method Summary collapse

Constructor Details

#initialize(passphrase) ⇒ YamlZlibBlowfish

yzb = YamlZlibBlowfish.new(passphrase)



12
13
14
15
16
# File 'lib/yaml_zlib_blowfish.rb', line 12

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)
end

Instance Method Details

#cipher(mode, data) ⇒ Object

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



20
21
22
23
24
25
# File 'lib/yaml_zlib_blowfish.rb', line 20

def cipher(mode, data)
  cipher         = OpenSSL::Cipher::BF.new(:CBC).send(mode)
  cipher.key_len = @key.length
  cipher.key     = @key
  cipher.update(data) + cipher.final
end

#decrypt(encrypted) ⇒ Object

plain_structure = yzb.decrypt(encrypted_compressed_dump)



28
29
30
# File 'lib/yaml_zlib_blowfish.rb', line 28

def decrypt(encrypted)
  YAML.load(Zlib::Inflate.inflate(cipher(:decrypt, encrypted)))
end

#dump(dumpfile, data) ⇒ Object

yzb.dump(filename, plain_structure)



43
44
45
# File 'lib/yaml_zlib_blowfish.rb', line 43

def dump(dumpfile, data)
  File.write(dumpfile, encrypt(data))
end

#encrypt(plain) ⇒ Object

encrypted_compressed_dump = yzb.encrypt(plain_structure)



33
34
35
# File 'lib/yaml_zlib_blowfish.rb', line 33

def encrypt(plain)
  cipher(:encrypt, Zlib::Deflate.deflate(YAML.dump(plain)))
end

#load(dumpfile) ⇒ Object

plain_structure = yzb.load(filename)



38
39
40
# File 'lib/yaml_zlib_blowfish.rb', line 38

def load(dumpfile)
  decrypt(File.read(dumpfile))
end