Class: Shh::BlowfishSerialiser

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

Instance Method Summary collapse

Constructor Details

#initialize(serialiser, passphrase) ⇒ BlowfishSerialiser

Returns a new instance of BlowfishSerialiser.



5
6
7
8
# File 'lib/shh/blowfish_serialiser.rb', line 5

def initialize serialiser, passphrase
  @blowfish = ::Crypt::Blowfish.new(passphrase)
  @serialiser = serialiser
end

Instance Method Details

#read(io) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/shh/blowfish_serialiser.rb', line 10

def read io
  decrypted_io = StringIO.new
  while l = io.read(8) do
    decrypted_io.print @blowfish.decrypt_block(l)
  end
  @serialiser.read StringIO.new(decrypted_io.string.gsub("\0",''))
end

#write(io, hash) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/shh/blowfish_serialiser.rb', line 18

def write io, hash
  serialised_io = StringIO.new
  @serialiser.write serialised_io, hash
  serialised_io.rewind
  while l = serialised_io.read(8) do
    while l.size < 8 do l += "\0" end
    io.print @blowfish.encrypt_block(l)
  end
end