Skip to content

Conversation

jonathanhefner
Copy link
Member

This PR is split into two related but separate commits:

The 1st commit refactors MessageEncryptor to make the 2nd commit simpler, and provide a small performance increase.

The 2nd commit adds support for urlsafe: true to MessageEncryptor, a la MessageVerifier since #45419.

Follow-up to rails#43924.

This commit refactors the logic around assembling and extracting the
parts of a message (namely: the encrypted data, the IV, and the auth
tag).  It also provides a small but reproducible performance increase
for a roundtrip.

Benchmark:

```ruby
require "benchmark/ips"
require "active_support/message_encryptor"

DATA = "x" * 100
ENCRYPTOR = ActiveSupport::MessageEncryptor.new(SecureRandom.random_bytes(32))

Benchmark.ips do |x|
  x.report("roundtrip") do
    ENCRYPTOR.decrypt_and_verify(ENCRYPTOR.encrypt_and_sign(DATA))
  end
end
```

Before:

```
Warming up --------------------------------------
           roundtrip     1.342k i/100ms
Calculating -------------------------------------
           roundtrip     13.525k (± 1.5%) i/s -     68.442k in   5.061532s
```

After:

```
Warming up --------------------------------------
           roundtrip     1.409k i/100ms
Calculating -------------------------------------
           roundtrip     14.125k (± 1.4%) i/s -     71.859k in   5.088419s
```
@jonathanhefner jonathanhefner force-pushed the message_encryptor-urlsafe-option branch from 8505a4b to 85b7a8a Compare June 28, 2022 19:50
This adds a `:urlsafe` option to the `MessageEncryptor` constructor.
When enabled, this option ensures that messages use a URL-safe encoding.
This matches the `MessageVerifier` `:urlsafe` option added in rails#45419.
@jonathanhefner jonathanhefner force-pushed the message_encryptor-urlsafe-option branch from 85b7a8a to d982332 Compare June 29, 2022 21:00
@jonathanhefner jonathanhefner merged commit 50402fc into rails:main Jun 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant