Skip to content

Commit c9a756c

Browse files
committed
Support BigInt conversion in HashDigest
- Renames `remainder` -> `number` - Adds `bigInt` method to get BigInt from bytes
1 parent e7d109d commit c9a756c

11 files changed

+33
-31
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# 1.11.4
22

33
- Move codecs to separate package: [hashlib_codecs](https://github.com/bitanon/hashlib_codecs)
4+
- Support BigInt conversion in `HashDigest`
5+
- Renames `remainder` -> `number`
6+
- Adds `bigInt` method to get `BigInt` from bytes
47

58
# 1.11.3
69

lib/src/alder32.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,5 @@ class _Alder32 extends HashBase {
3737
/// - [input] is the string to hash
3838
/// - The [encoding] is the encoding to use. Default is `input.codeUnits`
3939
int alder32code(String input, [Encoding? encoding]) {
40-
return alder32.string(input, encoding).remainder();
40+
return alder32.string(input, encoding).number();
4141
}

lib/src/core/hash_digest.dart

+16-19
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,22 @@ class HashDigest extends Object {
4141
String base64({bool urlSafe = false, bool padding = false}) =>
4242
toBase64(bytes, padding: padding, url: urlSafe);
4343

44+
/// The message digest as a BigInt.
45+
///
46+
/// If [endian] is [Endian.little], it will treat the digest bytes as a little
47+
/// endian number; Otherwise, if [endian] is [Endian.big], it will treat the
48+
/// digest bytes as a big endian number.
49+
BigInt bigInt({Endian endian = Endian.little}) =>
50+
toBigInt(bytes, endian: endian);
51+
52+
/// Gets 64-bit unsiged integer from the message digest.
53+
///
54+
/// If [endian] is [Endian.little], it will treat the digest bytes as a little
55+
/// endian number; Otherwise, if [endian] is [Endian.big], it will treat the
56+
/// digest bytes as a big endian number.
57+
int number([Endian endian = Endian.big]) =>
58+
toBigInt(bytes, endian: endian).toUnsigned(64).toInt();
59+
4460
/// The message digest as a string of ASCII alphabets.
4561
String ascii() => cvt.ascii.decode(bytes);
4662

@@ -50,25 +66,6 @@ class HashDigest extends Object {
5066
/// Returns the digest in the given [encoding]
5167
String to(cvt.Encoding encoding) => encoding.decode(bytes);
5268

53-
/// Returns the least significant bytes as a number.
54-
///
55-
/// If [endian] is big, it will return the last few bytes as a number;
56-
/// Otherwise, if [endian] is little, it will return the first few bytes
57-
/// as a number.
58-
int remainder([Endian endian = Endian.big]) {
59-
int result = 0;
60-
if (endian == Endian.big) {
61-
for (int i = bytes.length - 1, p = 0; i >= 0; --i, p += 8) {
62-
result |= bytes[i] << p;
63-
}
64-
} else {
65-
for (int i = 0, p = 0; i < 8 && i < bytes.length; ++i, p += 8) {
66-
result |= bytes[i] << p;
67-
}
68-
}
69-
return result;
70-
}
71-
7269
@override
7370
int get hashCode => bytes.hashCode;
7471

lib/src/crc16.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,5 @@ class _CRC16 extends HashBase {
2828
/// - [input] is the string to hash
2929
/// - The [encoding] is the encoding to use. Default is `input.codeUnits`
3030
int crc16code(String input, [Encoding? encoding]) {
31-
return crc16.string(input, encoding).remainder();
31+
return crc16.string(input, encoding).number();
3232
}

lib/src/crc32.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class _CRC32 extends HashBase {
3030
/// - [input] is the string to hash
3131
/// - The [encoding] is the encoding to use. Default is `input.codeUnits`
3232
int crc32code(String input, [Encoding? encoding]) {
33-
return crc32.string(input, encoding).remainder();
33+
return crc32.string(input, encoding).number();
3434
}
3535

3636
/// Extension to [String] to generate [crc32] code
@@ -40,6 +40,6 @@ extension CRC32StringExtension on String {
4040
/// Parameters:
4141
/// - If no [encoding] is defined, the `codeUnits` is used to get the bytes.
4242
int crc32code([Encoding? encoding]) {
43-
return crc32.string(this, encoding).remainder();
43+
return crc32.string(this, encoding).number();
4444
}
4545
}

lib/src/crc64.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class _CRC64 extends HashBase {
3030
/// - [input] is the string to hash
3131
/// - The [encoding] is the encoding to use. Default is `input.codeUnits`
3232
int crc64code(String input, [Encoding? encoding]) {
33-
return crc64.string(input, encoding).remainder();
33+
return crc64.string(input, encoding).number();
3434
}
3535

3636
/// Gets the CRC-64 hash of a String in hexadecimal.

lib/src/xxh3.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class XXH3 extends BlockHashBase {
5959
/// - [input] is the string to hash
6060
/// - The [encoding] is the encoding to use. Default is `input.codeUnits`
6161
int xxh3code(String input, [Encoding? encoding]) {
62-
return xxh3.string(input, encoding).remainder();
62+
return xxh3.string(input, encoding).number();
6363
}
6464

6565
/// Gets the 64-bit XXH3 hash of a String in hexadecimal.
@@ -78,7 +78,7 @@ extension XXH3StringExtension on String {
7878
/// Parameters:
7979
/// - If no [encoding] is defined, the `codeUnits` is used to get the bytes.
8080
int xxh3code([Encoding? encoding]) {
81-
return xxh3.string(this, encoding).remainder();
81+
return xxh3.string(this, encoding).number();
8282
}
8383

8484
/// Gets the 64-bit XXH3 hash of a String in hexadecimal.

lib/src/xxh32.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class XXHash32 extends BlockHashBase {
4545
/// - [input] is the string to hash
4646
/// - The [encoding] is the encoding to use. Default is `input.codeUnits`
4747
int xxh32code(String input, [Encoding? encoding]) {
48-
return xxh32.string(input, encoding).remainder();
48+
return xxh32.string(input, encoding).number();
4949
}
5050

5151
/// Extension to [String] to generate [xxh32] code
@@ -55,6 +55,6 @@ extension XXHash32StringExtension on String {
5555
/// Parameters:
5656
/// - If no [encoding] is defined, the `codeUnits` is used to get the bytes.
5757
int xxh32code([Encoding? encoding]) {
58-
return xxh32.string(this, encoding).remainder();
58+
return xxh32.string(this, encoding).number();
5959
}
6060
}

lib/src/xxh64.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class XXHash64 extends BlockHashBase {
4545
/// - [input] is the string to hash.
4646
/// - The [encoding] is the encoding to use. Default is `input.codeUnits`.
4747
int xxh64code(String input, [Encoding? encoding]) {
48-
return xxh64.string(input, encoding).remainder();
48+
return xxh64.string(input, encoding).number();
4949
}
5050

5151
/// Gets the 64-bit xxHash hash of a String in hexadecimal.
@@ -64,7 +64,7 @@ extension XXHash64StringExtension on String {
6464
/// Parameters:
6565
/// - If no [encoding] is defined, the `codeUnits` is used to get the bytes.
6666
int xxh64code([Encoding? encoding]) {
67-
return xxh64.string(this, encoding).remainder();
67+
return xxh64.string(this, encoding).number();
6868
}
6969

7070
/// Gets the 64-bit xxHash hash of a String in hexadecimal.

pubspec.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ platforms:
1515
windows:
1616

1717
dependencies:
18-
hashlib_codecs: ^1.1.1
18+
hashlib_codecs: ^1.2.0
1919

2020
dev_dependencies:
2121
lints: ^1.0.1

test/crc_test.dart

+2
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@ void main() {
1111
});
1212
test('CRC-16 with a string', () {
1313
expect(crc16.string("Wikipedia").hex(), "ee3e");
14+
expect(crc16code("Wikipedia"), 0xee3e);
1415
});
1516

1617
test('CRC-32 with empty string', () {
1718
expect(crc32code(""), 0);
1819
});
1920
test('CRC-32 with a string', () {
2021
expect(crc32.string("Wikipedia").hex(), "adaac02e");
22+
expect(crc32code("Wikipedia"), 0xadaac02e);
2123
});
2224

2325
test('CRC-64 with empty string', () {

0 commit comments

Comments
 (0)