CharlesO

CharlesO

What is the Replacement for depreciated functions in the Erlang :crypto module?

Please can you provide or point to replacement code for:

crypto — crypto v5.5 (erlang.org)

from here: Deprecations — Erlang/OTP v27.0.1

It seems several functions have no equivalents.


Functions Deprecated in OTP 27
code:lib_dir/2 (this functionality will be removed in a future release)
crypto:private_decrypt/4 (do not use)
crypto:private_encrypt/4 (use public_key:sign/3 instead)
crypto:public_decrypt/4 (use public_key:verify/4 instead)
crypto:public_encrypt/4 (do not use)
mnesia_registry:create_table/_ (use mnesia:create_table/2 instead)
public_key:decrypt_private/2 (do not use)
public_key:decrypt_private/3 (do not use)
public_key:decrypt_public/2 (use public_key:verify/4 instead)
public_key:decrypt_public/3 (use public_key:verify/5 instead)
public_key:encrypt_private/2 (use public_key:sign/3 instead)
public_key:encrypt_private/3 (use public_key:sign 4 instead)
public_key:encrypt_public/2 (do not use)
public_key:encrypt_public/3 (do not use)
ssl:prf/5 (Use export_key_materials/4 instead. Note that in OTP 28 the 'testing' way of calling this function will no longer be supported.)

I have been trying to redo : raw.githubusercontent.com/WhatsApp/WhatsApp-Flows-Tools/refs/heads/main/examples/endpoint/nodejs/basic/src/encryption.js

For a project but the samples are all coming back with depreciated functions. (Even ChatGPT and Copilot return incorrect code)

here is my effort so far:

defmodule Flows do

  defmodule FlowEndpointException do
    defexception [:message, :status_code]

    @impl true
    def exception({status_code, message}) do
      %__MODULE__{message: message, status_code: status_code}
    end
  end

  def decrypt_request(body, private_pem, passphrase) do
    %{
      "encrypted_aes_key" => encrypted_aes_key,
      "encrypted_flow_data" => encrypted_flow_data,
      "initial_vector" => initial_vector
    } = body

    private_key = :public_key.pem_entry_decode({:RSAPrivateKey, private_pem, passphrase})

    try do
      decrypted_aes_key =
        encrypted_aes_key
        |> Base.decode64!()
        |> :crypto.decrypt(:rsa, private_key, [
          {:rsa_padding, :rsa_pkcs1_oaep_padding},
          {:rsa_oaep_md, :sha256}
        ])

      flow_data_buffer = Base.decode64!(encrypted_flow_data)
      initial_vector_buffer = Base.decode64!(initial_vector)

      tag_length = 16

      {encrypted_flow_data_body, encrypted_flow_data_tag} =
        :erlang.split_binary(flow_data_buffer, byte_size(flow_data_buffer) - tag_length)

      decrypted_json_string =
        :crypto.crypto_one_time_aead(
          :aes_128_gcm,
          decrypted_aes_key,
          initial_vector_buffer,
          encrypted_flow_data_body,
          "",
          encrypted_flow_data_tag,
          false
        )

      {
        :ok,
        %{
          decrypted_body: Jason.decode!(decrypted_json_string),
          aes_key_buffer: decrypted_aes_key,
          initial_vector_buffer: initial_vector_buffer
        }
      }
    rescue
      _ ->
        raise FlowEndpointException,
              {421, "Failed to decrypt the request. Please verify your private key."}
    end
  end

  def encrypt_response(response, aes_key_buffer, initial_vector_buffer) do
    flipped_iv = for <<byte <- initial_vector_buffer>>, do: Bitwise.bnot(byte)

    {cipher_text, cipher_tag} =
      :crypto.crypto_one_time_aead(
        :aes_128_gcm,
        aes_key_buffer,
        flipped_iv,
        Jason.encode!(response),
        "",
        16,
        true
      )

    Base.encode64(cipher_text <> cipher_tag)
  end
end

Marked As Solved

voltone

voltone

Those deprecations have been reverted in OTP 27.1.

Also Liked

Schultzer

Schultzer

Working with crypto without any fundamental understanding is a recipe for disaster, even expert gets this wrong.

First you’ll need to understand why things have been deprecated, and what would be an alternative, if any.

Where Next?

Popular in Questions Top

yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
sacepums
Hey guys. I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
Codball
Mix format works fine if run from the cmd. I’ve followed this to facilitate the implementation into VSC which involves downloading an ext...
New

Other popular topics Top

chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
New
Tee
can someone please explain to me how Enum.reduce works with maps
New
sergio
I couldn’t find any guides that worked well with Phoenix 1.6.0 and esbuild. I hope this helps people test the waters and eases you into t...
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New

We're in Beta

About us Mission Statement