Class: Mayu::EncryptedMarshal

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/mayu/encrypted_marshal.rb

Defined Under Namespace

Classes: AdditionalData, DecryptError, Error, ExpiredError, IssuedInTheFutureError, Message

Constant Summary collapse

Cipher =
RbNaCl::AEAD::ChaCha20Poly1305IETF
DEFAULT_TTL_SECONDS =
10

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_key, default_ttl: DEFAULT_TTL_SECONDS) ⇒ EncryptedMarshal

Returns a new instance of EncryptedMarshal.



83
84
85
86
# File 'lib/mayu/encrypted_marshal.rb', line 83

def initialize(base_key, default_ttl: DEFAULT_TTL_SECONDS)
  @cipher = Cipher.new(RbNaCl::Hash.sha256(base_key))
  @default_ttl = default_ttl
end

Class Method Details

.random_keyObject



80
# File 'lib/mayu/encrypted_marshal.rb', line 80

def self.random_key = RbNaCl::Random.random_bytes(Cipher::KEYBYTES)

Instance Method Details

#dump(object, ttl: @default_ttl) ⇒ Object



89
90
91
92
93
# File 'lib/mayu/encrypted_marshal.rb', line 89

def dump(object, ttl: @default_ttl) =
      object
.then { Marshal.dump(_1) }
.then { Brotli.deflate(_1) }
.then { encrypt(_1, ttl:) }

#load(encrypted) ⇒ Object



96
97
98
99
100
101
102
# File 'lib/mayu/encrypted_marshal.rb', line 96

def load(encrypted) =
      encrypted
.then { Message.unpack(_1) }
.then { _1.verify_timestamps! }
.then { decrypt(_1) }
.then { Brotli.inflate(_1) }
.then { Marshal.load(_1) }