Can anyone shed any light on the algorithm used for Phoenix.Token.encrypt/decrypt?

Hi, we’re looking to create the output from Phoenix.Token.encrypt and .decrypt in another language for testing purposes. My guess it’s a combination of HEX(HMAC()) and concatenation with . of some of the context/key/salt.

Not sure if anyone can help out with whats going on under the hood beyond the docs Phoenix.Token — Phoenix v1.7.14?

Phoenix.Token mostly delegates the work to Plug.Crypto.

For instance, Phoenix.Token.encrypt leads here:

and then to MessageEncryptor:

Also note that encode in Plug.Crypto uses :erlang.term_to_binary, so you’ll need an implementation of External Term Format as well as all the ChaCha20 juggling.

4 Likes

thank you @al2o3cr for the detailed reply and pointing out the ETF and ChaCha20. It’s interesting taking a peek under the hood - thanks for sharing

For us, I think we’ll be moving to something more standard given we have that option at this point