File: stdlib.ex

package info (click to toggle)
erlang-hex 2.0.6-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,204 kB
  • sloc: erlang: 2,950; sh: 203; makefile: 10
file content (82 lines) | stat: -rw-r--r-- 2,983 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
defmodule Hex.Stdlib do
  @moduledoc false

  defmacro stacktrace() do
    if Version.compare(System.version(), "1.7.0") == :lt do
      quote do: System.stacktrace()
    else
      quote do: __STACKTRACE__
    end
  end

  # TODO: Remove this once we require OTP 24.0
  def ssh_hostkey_fingerprint(digset_type, key) do
    cond do
      # Requires Elixir 1.15.0
      function_exported?(Mix, :ensure_application!, 1) ->
        Mix.ensure_application!(:ssh)
        apply(:ssh, :hostkey_fingerprint, [digset_type, key])

      Code.ensure_loaded?(:ssh) and function_exported?(:ssh, :hostkey_fingerprint, 2) ->
        apply(:ssh, :hostkey_fingerprint, [digset_type, key])

      true ->
        apply(:public_key, :ssh_hostkey_fingerprint, [digset_type, key])
    end
  end

  # TODO: Remove this once we require OTP 22.1
  def crypto_hmac(type, key, data) do
    if Code.ensure_loaded?(:crypto) and function_exported?(:crypto, :mac, 4) do
      apply(:crypto, :mac, [:hmac, type, key, data])
    else
      apply(:crypto, :hmac, [type, key, data])
    end
  end

  # TODO: Remove this once we require OTP 22.0
  def crypto_one_time_encrypt(cipher, key, iv, data) do
    if Code.ensure_loaded?(:crypto) and function_exported?(:crypto, :crypto_one_time, 5) do
      apply(:crypto, :crypto_one_time, [cipher, key, iv, data, true])
    else
      apply(:crypto, :block_encrypt, [cipher, key, iv, data])
    end
  end

  # TODO: Remove this once we require OTP 22.0
  def crypto_one_time_decrypt(cipher, key, iv, data) do
    if Code.ensure_loaded?(:crypto) and function_exported?(:crypto, :crypto_one_time, 5) do
      apply(:crypto, :crypto_one_time, [cipher, key, iv, data, false])
    else
      apply(:crypto, :block_decrypt, [cipher, key, iv, data])
    end
  end

  # TODO: Remove this once we require OTP 22.0
  def crypto_one_time_aead_encrypt(cipher, key, iv, plain_text, aad) do
    if Code.ensure_loaded?(:crypto) and function_exported?(:crypto, :crypto_one_time_aead, 5) do
      apply(:crypto, :crypto_one_time_aead, [cipher, key, iv, plain_text, aad, true])
    else
      apply(:crypto, :block_encrypt, [:aes_gcm, key, iv, {aad, plain_text}])
    end
  end

  # TODO: Remove this once we require OTP 22.0
  def crypto_one_time_aead_decrypt(cipher, key, iv, cipher_text, aad, cipher_tag) do
    if Code.ensure_loaded?(:crypto) and function_exported?(:crypto, :crypto_one_time_aead, 5) do
      apply(:crypto, :crypto_one_time_aead, [cipher, key, iv, cipher_text, aad, cipher_tag, false])
    else
      apply(:crypto, :block_decrypt, [:aes_gcm, key, iv, {aad, cipher_text, cipher_tag}])
    end
  end

  # TODO: Remove this once we require OTP 22.0
  def ssl_cipher_suites(string_type) do
    if Code.ensure_loaded?(:ssl) and function_exported?(:ssl, :cipher_suites, 3) do
      [ssl_version | _] = :ssl.versions()[:supported]
      apply(:ssl, :cipher_suites, [:all, ssl_version, string_type])
    else
      apply(:ssl, :cipher_suites, [string_type])
    end
  end
end