Class: Rage::Cookies::EncryptedJar

Inherits:
Object
  • Object
show all
Defined in:
lib/rage/cookies.rb

Constant Summary collapse

SALT =
"encrypted cookie"
PADDING =
"00"

Class Method Summary collapse

Class Method Details

.dump(value) ⇒ Object



292
293
294
295
296
# File 'lib/rage/cookies.rb', line 292

def dump(value)
  # add two bytes to hold meta information, e.g. in case we
  # need to change the encryption algorithm in the future
  Base64.urlsafe_encode64(PADDING + primary_box.encrypt(value.to_s))
end

.load(value) ⇒ Object



273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/rage/cookies.rb', line 273

def load(value)
  box = primary_box

  begin
    box.decrypt(Base64.urlsafe_decode64(value).byteslice(2..))
  rescue ArgumentError
    Rage.logger.debug("Failed to decode encrypted cookie")
    nil
  rescue RbNaCl::CryptoError
    Rage.logger.debug("Failed to decrypt encrypted cookie")
    i ||= 0
    if (box = fallback_boxes[i])
      Rage.logger.debug("Trying to decrypt with fallback key ##{i + 1}")
      i += 1
      retry
    end
  end
end