GeezerVison is showing github.com — generateKeyPairSync doesn't return KeyObject · Issue #25322 · nodejs/node
generateKeyPairSync doesn't return KeyObject #25322Labels confirmed-bugIssues and PRs for confirmed bugs.cryptoIssues and PRs related to the crypto subsystem. Description
- Version: v11.6.0
- Platform: macOS Mojave (10.14.2) Darwin C02TT3JQHTD6 18.2.0 Darwin Kernel Version 18.2.0: Mon Nov 12 20:24:46 PST 2018; root:xnu-4903.231.4~2/RELEASE_X86_64 x86_64
From crypto.generateKeyPair documentation
If a publicKeyEncoding or privateKeyEncoding was specified, this function behaves as if keyObject.export() had been called on its result. Otherwise, the respective part of the key is returned as a KeyObject.
This documentation part is missing from crypto.generateKeyPairSync but you can still omit the encoding objects, then a simple empty object {} is returned instead of an expected KeyObject.
/cc @tniessen
Examples:
const { generateKeyPairSync } = require('crypto')
console.log('generateKeyPairSync result', generateKeyPairSync('rsa', { modulusLength: 2048 }))
// => generateKeyPairSync result { publicKey: {}, privateKey: {} }Activitychanged the title [-]generateKeyPair (+sync) doesn't return KeyObject as documented[/-] [+]generateKeyPair (+sync) doesn't return KeyObject as documented when promisified[/+] on Jan 3, 2019changed the title [-]generateKeyPair (+sync) doesn't return KeyObject as documented when promisified[/-] [+]generateKeyPairSync doesn't return KeyObject[/+] on Jan 3, 2019tniessen commented on Jan 3, 2019 Thanks for reporting this! The problem is that the native (C++) key object is being returned, not the JavaScript key object, that's why the object appears to be empty.
> const { publicKey, privateKey } = crypto.generateKeyPairSync('rsa', { modulusLength: 2048 })
undefined
> publicKey.getAsymmetricKeyType()
'rsa'
> publicKey.getSymmetricKeySize()
C:\WINDOWS\system32\cmd.exe - node[6416]: src\node_crypto.cc:3350: Assertion `(key_type_) == (kKeyTypeSecret)' failed.
1: 00007FF7F0EC3FDA v8::internal::GCIdleTimeHandler::GCIdleTimeHandler+4714
2: 00007FF7F0E68AD6 uv_loop_fork+80918
3: 00007FF7F0E68B8F uv_loop_fork+81103
4: 00007FF7F0D8682F std::vector<v8::CpuProfileDeoptFrame,std::allocator<v8::CpuProfileDeoptFrame> >::vector<v8::CpuProfileDeoptFrame,std::allocator<v8::CpuProfileDeoptFrame> >+102815
5: 00007FF7F12D20BE v8::internal::ZoneVector<v8::internal::compiler::MoveOperands * __ptr64>::ZoneVector<v8::internal::compiler::MoveOperands * __ptr64>+59870
6: 00007FF7F12D3620 v8::internal::ZoneVector<v8::internal::compiler::MoveOperands * __ptr64>::ZoneVector<v8::internal::compiler::MoveOperands * __ptr64>+65344
7: 00007FF7F12D25B9 v8::internal::ZoneVector<v8::internal::compiler::MoveOperands * __ptr64>::ZoneVector<v8::internal::compiler::MoveOperands * __ptr64>+61145
8: 00007FF7F12D249B v8::internal::ZoneVector<v8::internal::compiler::MoveOperands * __ptr64>::ZoneVector<v8::internal::compiler::MoveOperands * __ptr64>+60859
9: 00000038CF650361tniessen commented on Dec 19, 2019 @cqupt-yifanwu Your code creates a key pair and returns them as a pair of Buffers, because you specified the publicKeyEncoding and privateKeyEncoding options. Buffer does not have properties such as getAsymmetricKeyType and getSymmetricKeySize. You can either remove the encoding options and use KeyObjects, or create KeyObjects from the returned Buffers. cqupt-yifanwu commented on Dec 19, 2019
@cqupt-yifanwu Your code creates a key pair and returns them as a pair of Buffers, because you specified the publicKeyEncoding and privateKeyEncoding options. Buffer does not have properties such as getAsymmetricKeyType and getSymmetricKeySize. You can either remove the encoding options and use KeyObjects, or create KeyObjects from the returned Buffers.
i want to Communication with java Server in ECDH encryption way ,but the generateKeyPairSync retruns privateKey has different length with java server tniessen commented on Dec 19, 2019 @cqupt-yifanwu Please open a new issue for that (preferably in nodejs/help), and please include code to reproduce your problem!
MetadataMetadataconfirmed-bugIssues and PRs for confirmed bugs.cryptoIssues and PRs related to the crypto subsystem. DevelopmentNo branches or pull requestsIssue actions
|