Class: Rack::Session::Encryptor

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/session/encryptor.rb

Defined Under Namespace

Modules: Serializable Classes: Error, InvalidMessage, InvalidSignature, V1, V2

Instance Method Summary collapse

Constructor Details

#initialize(secret, opts = {}) ⇒ Encryptor

Returns a new instance of Encryptor.



347
348
349
350
351
352
353
354
355
356
357
358
359
360
# File 'lib/rack/session/encryptor.rb', line 347

def initialize(secret, opts = {})
  opts = opts.dup

  @mode = opts.delete(:mode)&.to_sym || :guess_version
  case @mode
  when :v1
    @v1 = V1.new(secret, opts)
  when :v2
    @v2 = V2.new(secret, opts)
  else
    @v1 = V1.new(secret, opts)
    @v2 = V2.new(secret, opts)
  end
end

Instance Method Details

#decrypt(base64_data) ⇒ Object



362
363
364
365
366
367
368
369
370
371
372
373
374
# File 'lib/rack/session/encryptor.rb', line 362

def decrypt(base64_data)
  decryptor =
    case @mode
    when :v2
      v2
    when :v1
      v1
    else
      guess_decryptor(base64_data)
    end

  decryptor.decrypt(base64_data)
end

#encrypt(message) ⇒ Object



376
377
378
379
380
381
382
383
384
385
386
# File 'lib/rack/session/encryptor.rb', line 376

def encrypt(message)
  encryptor =
    case @mode
    when :v1
      v1
    else
      v2
    end

  encryptor.encrypt(message)
end