Skip to content

Commit c73c274

Browse files
authored
Fix OTP compatibility (#934)
Closes #932.
1 parent f2edd26 commit c73c274

2 files changed

Lines changed: 37 additions & 142 deletions

File tree

lib/hex/stdlib.ex

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -117,53 +117,57 @@ defmodule Hex.Stdlib do
117117
end
118118

119119
# TODO: Remove this once we require OTP 22.1
120-
if Code.ensure_loaded?(:crypto) and function_exported?(:crypto, :mac, 4) do
121-
def crypto_hmac(type, key, data), do: :crypto.mac(:hmac, type, key, data)
122-
else
123-
def crypto_hmac(type, key, data), do: :crypto.hmac(type, key, data)
120+
def crypto_hmac(type, key, data) do
121+
if Code.ensure_loaded?(:crypto) and function_exported?(:crypto, :mac, 4) do
122+
apply(:crypto, :mac, [:hmac, type, key, data])
123+
else
124+
apply(:crypto, :hmac, [type, key, data])
125+
end
124126
end
125127

126128
# TODO: Remove this once we require OTP 22.0
127-
if Code.ensure_loaded?(:crypto) and function_exported?(:crypto, :crypto_one_time, 5) do
128-
def crypto_one_time_encrypt(cipher, key, iv, data),
129-
do: :crypto.crypto_one_time(cipher, key, iv, data, true)
130-
else
131-
def crypto_one_time_encrypt(cipher, key, iv, data),
132-
do: :crypto.block_encrypt(cipher, key, iv, data)
129+
def crypto_one_time_encrypt(cipher, key, iv, data) do
130+
if Code.ensure_loaded?(:crypto) and function_exported?(:crypto, :crypto_one_time, 5) do
131+
apply(:crypto, :crypto_one_time, [cipher, key, iv, data, true])
132+
else
133+
apply(:crypto, :block_encrypt, [cipher, key, iv, data])
134+
end
133135
end
134136

135137
# TODO: Remove this once we require OTP 22.0
136-
if Code.ensure_loaded?(:crypto) and function_exported?(:crypto, :crypto_one_time, 5) do
137-
def crypto_one_time_decrypt(cipher, key, iv, data),
138-
do: :crypto.crypto_one_time(cipher, key, iv, data, false)
139-
else
140-
def crypto_one_time_decrypt(cipher, key, iv, data),
141-
do: :crypto.block_decrypt(cipher, key, iv, data)
138+
def crypto_one_time_decrypt(cipher, key, iv, data) do
139+
if Code.ensure_loaded?(:crypto) and function_exported?(:crypto, :crypto_one_time, 5) do
140+
apply(:crypto, :crypto_one_time, [cipher, key, iv, data, false])
141+
else
142+
apply(:crypto, :block_decrypt, [cipher, key, iv, data])
143+
end
142144
end
143145

144146
# TODO: Remove this once we require OTP 22.0
145-
if Code.ensure_loaded?(:crypto) and function_exported?(:crypto, :crypto_one_time_aead, 7) do
146-
def crypto_one_time_aead_encrypt(cipher, key, iv, plain_text, aad),
147-
do: :crypto.crypto_one_time_aead(cipher, key, iv, plain_text, aad, true)
148-
else
149-
def crypto_one_time_aead_encrypt(_cipher, key, iv, plain_text, aad),
150-
do: :crypto.block_encrypt(:aes_gcm, key, iv, {aad, plain_text})
147+
def crypto_one_time_aead_encrypt(cipher, key, iv, plain_text, aad) do
148+
if Code.ensure_loaded?(:crypto) and function_exported?(:crypto, :crypto_one_time_aead, 5) do
149+
apply(:crypto, :crypto_one_time_aead, [cipher, key, iv, plain_text, aad, true])
150+
else
151+
apply(:crypto, :block_encrypt, [:aes_gcm, key, iv, {aad, plain_text}])
152+
end
151153
end
152154

153155
# TODO: Remove this once we require OTP 22.0
154-
if Code.ensure_loaded?(:crypto) and function_exported?(:crypto, :crypto_one_time_aead, 7) do
155-
def crypto_one_time_aead_decrypt(cipher, key, iv, cipher_text, aad, cipher_tag),
156-
do: :crypto.crypto_one_time_aead(cipher, key, iv, cipher_text, aad, cipher_tag, false)
157-
else
158-
def crypto_one_time_aead_decrypt(_cipher, key, iv, cipher_text, aad, cipher_tag),
159-
do: :crypto.block_decrypt(:aes_gcm, key, iv, {aad, cipher_text, cipher_tag})
156+
def crypto_one_time_aead_decrypt(cipher, key, iv, cipher_text, aad, cipher_tag) do
157+
if Code.ensure_loaded?(:crypto) and function_exported?(:crypto, :crypto_one_time_aead, 5) do
158+
apply(:crypto, :crypto_one_time_aead, [cipher, key, iv, cipher_text, aad, cipher_tag, false])
159+
else
160+
apply(:crypto, :block_decrypt, [:aes_gcm, key, iv, {aad, cipher_text, cipher_tag}])
161+
end
160162
end
161163

162164
# TODO: Remove this once we require OTP 22.0
163-
if Code.ensure_loaded?(:ssl) and function_exported?(:ssl, :cipher_suites, 3) do
164-
def ssl_cipher_suites(string_type),
165-
do: :ssl.cipher_suites(:all, hd(:ssl.versions()[:supported]), string_type)
166-
else
167-
def ssl_cipher_suites(string_type), do: :ssl.cipher_suites(string_type)
165+
def ssl_cipher_suites(string_type) do
166+
if Code.ensure_loaded?(:ssl) and function_exported?(:ssl, :cipher_suites, 3) do
167+
[ssl_version | _] = :ssl.versions()[:supported]
168+
apply(:ssl, :cipher_suites, [:all, ssl_version, string_type])
169+
else
170+
apply(:ssl, :cipher_suites, [string_type])
171+
end
168172
end
169173
end

test/hex/http/validate_cert_test.exs

Lines changed: 0 additions & 109 deletions
This file was deleted.

0 commit comments

Comments
 (0)