Erlang Crypto New API crypto_one_time/5 不接受选项:[{encrypt,true}]

Erlang Crypto New API crypto_one_time/5 does not accept Options: [{encrypt,true}]

Erlang Crypto new API 不适用于 crypto_ops()

回复:10.6.4

http://erlang.org/doc/apps/crypto/new_api.html#example-of-crypto_one_time-5

示例:

Key = <<1:128>>.
IV = <<0:128>>.
     
crypto:crypto_one_time(aes_128_ctr, Key, IV, <<"test">>,true).

按预期工作:

<<113,32,217,161>>

根据文档:

FlagOrOptions = crypto_opts() | boolean()

crypto_opts() = boolean() | [crypto_opt()]
crypto_opt() = {encrypt, boolean()} | {padding, padding()}

"在新 API 中选择加密 ({encrypt,true}) 或解密 ({encrypt,false})。"

当我这样做时:

Key = <<1:128>>.
IV = <<0:128>>.
crypto:crypto_one_time(aes_128_ctr, Key, IV, <<"test">>,[{encrypt,true}]).

我收到错误消息:

* exception error: {badarg,{"api_ng.c",72},"Bad enc flag"}
 in function  crypto:ng_crypto_one_time_nif/5
    called as crypto:ng_crypto_one_time_nif(aes_128_ctr,
                                            <<0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1>>,
                                            <<0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0>>,
                                            <<"test">>,
                                            [{encrypt,true}])

在对 github 上的 Erlang 存储库进行一些调查后,我发现函数 crypto:crypto_one_time/5 已更改为使用选项,参见 OTP-22.2.8 and for comparison please, see OTP-23.0.3. Also if you take a look to the crypto_init/3 function in OTP-22.2.8 and will try compare with crypto_init/3 function in OTP-23.0.3 您将看到变量和变量的名称从 EncryptFlag :: boolean() 更改为 FlagOrOptions :: crypto_opts() | boolean()。因此,这意味着 [{encrypt, true/false}][{padding, true/false}] 等选项的传递在 OTP-22.2.8 中根本没有实现,要开始使用这些选项,您需要将 Erlang 升级到最新版本.

看起来 ERTS 10.6.4 在 crypto_one_time 实现中有一个错误。我是 运行 erts 11.0,它工作正常:

alexei@MacBook-Pro src % erl                                                              
Erlang/OTP 23 [erts-11.0] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [hipe] [dtrace]

Eshell V11.0  (abort with ^G)

1> Key = <<1:128>>.
<<0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1>>
2> IV = <<0:128>>.
<<0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0>>
3> crypto:crypto_one_time(aes_128_ctr, Key, IV, <<"test">>,[{encrypt,true}]).
<<113,32,217,161>>

只需更新 Erlang 版本即可。