new
- 1.0.0
- 1.1.6
- 1.2.6
- 2.0.3
- 2.1.0
- 2.2.1
- 2.3.8 (0)
- 3.0.0 (0)
- 3.0.9 (0)
- 3.1.0 (0)
- 3.2.1 (0)
- 3.2.8 (0)
- 3.2.13 (0)
- 4.0.2 (10)
- 4.1.8 (0)
- 4.2.1 (2)
- 4.2.7 (0)
- 4.2.9 (0)
- 5.0.0.1 (0)
- 5.1.7 (5)
- 5.2.3 (0)
- 6.0.0 (0)
- 6.1.3.1 (0)
- 6.1.7.7 (0)
- 7.0.0 (0)
- 7.1.3.2 (38)
- 7.1.3.4 (0)
- What's this?
new(secret, sign_secret = nil, **options)
public
Initialize a new MessageEncryptor. secret must be at least as long as the cipher key size. For the default ‘aes-256-gcm’ cipher, this is 256 bits. If you are using a user-entered secret, you can generate a suitable key by using ActiveSupport::KeyGenerator or a similar key derivation function.
The first additional parameter is used as the signature key for MessageVerifier. This allows you to specify keys to encrypt and sign data. Ignored when using an AEAD cipher like ‘aes-256-gcm’.
ActiveSupport::MessageEncryptor.new('secret', 'signature_secret')
Options
- :cipher
-
Cipher to use. Can be any cipher returned by +OpenSSL::Cipher.ciphers+. Default is ‘aes-256-gcm’.
- :digest
-
Digest used for signing. Ignored when using an AEAD cipher like ‘aes-256-gcm’.
- :serializer
-
The serializer used to serialize message data. You can specify any object that responds to dump and load, or you can choose from several preconfigured serializers: :marshal, :json_allow_marshal, :json, :message_pack_allow_marshal, :message_pack.
The preconfigured serializers include a fallback mechanism to support multiple deserialization formats. For example, the :marshal serializer will serialize using Marshal, but can deserialize using Marshal, ActiveSupport::JSON, or ActiveSupport::MessagePack. This makes it easy to migrate between serializers.
The :marshal, :json_allow_marshal, and :message_pack_allow_marshal serializers support deserializing using Marshal, but the others do not. Beware that Marshal is a potential vector for deserialization attacks in cases where a message signing secret has been leaked. If possible, choose a serializer that does not support Marshal.
The :message_pack and :message_pack_allow_marshal serializers use ActiveSupport::MessagePack, which can roundtrip some Ruby types that are not supported by JSON, and may provide improved performance. However, these require the msgpack gem.
When using Rails, the default depends on config.active_support.message_serializer. Otherwise, the default is :marshal.
- :url_safe
-
By default, MessageEncryptor generates RFC 4648 compliant strings which are not URL-safe. In other words, they can contain “+” and “/”. If you want to generate URL-safe strings (in compliance with “Base 64 Encoding with URL and Filename Safe Alphabet” in RFC 4648), you can pass true.
- :force_legacy_metadata_serializer
-
Whether to use the legacy metadata serializer, which serializes the message first, then wraps it in an envelope which is also serialized. This was the default in Rails 7.0 and below.
If you don’t pass a truthy value, the default is set using config.active_support.use_message_serializer_for_metadata.