Skip to content

Commit

Permalink
Fix OTP compatibility (#934)
Browse files Browse the repository at this point in the history
Closes #932.
  • Loading branch information
ericmj authored Dec 14, 2021
1 parent f2edd26 commit c73c274
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 142 deletions.
70 changes: 37 additions & 33 deletions lib/hex/stdlib.ex
Original file line number Diff line number Diff line change
Expand Up @@ -117,53 +117,57 @@ defmodule Hex.Stdlib do
end

# TODO: Remove this once we require OTP 22.1
if Code.ensure_loaded?(:crypto) and function_exported?(:crypto, :mac, 4) do
def crypto_hmac(type, key, data), do: :crypto.mac(:hmac, type, key, data)
else
def crypto_hmac(type, key, data), do: :crypto.hmac(type, key, data)
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
if Code.ensure_loaded?(:crypto) and function_exported?(:crypto, :crypto_one_time, 5) do
def crypto_one_time_encrypt(cipher, key, iv, data),
do: :crypto.crypto_one_time(cipher, key, iv, data, true)
else
def crypto_one_time_encrypt(cipher, key, iv, data),
do: :crypto.block_encrypt(cipher, key, iv, data)
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
if Code.ensure_loaded?(:crypto) and function_exported?(:crypto, :crypto_one_time, 5) do
def crypto_one_time_decrypt(cipher, key, iv, data),
do: :crypto.crypto_one_time(cipher, key, iv, data, false)
else
def crypto_one_time_decrypt(cipher, key, iv, data),
do: :crypto.block_decrypt(cipher, key, iv, data)
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
if Code.ensure_loaded?(:crypto) and function_exported?(:crypto, :crypto_one_time_aead, 7) do
def crypto_one_time_aead_encrypt(cipher, key, iv, plain_text, aad),
do: :crypto.crypto_one_time_aead(cipher, key, iv, plain_text, aad, true)
else
def crypto_one_time_aead_encrypt(_cipher, key, iv, plain_text, aad),
do: :crypto.block_encrypt(:aes_gcm, key, iv, {aad, plain_text})
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
if Code.ensure_loaded?(:crypto) and function_exported?(:crypto, :crypto_one_time_aead, 7) do
def crypto_one_time_aead_decrypt(cipher, key, iv, cipher_text, aad, cipher_tag),
do: :crypto.crypto_one_time_aead(cipher, key, iv, cipher_text, aad, cipher_tag, false)
else
def crypto_one_time_aead_decrypt(_cipher, key, iv, cipher_text, aad, cipher_tag),
do: :crypto.block_decrypt(:aes_gcm, key, iv, {aad, cipher_text, cipher_tag})
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
if Code.ensure_loaded?(:ssl) and function_exported?(:ssl, :cipher_suites, 3) do
def ssl_cipher_suites(string_type),
do: :ssl.cipher_suites(:all, hd(:ssl.versions()[:supported]), string_type)
else
def ssl_cipher_suites(string_type), do: :ssl.cipher_suites(string_type)
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
109 changes: 0 additions & 109 deletions test/hex/http/validate_cert_test.exs

This file was deleted.

0 comments on commit c73c274

Please sign in to comment.