add chacha20
This commit is contained in:
parent
c28699bc08
commit
d6a9e6a3ac
@ -12,7 +12,7 @@
|
|||||||
%% API
|
%% API
|
||||||
-export([rsa_encrypt/2, rsa_pem_decode/1]).
|
-export([rsa_encrypt/2, rsa_pem_decode/1]).
|
||||||
-export([aes_encrypt/3, aes_decrypt/3]).
|
-export([aes_encrypt/3, aes_decrypt/3]).
|
||||||
-export([test/0]).
|
-export([test/0, test_chacha20/0]).
|
||||||
|
|
||||||
test() ->
|
test() ->
|
||||||
Key = <<"abcdabcdabcdabcd">>,
|
Key = <<"abcdabcdabcdabcd">>,
|
||||||
@ -39,3 +39,43 @@ aes_encrypt(Key, IVec, PlainText) when is_binary(Key), is_binary(IVec), is_binar
|
|||||||
-spec aes_decrypt(binary(), binary(), binary()) -> binary().
|
-spec aes_decrypt(binary(), binary(), binary()) -> binary().
|
||||||
aes_decrypt(Key, IVec, CipherText) when is_binary(Key), is_binary(IVec), is_binary(CipherText) ->
|
aes_decrypt(Key, IVec, CipherText) when is_binary(Key), is_binary(IVec), is_binary(CipherText) ->
|
||||||
crypto:crypto_one_time(aes_128_ofb, Key, IVec, CipherText, [{encrypt, false}, {padding, pkcs_padding}]).
|
crypto:crypto_one_time(aes_128_ofb, Key, IVec, CipherText, [{encrypt, false}, {padding, pkcs_padding}]).
|
||||||
|
|
||||||
|
|
||||||
|
test_chacha20() ->
|
||||||
|
Key = crypto:strong_rand_bytes(32),
|
||||||
|
Nonce = crypto:strong_rand_bytes(12),
|
||||||
|
PlainText = <<"hello world">>,
|
||||||
|
|
||||||
|
Enc = chacha20_encrypt(Key, Nonce, PlainText),
|
||||||
|
|
||||||
|
Ex = chacha20_decrypt(Key, Enc),
|
||||||
|
|
||||||
|
logger:debug("yes ex is: ~p", [Ex]),
|
||||||
|
ok.
|
||||||
|
|
||||||
|
|
||||||
|
chacha20_encrypt(Key, Nonce, Plain) ->
|
||||||
|
AAD = <<>>,
|
||||||
|
{Cipher, Tag} = crypto:crypto_one_time_aead(
|
||||||
|
chacha20_poly1305,
|
||||||
|
Key,
|
||||||
|
Nonce,
|
||||||
|
Plain,
|
||||||
|
AAD,
|
||||||
|
true
|
||||||
|
),
|
||||||
|
<<Nonce/binary, Cipher/binary, Tag/binary>>.
|
||||||
|
|
||||||
|
chacha20_decrypt(Key, <<Nonce:12/binary, Rest/binary>>) ->
|
||||||
|
AAD = <<>>,
|
||||||
|
CipherLen = byte_size(Rest) - 16,
|
||||||
|
<<Cipher:CipherLen/binary, Tag:16/binary>> = Rest,
|
||||||
|
crypto:crypto_one_time_aead(
|
||||||
|
chacha20_poly1305,
|
||||||
|
Key,
|
||||||
|
Nonce,
|
||||||
|
Cipher,
|
||||||
|
AAD,
|
||||||
|
Tag,
|
||||||
|
false
|
||||||
|
).
|
||||||
@ -53,8 +53,12 @@ message SDLRegisterSuper {
|
|||||||
// 部分逻辑会脱离quic去通讯,增加session_token校验
|
// 部分逻辑会脱离quic去通讯,增加session_token校验
|
||||||
message SDLRegisterSuperAck {
|
message SDLRegisterSuperAck {
|
||||||
uint32 pkt_id = 1;
|
uint32 pkt_id = 1;
|
||||||
bytes aes_key = 2;
|
// 目前支持aes, chacha20
|
||||||
bytes session_token = 3;
|
string algorithm = 2;
|
||||||
|
bytes key = 3;
|
||||||
|
// 逻辑分段,chacha20加密算法需要使用该字段
|
||||||
|
uint32 region_id = 4;
|
||||||
|
bytes session_token = 5;
|
||||||
}
|
}
|
||||||
|
|
||||||
message SDLRegisterSuperNak {
|
message SDLRegisterSuperNak {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user