Class: ActiverecordCursorPagination::SecureCursorSerializer

Inherits:
Serializer
  • Object
show all
Defined in:
lib/activerecord_cursor_pagination/secure_cursor_serializer.rb

Overview

Secure cursor serializer implementation using AES 256 encryption.

Instance Method Summary collapse

Instance Method Details

#deserialize(str) ⇒ Hash

Deserializes the secure cursor.

Parameters:

  • str (String)

    The AES encrypted serialized JSON string.

Returns:

  • (Hash)


11
12
13
14
15
16
17
18
# File 'lib/activerecord_cursor_pagination/secure_cursor_serializer.rb', line 11

def deserialize(str)
  c = cipher.decrypt
  c.key = cipher_key
  decoded = Base64.strict_decode64 str
  decrypted = c.update(decoded) + c.final
  json = JSON.parse decrypted
  json.symbolize_keys
end

#serialize(hash) ⇒ String

Serializes and secures the hash representation of a cursor.

Parameters:

  • hash (Hash)

    The hash representation of a cursor.

Returns:

  • (String)

    The encrypted cursor string.



26
27
28
29
30
31
32
# File 'lib/activerecord_cursor_pagination/secure_cursor_serializer.rb', line 26

def serialize(hash)
  c = cipher.encrypt
  c.key = cipher_key
  json = JSON.generate hash
  encrypted = c.update(json) + c.final
  Base64.strict_encode64 encrypted
end