Class: Mayu::EncryptedMarshal::Message

Inherits:
Data
  • Object
show all
Defined in:
lib/mayu/encrypted_marshal.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#adObject (readonly)

Returns the value of attribute ad

Returns:

  • (Object)

    the current value of ad



36
37
38
# File 'lib/mayu/encrypted_marshal.rb', line 36

def ad
  @ad
end

#ciphertextObject (readonly)

Returns the value of attribute ciphertext

Returns:

  • (Object)

    the current value of ciphertext



36
37
38
# File 'lib/mayu/encrypted_marshal.rb', line 36

def ciphertext
  @ciphertext
end

#nonceObject (readonly)

Returns the value of attribute nonce

Returns:

  • (Object)

    the current value of nonce



36
37
38
# File 'lib/mayu/encrypted_marshal.rb', line 36

def nonce
  @nonce
end

Class Method Details

.unpack(data) ⇒ Object



38
39
40
41
42
43
# File 'lib/mayu/encrypted_marshal.rb', line 38

def self.unpack(data)
  data.unpack("S a*") => [nonce_length, data]
  data.unpack("a#{nonce_length} S a*") => [nonce, ad_length, data]
  data.unpack("a#{ad_length} a*") => [ad, ciphertext]
  new(nonce, AdditionalData.unpack(ad), ciphertext)
end

Instance Method Details

#expired?(now: Time.now) ⇒ Boolean

Returns:

  • (Boolean)


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

def expired?(now: Time.now) = ad.expired?(now:)

#expires_atObject



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

def expires_at = ad.expires_at

#packObject



45
46
47
48
49
50
51
52
53
54
# File 'lib/mayu/encrypted_marshal.rb', line 45

def pack
  packed_ad = ad.pack
  [
    nonce.bytesize,
    nonce,
    packed_ad.bytesize,
    packed_ad,
    ciphertext
  ].pack("S a* S a* a*")
end

#verify_timestamps!(now: Time.now) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/mayu/encrypted_marshal.rb', line 59

def verify_timestamps!(now: Time.now)
  if ad.expired?(now:)
    raise ExpiredError, "Message expired at #{ad.expires_at}"
  end

  if ad.issued_at > now.to_f
    raise IssuedInTheFutureError,
          "Message was issued in the future, #{Time.at(ad.issued_at)}"
  end

  self
end