Class: Kingdom::Box

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

Instance Method Summary collapse

Constructor Details

#initialize(k) ⇒ Box

Returns a new instance of Box.



23
24
25
# File 'lib/kingdom/box.rb', line 23

def initialize k
  @key = OpenSSL::Digest::SHA256.digest(k)
end

Instance Method Details

#<<(i) ⇒ Object



34
35
36
# File 'lib/kingdom/box.rb', line 34

def << i
  encrypt(i)
end

#[](i) ⇒ Object



47
48
49
# File 'lib/kingdom/box.rb', line 47

def [] i
  decrypt(i)
end

#decrypt(c) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/kingdom/box.rb', line 37

def decrypt c
  d = Kingdom.decode(c)
  iv = d[0..15]
  e = d[16..-1]
  y = Kingdom.cipher
  y.decrypt
  y.key = @key
  y.iv = iv
  y.update(e) + y.final
end

#encrypt(s) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/kingdom/box.rb', line 26

def encrypt s
  c = Kingdom.cipher
  c.encrypt
  c.key = @key
  iv = c.random_iv
  e = c.update(s) + c.final
  Kingdom.encode(iv + e)
end