bcgit / pc-dart

Pointy Castle - Dart Derived Bouncy Castle APIs
MIT License
242 stars 125 forks source link

Full width int not supported on web #126

Open Paradoxu opened 3 years ago

Paradoxu commented 3 years ago

I took this example from the Argon2 test:

final parameters = Argon2Parameters(
  Argon2Parameters.ARGON2_d,
  latin1.encode('mySaltString'),
  desiredKeyLength: 32,
  version: Argon2Parameters.ARGON2_VERSION_10,
  iterations: 3,
  memoryPowerOf2: 32,
);

final gen = Argon2BytesGenerator()..init(parameters);
final valueBytes = Uint8List.fromList(utf8.encode(value));
final result = gen.process(valueBytes);

return String.fromCharCodes(result);

But when I use it on Google Chrome I get this exception:

Error: full width integer not supported on this platform
    at Object.throw_ [as throw] (http://localhost:64504/dart_sdk.js:5348:11)
    at web.PlatformWeb.new.assertFullWidthInteger (http://localhost:64504/packages/pointycastle/src/platform_check/web.dart.lib.js:35:19)
    at new argon2.Argon2BytesGenerator.new (http://localhost:64504/packages/pointycastle/ecc/curves/secp521r1.dart.lib.js:8488:38)
    at hash (http://localhost:64504/packages/passy/features/data/hasher.dart.lib.js:47:26)
    at hash.next (<anonymous>)
    at runBody (http://localhost:64504/dart_sdk.js:39250:34)
    at Object._async [as async] (http://localhost:64504/dart_sdk.js:39281:7)
    at hasher.Hasher.new.hash (http://localhost:64504/packages/passy/features/data/hasher.dart.lib.js:44:20)
    at main$ (http://localhost:64504/packages/passy/main.dart.lib.js:580:107)
    at main$.next (<anonymous>)
    at runBody (http://localhost:64504/dart_sdk.js:39250:34)
    at Object._async [as async] (http://localhost:64504/dart_sdk.js:39281:7)
    at main$ (http://localhost:64504/packages/passy/main.dart.lib.js:576:18)
    at main (http://localhost:64504/web_entrypoint.dart.lib.js:36:29)
    at main.next (<anonymous>)
    at http://localhost:64504/dart_sdk.js:39230:33
    at _RootZone.runUnary (http://localhost:64504/dart_sdk.js:39087:58)
    at _FutureListener.thenAwait.handleValue (http://localhost:64504/dart_sdk.js:34073:29)
    at handleValueCallback (http://localhost:64504/dart_sdk.js:34633:49)
    at Function._propagateToListeners (http://localhost:64504/dart_sdk.js:34671:17)
    at _Future.new.[_completeWithValue] (http://localhost:64504/dart_sdk.js:34513:23)
    at http://localhost:64504/dart_sdk.js:33724:46

And when I try it on an android device I get this error:

E/flutter ( 8615): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: Out of Memory
E/flutter ( 8615): #0      new _List (dart:core-patch/array.dart)
E/flutter ( 8615): #1      List._allocateData (dart:core-patch/growable_array.dart:356:16)
E/flutter ( 8615): #2      new _GrowableList (dart:core-patch/growable_array.dart:97:16)
E/flutter ( 8615): #3      new _GrowableList.generate (dart:core-patch/growable_array.dart:131:20)
E/flutter ( 8615): #4      Argon2BytesGenerator._initMemory
package:pointycastle/key_derivators/argon2.dart:141
E/flutter ( 8615): #5      Argon2BytesGenerator._doInit
package:pointycastle/key_derivators/argon2.dart:137
E/flutter ( 8615): #6      Argon2BytesGenerator.init
package:pointycastle/key_derivators/argon2.dart:90
E/flutter ( 8615): #7      Hasher.hash
package:passy/…/data/hasher.dart:38
E/flutter ( 8615): #8      main
package:passy/main.dart:30
E/flutter ( 8615): #9      _runMainZoned.<anonymous closure>.<anonymous closure> (dart:ui/hooks.dart:142:25)
E/flutter ( 8615): #10     _rootRun (dart:async/zone.dart:1354:13)
E/flutter ( 8615): #11     _CustomZone.run (dart:async/zone.dart:1258:19)
E/flutter ( 8615): #12     _runZoned (dart:async/zone.dart:1789:10)
E/flutter ( 8615): #13     runZonedGuarded (dart:async/zone.dart:1777:12)
E/flutter ( 8615): #14     _runMainZoned.<anonymous closure> (dart:ui/hooks.dart:138:5)
E/flutter ( 8615): #15     _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:283:19)
E/flutter ( 8615): #16     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:184:12)

Am I calling the library with wrong parameters? If so, how should I use it?

Flutter version: 2.2.3 Windows: 10 Android: 11 Chrome version: 92.0.4515.107

AKushWarrior commented 3 years ago

Argon2 is not supported on web currently; it requires 64-bit integers, and we have no way to simulate that behavior from Dart code compiled to JS.

The Android one is a bug.

Paradoxu commented 3 years ago

Argon2 is not supported on web currently; it requires 64-bit integers, and we have no way to simulate that behavior from Dart code compiled to JS.

The Android one is a bug.

Shouldn't be possibile to use the BigInt to represent such large numbers? 🤔 never tested but both Dart and JS has this class to represent integers larger than 2^53-1

licy183 commented 3 years ago

PR #133 may be helpful. But I don't think implementing argon2 in pure dart is a good choice.

willbinge commented 2 years ago

Are there plans to make a similar fix for Poly1305, which still has the calls to assertFullWidthInteger?

levischechter commented 1 year ago

Are there plans to make a similar fix for Poly1305, which still has the calls to assertFullWidthInteger?

join the question

aminjoharinia commented 2 months ago

I used this package: cryptography https://pub.dev/packages/cryptography