Skip to content

Commit

Permalink
Fix import issue for web platform
Browse files Browse the repository at this point in the history
  • Loading branch information
dipu-bd committed Jul 1, 2024
1 parent 17c20be commit 5add7d0
Show file tree
Hide file tree
Showing 13 changed files with 127 additions and 61 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 1.18.1

- Fix import issue for _web_ platform.

# 1.18.0

- Implement BCrypt algorithm.
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,11 @@ void main() {
var scryptLittle = ScryptSecurity.little;
print("[scrypt] => ${scrypt(pw, salt, security: scryptLittle, dklen: 24)}");
print('');
// Examples of bcrypt key derivation
var bcryptLittle = BcryptSecurity.little;
print("[bcrypt] => ${bcrypt(pw, bcryptSalt(security: bcryptLittle))}");
print('');
}
```

Expand Down
7 changes: 4 additions & 3 deletions dart_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ concurrency: 8
platforms: [vm, node]

tags:
skip-js:
vm-only:
skip: true
on_platform:
node:
skip: true
vm:
skip: false
5 changes: 5 additions & 0 deletions example/hashlib_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,9 @@ void main() {
var scryptLittle = ScryptSecurity.little;
print("[scrypt] => ${scrypt(pw, salt, security: scryptLittle, dklen: 24)}");
print('');

// Examples of bcrypt key derivation
var bcryptLittle = BcryptSecurity.little;
print("[bcrypt] => ${bcrypt(pw, bcryptSalt(security: bcryptLittle))}");
print('');
}
47 changes: 2 additions & 45 deletions lib/src/core/hash_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@

import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'dart:typed_data';

import 'package:hashlib/src/core/hash_digest.dart';

export 'hash_base_stub.dart' if (dart.library.io) 'hash_base_io.dart';

/// This sink allows adding arbitrary length byte arrays
/// and produces a [HashDigest] on [close].
abstract class HashDigestSink implements Sink<List<int>> {
Expand Down Expand Up @@ -119,47 +119,4 @@ abstract class HashBase implements StreamTransformer<List<int>, HashDigest> {
]) {
return bind(stream.transform(encoding.encoder)).first;
}

/// Converts the [input] file and returns a [HashDigest] asynchronously.
///
/// If [start] is present, the file will be read from byte-offset [start].
/// Otherwise from the beginning (index 0).
///
/// If [end] is present, only bytes up to byte-index [end] will be read.
/// Otherwise, until end of file.
@pragma('vm:prefer-inline')
Future<HashDigest> file(File input, [int start = 0, int? end]) {
return bind(input.openRead(start, end)).first;
}

/// Converts the [input] file and returns a [HashDigest] synchronously.
///
/// If [start] is present, the file will be read from byte-offset [start].
/// Otherwise from the beginning (index 0).
///
/// If [end] is present, only bytes up to byte-index [end] will be read.
/// Otherwise, until end of file.
///
/// If [bufferSize] is present, the file will be read in chunks of this size.
/// By default the [bufferSize] is `2048`.
HashDigest fileSync(
File input, {
int start = 0,
int? end,
int bufferSize = 2048,
}) {
var raf = input.openSync();
try {
var sink = createSink();
var buffer = Uint8List(bufferSize);
int length = end ?? raf.lengthSync();
for (int i = start, l; i < length; i += l) {
l = raf.readIntoSync(buffer);
sink.add(buffer, 0, l);
}
return sink.digest();
} finally {
raf.closeSync();
}
}
}
54 changes: 54 additions & 0 deletions lib/src/core/hash_base_io.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright (c) 2024, Sudipto Chandra
// All rights reserved. Check LICENSE file for details.

import 'dart:async';
import 'dart:io';
import 'dart:typed_data';

import 'hash_base.dart';
import 'hash_digest.dart';

extension HashBaseFileSupport on HashBase {
/// Converts the [input] file and returns a [HashDigest] asynchronously.
///
/// If [start] is present, the file will be read from byte-offset [start].
/// Otherwise from the beginning (index 0).
///
/// If [end] is present, only bytes up to byte-index [end] will be read.
/// Otherwise, until end of file.
@pragma('vm:prefer-inline')
Future<HashDigest> file(File input, [int start = 0, int? end]) {
return bind(input.openRead(start, end)).first;
}

/// Converts the [input] file and returns a [HashDigest] synchronously.
///
/// If [start] is present, the file will be read from byte-offset [start].
/// Otherwise from the beginning (index 0).
///
/// If [end] is present, only bytes up to byte-index [end] will be read.
/// Otherwise, until end of file.
///
/// If [bufferSize] is present, the file will be read in chunks of this size.
/// By default the [bufferSize] is `2048`.
HashDigest fileSync(
File input, {
int start = 0,
int? end,
int bufferSize = 2048,
}) {
var raf = input.openSync();
try {
var sink = createSink();
var buffer = Uint8List(bufferSize);
int length = end ?? raf.lengthSync();
for (int i = start, l; i < length; i += l) {
l = raf.readIntoSync(buffer);
sink.add(buffer, 0, l);
}
return sink.digest();
} finally {
raf.closeSync();
}
}
}
40 changes: 40 additions & 0 deletions lib/src/core/hash_base_stub.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright (c) 2024, Sudipto Chandra
// All rights reserved. Check LICENSE file for details.

import 'dart:async';

import 'hash_base.dart';
import 'hash_digest.dart';

extension HashBaseFileSupport on HashBase {
/// Converts the [input] file and returns a [HashDigest] asynchronously.
///
/// If [start] is present, the file will be read from byte-offset [start].
/// Otherwise from the beginning (index 0).
///
/// If [end] is present, only bytes up to byte-index [end] will be read.
/// Otherwise, until end of file.
@pragma('vm:prefer-inline')
Future<HashDigest> file(input, [int start = 0, int? end]) {
throw UnsupportedError('Unavailable for this platform');
}

/// Converts the [input] file and returns a [HashDigest] synchronously.
///
/// If [start] is present, the file will be read from byte-offset [start].
/// Otherwise from the beginning (index 0).
///
/// If [end] is present, only bytes up to byte-index [end] will be read.
/// Otherwise, until end of file.
///
/// If [bufferSize] is present, the file will be read in chunks of this size.
/// By default the [bufferSize] is `2048`.
HashDigest fileSync(
input, {
int start = 0,
int? end,
int bufferSize = 2048,
}) {
throw UnsupportedError('Unavailable for this platform');
}
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: hashlib
description: Secure hash functions, checksum generators, and key derivation algorithms optimized for Dart.
homepage: https://github.com/bitanon/hashlib
version: 1.18.0
version: 1.18.1

environment:
sdk: '>=2.14.0 <4.0.0'
Expand Down
8 changes: 4 additions & 4 deletions test/compare_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,14 @@ void main() {
var hash = await crypto.md5.bind(file.openRead()).first;
var hash2 = await md5.file(file);
expect(hash2.hex(), toHex(hash.bytes));
}, tags: 'skip-js');
}, tags: 'vm-only');

test('for a file sync', () async {
var file = File('LICENSE');
var hash = await crypto.md5.bind(file.openRead()).first;
var hash2 = md5.fileSync(file);
expect(hash2.hex(), toHex(hash.bytes));
}, tags: 'skip-js');
}, tags: 'vm-only');

test('with crypto', () {
for (int i = 0; i < 100; ++i) {
Expand Down Expand Up @@ -201,7 +201,7 @@ void main() {
var hash = pc_md4.MD4Digest().process(file.readAsBytesSync());
var hash2 = md4.fileSync(file);
expect(hash2.hex(), toHex(hash));
}, tags: 'skip-js');
}, tags: 'vm-only');
});

group('SM3 comparison', () {
Expand All @@ -221,7 +221,7 @@ void main() {
var hash = pc_sm3.SM3Digest().process(file.readAsBytesSync());
var hash2 = sm3.fileSync(file);
expect(hash2.hex(), toHex(hash));
}, tags: 'skip-js');
}, tags: 'vm-only');
});

group('SHA1 comparison', () {
Expand Down
10 changes: 5 additions & 5 deletions test/ripemd128_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ void main() {
expect(ripemd128sum(m[i]), r[i]);
}
});
// test('with a millian "a"', () {
// var m = List<int>.filled(1000000, 'a'.codeUnitAt(0));
// var r = "4a7f5723f954eba1216c9d8f6320431f";
// expect(ripemd128.convert(m).hex(), r);
// }, tags: ['skip-js']);
test('with a millian "a"', () {
var m = List<int>.filled(1000000, 'a'.codeUnitAt(0));
var r = "4a7f5723f954eba1216c9d8f6320431f";
expect(ripemd128.convert(m).hex(), r);
}, skip: true);
});
}
2 changes: 1 addition & 1 deletion test/ripemd160_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ void main() {
var m = List<int>.filled(1000000, 'a'.codeUnitAt(0));
var r = '52783243c1697bdbe16d37f97f68f08325dc1528';
expect(ripemd160.convert(m).hex(), r);
}, tags: ['skip-js']);
}, skip: true);
test('string: "Hello, world!"', () {
var m = "Hello, world!";
var r = "58262d1fbdbe4530d8865d3518c6d6e41002610f";
Expand Down
2 changes: 1 addition & 1 deletion test/ripemd256_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ void main() {
var r = "ac953744e10e31514c150d4d8d7b6773"
"42e33399788296e43ae4850ce4f97978";
expect(ripemd256.convert(m).hex(), r);
}, tags: ['skip-js']);
}, skip: true);
});
}
2 changes: 1 addition & 1 deletion test/ripemd320_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ void main() {
var r = "bdee37f4371e20646b8b0d862dda16292ae36f4"
"0965e8c8509e63d1dbddecc503e2b63eb9245bb66";
expect(ripemd320.convert(m).hex(), r);
}, tags: ['skip-js']);
}, skip: true);
});
}

0 comments on commit 5add7d0

Please sign in to comment.