Skip to content

Commit 0d0c90b

Browse files
committed
feat: sha-2 hash functions; refactoring
Breaking Changes
1 parent 2e59e17 commit 0d0c90b

File tree

158 files changed

+9075
-939
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

158 files changed

+9075
-939
lines changed

CHANGELOG.md

+23
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,29 @@ and this project adheres to [Dart Package Versioning](https://dart.dev/tools/pub
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- file "func.dart" with the definition of the functional interfaces.
13+
- cryptographic package with hash functions sha224, sha256, sha384, sha512,
14+
along with their related Hmac function (Hash-based message authentication
15+
code)
16+
- tabular text of ordinary text and data. The data can be represented with
17+
binary, octal, decimal, or hexadecimal notation.
18+
- radix package: a set of classes for converting numeric data into its textual
19+
representation in a given radix (numeric base).
20+
- bit package: a set of classes for bit-related operations.
21+
- several benchmarks comparing Dartoos to the Dart sdk or other third-party
22+
packages like _Crypto_.
23+
24+
### Changed
25+
26+
- No class extends FutureWrap any more — **BREAKING CHANGE**.
27+
- A general reorganization of the package directory structures.
28+
29+
### Removed
30+
31+
- FutureWrap — **BREAKING CHANGE**.
32+
1033
## [0.2.0] - 2021-10-15
1134

1235
### Added

README.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ This project is heavily inspired by Java
3333

3434
- No external dependencies.
3535
- Object-Oriented mindset: each concept is implemented by an **immutable** class.
36-
- For easier composability and integration with the undelying Dart's sdk, most
37-
of the classes are instances of Future.
36+
- Straightforward integration with the underlying Dart sdk.
3837

3938
## Getting started
4039

example/.gitignore

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Files and directories created by pub.
2+
.dart_tool/
3+
.packages
4+
5+
# Conventional directory for build output.
6+
build/

example/CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## 1.0.0
2+
3+
- Initial version.

example/README.md

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Dartoos Showcase
2+
3+
How to use Dartoos effectively.
4+
5+
## Getting Started
6+
7+
From the root directory of this project, enter the following commands:
8+
9+
```shell
10+
cd example/
11+
dart run radix/hex_of_bytes_benchmark.dart
12+
```
13+
14+
This launchs the benchmark of the `HexOfBytes` class, which in turn converts an
15+
array of bytes to its hexadecimal text representation. The end result is a small
16+
report that compares the performance of a typical Dart implementation of such a
17+
conversion (bytes to hexadecimal) with the performance of the conversion
18+
provided by the Dartoos package.
19+
20+
Likewise, you can issue similar commands to launch other benchmarks programs.

example/analysis_options.yaml

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
include: package:lint/analysis_options_package.yaml
2+
analyzer:
3+
strong-mode:
4+
# A value of false ensures that the type inference engine never implicitly casts
5+
# from dynamic to a more specific type.
6+
implicit-casts: false
7+
# A value of false ensures that the type inference engine never chooses the
8+
# dynamic type when it can’t determine a static type.
9+
# implicit-dynamic: false
10+
implicit-dynamic: false
11+
linter:
12+
rules:
13+
# Make constructors the first thing in every class
14+
sort_constructors_first: true
15+
# Good packages document everything
16+
public_member_api_docs: true
17+
# Always await.
18+
unawaited_futures: true
19+
always_declare_return_types: true
20+
cancel_subscriptions: true
21+
close_sinks: true
22+
only_throw_errors: true
23+
package_api_docs: true

example/base64_benchmark.dart

-69
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
}
+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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 sha256 vs. Dart's built-in
9+
///
10+
/// Running:
11+
/// ```dart /example/crypto/hash/sha256_benchmark.dart```
12+
///
13+
/// or
14+
///
15+
/// Compile to 'jit'.
16+
/// ```dart compile jit-snapshot example/crypto/hash/sha256_benchmark.dart```
17+
/// ```dart /example/crypto/hash/sha256_benchemark.jit```
18+
void main() {
19+
print("Dartoos sha256 vs. Cryptos sha256...");
20+
21+
const len = 25000000;
22+
const alphabet =
23+
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
24+
final data = BytesOf.text(Rand(len, alphabet)).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 cryptoDigest = crypto.sha256.convert(data).toString();
30+
crypto.sha256.blockSize;
31+
final cryptoHashTime = watch.elapsedMicroseconds / 1000;
32+
watch.stop();
33+
print('Crypto hashing time.......: $cryptoHashTime milliseconds');
34+
print('Crypto digest value.......: $cryptoDigest');
35+
watch.reset();
36+
37+
watch.start();
38+
final dartoosDigest = hexSha256(data);
39+
final dartoosHashTime = watch.elapsedMicroseconds / 1000;
40+
watch.stop();
41+
print('Dartoos hashing time......: $dartoosHashTime milliseconds');
42+
print('Dartoos digest value......: $dartoosDigest');
43+
const perf = PerfGain();
44+
print('Performance ratio.........: ${perf(cryptoHashTime, dartoosHashTime)}');
45+
print('Are the generated digests the same? ${dartoosDigest == cryptoDigest}');
46+
}
+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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 sha512 vs. Dart's built-in
9+
///
10+
/// Running:
11+
/// ```dart /example/crypto/hash/sha512_benchmark.dart```
12+
///
13+
/// or
14+
///
15+
/// Compile to 'jit'.
16+
/// ```dart compile jit-snapshot example/crypto/hash/sha512_benchmark.dart```
17+
/// ```dart /example/crypto/hash/sha512_benchemark.jit```
18+
void main() {
19+
print("Dartoos sha512 vs. Crypto sha512...");
20+
21+
const len = 25000000;
22+
const alphabet =
23+
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
24+
final data = BytesOf.text(Rand(len, alphabet)).value;
25+
print('\nLength of the data to be hashed: ${data.lengthInBytes} bytes.');
26+
27+
print('\n--- SHA-512 — Elapsed times for hasing ---');
28+
final watch = Stopwatch()..start();
29+
final cryptoDigest = crypto.sha512.convert(data).toString();
30+
final cryptoHashTime = watch.elapsedMicroseconds / 1000;
31+
watch.stop();
32+
print('Crypto hashing time.......: $cryptoHashTime milliseconds');
33+
print('Crypto digest value.......: $cryptoDigest');
34+
watch.reset();
35+
36+
watch.start();
37+
final dartoosDigest = hexSha512(data);
38+
final dartoosHashTime = watch.elapsedMicroseconds / 1000;
39+
watch.stop();
40+
print('Dartoos hashing time......: $dartoosHashTime milliseconds');
41+
print('Dartoos digest value......: $dartoosDigest');
42+
const perf = PerfGain();
43+
print('Performance ratio.........: ${perf(cryptoHashTime, dartoosHashTime)}');
44+
print('Are the generated digests the same? ${dartoosDigest == cryptoDigest}');
45+
}
+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// ignore_for_file: avoid_print
2+
import 'dart:convert';
3+
import 'dart:typed_data';
4+
5+
import 'package:dartoos/dartoos.dart';
6+
import 'package:dartoos/src/encoding/base64/base64_enc.dart';
7+
8+
import '../utils/perf_gain.dart';
9+
10+
/// Dartoos base64-encoder/decoder vs. Dart's built-in base64-encoder/decoder.
11+
///
12+
/// Running:
13+
/// ```shell
14+
/// dart run example/encoding/base64_benchmark.dart
15+
/// ```
16+
///
17+
/// or
18+
///
19+
/// Compile to 'jit'.
20+
/// ```shell
21+
/// dart compile jit-snapshot example/encoding/base64_benchmark.dart
22+
/// dart run /example/encoding/base64_benchmark.jit
23+
/// ```
24+
void main() {
25+
print("Dartoos base64 vs. Dart's built-in base64...");
26+
27+
const len = 50000000;
28+
const alphabet =
29+
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
30+
final bytes = BytesOf.text(Rand(len, alphabet)).value;
31+
print('\nLength of the data to be encoded: ${bytes.length} bytes.');
32+
33+
print('\n--- Encoding elapsed times ---');
34+
final watch = Stopwatch()..start();
35+
final dartEnc = base64Encode(bytes);
36+
final dartEncTime = watch.elapsedMicroseconds / 1000;
37+
print('Dart base64-encoding......: $dartEncTime milliseconds');
38+
watch.stop();
39+
watch.reset();
40+
41+
watch.start();
42+
const dartoosBase64Enc = Base64Enc();
43+
final dartoosEnc = dartoosBase64Enc(bytes);
44+
final dartoosEncTime = watch.elapsedMicroseconds / 1000;
45+
print('Dartoos base64-encoding...: $dartoosEncTime milliseconds');
46+
watch.stop();
47+
watch.reset();
48+
const perf = PerfGain();
49+
print('Encoding Performance......: ${perf(dartEncTime, dartoosEncTime)}');
50+
51+
print('\n--- Decoding elapsed times ---');
52+
watch.start();
53+
final dartDec = base64Decode(dartEnc);
54+
final dartDecTime = watch.elapsedMicroseconds / 1000;
55+
print('Dart base64-decoding......: $dartDecTime milliseconds');
56+
watch.stop();
57+
watch.reset();
58+
59+
watch.start();
60+
const dartoosDecoder = Base64Dec();
61+
final dartoosDec = dartoosDecoder(dartoosEnc);
62+
final dartoosDecTime = watch.elapsedMicroseconds / 1000;
63+
print('Dartoos base64-decoding...: $dartoosDecTime milliseconds');
64+
watch.stop();
65+
watch.reset();
66+
print('Decoding Performance......: ${perf(dartDecTime, dartoosDecTime)}');
67+
68+
print('\n--- Encoding/decoding results comparison ---');
69+
print('dartEncoded == dartoosEncoded? ${dartEnc == dartoosEnc}');
70+
print('dartDecoded == dartoosDecoded? ${bytesEqual(dartDec, dartoosDec)}');
71+
}
72+
73+
/// Checks for the equality between [first] and [second].
74+
bool bytesEqual(Uint8List first, Uint8List second) {
75+
if (first.length != second.length) return false;
76+
final length = first.length;
77+
int i = 0;
78+
while ((i < length) && (first[i] == second[i])) {
79+
++i;
80+
}
81+
return i == length;
82+
}
83+
84+
/// Returns a formatted string describing the performance gain ('+', positive
85+
/// values) or loss ('-', negative values).
86+
///
87+
/// Examples: +12% for a performance gain; -5.5% for a performance loss.
88+
// String perfGain(double dartTime, double dartoosTime) {
89+
// final perc = ((dartTime / dartoosTime) * 100) - 100;
90+
// final sign = perc > 0 ? '+' : '';
91+
// return "$sign${perc.toStringAsFixed(2)}% ('+' gain; '-' loss)";
92+
// }

0 commit comments

Comments
 (0)