Class: AttrEncrypter

Inherits:
Object
  • Object
show all
Defined in:
lib/attr_encrypter.rb,
lib/attr_encrypter/version.rb

Defined Under Namespace

Modules: Accessors, Errors, Generator Classes: Boxes

Constant Summary collapse

DIGEST_FORMAT =
/^(\d+)\.([a-zA-Z0-9\+\=\n\/]+)$/.freeze
VERSION =
"1.0.0"

Instance Method Summary collapse

Constructor Details

#initialize(keychain) ⇒ AttrEncrypter


16
17
18
# File 'lib/attr_encrypter.rb', line 16

def initialize(keychain)
  @boxes = Boxes.new(keychain || "")
end

Instance Method Details

#decrypt(digest) ⇒ Object


28
29
30
31
32
33
34
35
# File 'lib/attr_encrypter.rb', line 28

def decrypt(digest)
  segments  = digest.match(DIGEST_FORMAT)
  version   = segments[1].to_i
  encoded   = segments[2]
  encrypted = Base64.decode64(encoded)

  @boxes[version].decrypt(encrypted)
end

#encrypt(raw) ⇒ Object


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

def encrypt(raw)
  version   = @boxes.latest_version
  encrypted = @boxes[version].encrypt(raw)
  encoded   = Base64.encode64(encrypted)

  "#{version}.#{encoded}"
end