|
| 1 | +// ignore_for_file: avoid_print |
| 2 | + |
| 3 | +import 'package:crypto/crypto.dart' as crypto; |
| 4 | +import 'package:dartoos/dartoos.dart'; |
| 5 | + |
| 6 | +import '../../utils/perf_gain.dart'; |
| 7 | + |
| 8 | +/// Dartoos Hmac Sha256 vs. Dart's built-in |
| 9 | +/// |
| 10 | +/// Running: |
| 11 | +/// ```dart /example/crypto/hash/hmac_sha256_benchmark.dart``` |
| 12 | +/// |
| 13 | +/// or |
| 14 | +/// |
| 15 | +/// Compile to 'jit'. |
| 16 | +/// ```dart compile jit-snapshot example/crypto/hash/hmac_sha256_benchmark.dart``` |
| 17 | +/// ```dart /example/crypto/hash/hmac_sha256_benchemark.jit``` |
| 18 | +void main() { |
| 19 | + print("Dartoos HMAC-SHA256 vs. Cryptos HMAC-SHA256..."); |
| 20 | + const len = 25000000; |
| 21 | + const alphabet = |
| 22 | + 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; |
| 23 | + final data = BytesOf.text(Rand(len, alphabet)).value; |
| 24 | + final key = BytesOf.utf8('Dartoos vs Crypto').value; |
| 25 | + print('\nLength of the data to be hashed: ${data.lengthInBytes} bytes.'); |
| 26 | + |
| 27 | + print('\n--- Hashing elapsed times ---'); |
| 28 | + final watch = Stopwatch()..start(); |
| 29 | + final cryptoHmac = crypto.Hmac(crypto.sha256, key).convert(data).toString(); |
| 30 | + final cryptoHashTime = watch.elapsedMicroseconds / 1000; |
| 31 | + watch.stop(); |
| 32 | + print('Crypto hashing time.......: $cryptoHashTime milliseconds'); |
| 33 | + print('Crypto digest value.......: $cryptoHmac'); |
| 34 | + watch.reset(); |
| 35 | + |
| 36 | + watch.start(); |
| 37 | + final dartoosHmac = HexHmac.sha256(key).value(data); |
| 38 | + final dartoosHashTime = watch.elapsedMicroseconds / 1000; |
| 39 | + watch.stop(); |
| 40 | + print('Dartoos hashing time......: $dartoosHashTime milliseconds'); |
| 41 | + print('Dartoos digest value......: $dartoosHmac'); |
| 42 | + const perf = PerfGain(); |
| 43 | + print('Performance ratio.........: ${perf(cryptoHashTime, dartoosHashTime)}'); |
| 44 | + print('Are the generated digests the same? ${dartoosHmac == cryptoHmac}'); |
| 45 | +} |
0 commit comments