crypto.generateKeyPairSync('ed25519') 不验证简单测试,'ec' 密钥对可以毫无问题地进行验证。错误是什么?

crypto.generateKeyPairSync('ed25519') does not verify simple test, which an 'ec' keypair verifies without issue. What is the mistake?

一直在使用 Nodejs 中的加密模块并使用 crypto.generateKeyPairSync。当 运行 使用 'ec' 密钥进行简短验证测试时,这会按预期进行评估,但当使用 'ed25519' 生成的密钥进行测试时,这永远不会评估为真。

  const crypto = require('crypto')

  let identity = {}

  identity.identifierKeys__ = await crypto.generateKeyPairSync('ec', {
    namedCurve: 'secp256k1', // Options
    publicKeyEncoding: {
      type: 'spki',
      format: 'pem'
    },
    privateKeyEncoding: {
      type: 'pkcs8',
      format: 'pem'
    }
  })
  identity.identifierKeys = await crypto.generateKeyPairSync('ed25519', {
    publicKeyEncoding: {
      type: 'spki',
      format: 'pem'
    },
    privateKeyEncoding: {
      type: 'pkcs8',
      format: 'pem'
    }
  })
// console.log(identity.identifierKeys)
// test keys
  let key = identity.identifierKeys__ // if i swap to .identifierKeys, never evaluates to true
  let msg = 'testingInformation'
  let v = await crypto.createSign('SHA256')
      v.write(msg)
      v.end()
  let sig = await v.sign(key.privateKey, 'base64')
  let t = await crypto.createVerify('SHA256')
      t.write(msg)
      t.end()
  let r = await t.verify(key.publicKey, sig, 'base64')

  console.log(sig)
  console.log(r)

NodeJS 为 signing/verifying、Sign/Verify class (since v0.1.92) and crypto.sign()/crypto.verify()(自 v12.0.0 起)提供了两个实现。

使用 crypto.sign()crypto.verify() 可以使用 Ed25519(和 Ed448)进行签名和验证:

...
let msg = 'testingInformation'
var signature = crypto.sign(null, Buffer.from(msg), identity.identifierKeys.privateKey)
var verified = crypto.verify(null, Buffer.from(msg), identity.identifierKeys.publicKey, signature)
console.log(signature.toString('base64'))
console.log(verified)