no-EVP
1> c(bench3).
{ok,bench3}
2> bench3:run().
encrypt: 65230 times/sec
decrypt: 67715 times/sec
ok
EVP
1> c(bench3).
{ok,bench3}
2> bench3:run().
encrypt: 273026 times/sec
decrypt: 272186 times/sec
ok
| -module(bench3). | |
| -export([run/0, encrypt/0, decrypt/0]). | |
| -define(KEY, binary:copy(<<0>>, 16)). | |
| -define(IV, binary:copy(<<0>>, 16)). | |
| -define(PLAIN_TEXT, binary:copy(<<"1234567890">>, 140)). | |
| -define(TIME, 1.0e6). | |
| run() -> | |
| C0 = encrypt(), | |
| C1 = decrypt(), | |
| io:format("encrypt: ~p times/sec~ndecrypt: ~p times/sec~n", [C0, C1]). | |
| encrypt() -> | |
| encrypt0(?KEY, ?IV, ?PLAIN_TEXT). | |
| encrypt0(Key, Ivec, PlainText) -> | |
| encrypt1(0, erlang:timestamp(), Key, Ivec, PlainText). | |
| encrypt1(C, TS, Key, Ivec, PlainText) -> | |
| case timer:now_diff(erlang:timestamp(), TS) of | |
| N when (N > ?TIME) -> | |
| C; | |
| _ -> | |
| crypto:stream_encrypt(crypto:stream_init(aes_ctr, Key, Ivec), PlainText), | |
| encrypt1(C+1, TS, Key, Ivec, PlainText) | |
| end. | |
| decrypt() -> | |
| {_State, CipherText} = crypto:stream_encrypt(crypto:stream_init(aes_ctr, ?KEY, ?IV), ?PLAIN_TEXT), | |
| % same with encryption | |
| encrypt0(?KEY, ?IV, CipherText). |
no-EVP
1> c(bench3).
{ok,bench3}
2> bench3:run().
encrypt: 65230 times/sec
decrypt: 67715 times/sec
ok
EVP
1> c(bench3).
{ok,bench3}
2> bench3:run().
encrypt: 273026 times/sec
decrypt: 272186 times/sec
ok