ilap / pinenacl-dart

The Dart implementation of the PyNaCl API with the TweetNaCl cryptographic library
MIT License
36 stars 4 forks source link

Unhandled Exception: LateInitializationError: Field 'prefixLength' has not been initialized #10

Closed Andrew-Bekhiet closed 3 years ago

Andrew-Bekhiet commented 3 years ago

Hi, I've found a bug here and I think this is a simple one Happens after constructing new ed25519.SigningKey using generate() constructor and then retriving the private or the public key bytes using prefix or suffix

ilap commented 3 years ago

The suffix and prefix should not be visible publicly and should be private properties. Unfortunately they cannot be due to the dart's constraints.

Anyway, the SigningKey class did not use suffix for publicKey but used prefix for keyBytes, which can cause issue as the prefixLength was not implicitly declared.

So, best practice is: do not use prefix and suffix as they should be used internally and use keyBytes if you're interested for the seed or the newly created seed or verifyKey/publicKey for the publicKey part of the private key.

Thanks, for pointing this out to me. I have fixed the signingkey by adding the proper suffix and prefix and created a new getter for seed part of the key.

ilap commented 3 years ago

Fixed in the latest master

Andrew-Bekhiet commented 3 years ago

I know it didn't use suffix but when I looked at the code I realized that prefix and suffix use the same prefixLength variable so the bug affects both of them

Anyway, I used the signingKey.keyBytes.take(32) as a temporary workaround

BTW you can use @protected annotation to point out that these variable are intended only for internal use

Anyway thanks for fixing this

ilap commented 3 years ago

BTW you can use @protected annotation to point out that these variable are intended only for internal use

I do not use meta package, though in the future I could introduce it for static analyzing.