Skip to content

Instantly share code, notes, and snippets.

@wdshin
Created May 30, 2018 06:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wdshin/666159e077f51eb9ac61250761f2e010 to your computer and use it in GitHub Desktop.
Save wdshin/666159e077f51eb9ac61250761f2e010 to your computer and use it in GitHub Desktop.
Test DH
test_dh() ->
DHParams = crypto:dh_generate_parameters(1024,2),
[ DH1, DH2 ] = DHParams,
%io:format("DHParams DH1(dh_generate_parameters(1024,2)) = ~p ~n",[bin2hex(DH1)]),
{ PubKey1 , PriKey1 } = crypto:generate_key(dh,DHParams),
%io:format("PubKey1 = ~p ~n",[bin2hex(PubKey1)]),
%io:format("PriKey1 = ~p ~n",[bin2hex(PriKey1)]),
{ PubKey2 , PriKey2 } = crypto:generate_key(dh,DHParams),
%io:format("PubKey2 = ~p ~n",[bin2hex(PubKey1)]),
%io:format("PriKey2 = ~p ~n",[bin2hex(PriKey1)]),
SharedKey1 = crypto:compute_key(dh,PubKey2,PriKey1,DHParams),
SharedKey2 = crypto:compute_key(dh,PubKey1,PriKey2,DHParams),
%io:format("SharedKey1 = ~p ~n",[bin2hex(SharedKey1)]),
%io:format("SharedKey2 = ~p ~n",[bin2hex(SharedKey2)]),
SharedKey1 =:= SharedKey2.
test_ecdh() ->
%% sect571r1| sect571k1| sect409r1| sect409k1| secp521r1| secp384r1| secp224r1| secp224k1|
%% secp192k1| secp160r2| secp128r2| secp128r1| sect233r1| sect233k1| sect193r2| sect193r1|
%% sect131r2| sect131r1| sect283r1| sect283k1| sect163r2| secp256k1| secp160k1| secp160r1|
%% secp112r2| secp112r1| sect113r2| sect113r1| sect239k1| sect163r1| sect163k1| secp256r1|
%% secp192r1|
{ PubKey1 , PriKey1 } = crypto:generate_key(ecdh,sect571r1),
%io:format("PubKey1 = ~p ~n",[bin2hex(PubKey1)]),
%io:format("PriKey1 = ~p ~n",[bin2hex(PriKey1)]),
{ PubKey2 , PriKey2 } = crypto:generate_key(ecdh,sect571r1),
%io:format("PubKey2 = ~p ~n",[bin2hex(PubKey1)]),
%io:format("PriKey2 = ~p ~n",[bin2hex(PriKey1)]),
SharedKey1 = crypto:compute_key(ecdh,PubKey2,PriKey1,sect571r1),
SharedKey2 = crypto:compute_key(ecdh,PubKey1,PriKey2,sect571r1),
%io:format("SharedKey1 = ~p ~n",[bin2hex(SharedKey1)]),
%io:format("SharedKey2 = ~p ~n",[bin2hex(SharedKey2)]),
SharedKey1 =:= SharedKey2.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment