【问题标题】:Is it possible to sign a message with a base58 secret key with Tweetnacl library?是否可以使用 Tweetnacl 库使用 base58 密钥签署消息?
【发布时间】:2019-09-20 06:41:22
【问题描述】:

我已经使用Tweetnacl.js 生成了一个密钥对:

const base58KeyPairGenerator = (seed: Uint8Array) => ({
    publicKey: keyToPublicIdentityKey(nacl.sign.keyPair.fromSeed(seed).publicKey),
    secretKey: seedToSecretIdentityKey(seed)
});

const keyToPublicIdentityKey = (key) => {
    return keyToIdentityKey(key, 'idpub');
};

const seedToSecretIdentityKey = (seed) => {
    return keyToIdentityKey(seed, 'idsec');
};

const keyToIdentityKey = (key, prefix) => {
    const keyBuffer = Buffer.from(key, 'hex');
    if (keyBuffer.length !== 32) {
        throw new Error('Key/seed must be 32 bytes long.');
    }
    const address = Buffer.concat([prefix, keyBuffer]);
    const checksum = sha256d(address).slice(0, 4);
    return base58.encode(Buffer.concat([address, checksum]));
};

const sha256d = (data) => {
    return Buffer.from(
        sha256()
            .update(
                sha256()
                    .update(data)
                    .digest()
            )
            .digest()
    );
};

const keyPair = base58KeyPairGenerator(nacl.randomBytes(32));

现在我有一个 base58 密钥对(base58 中的公钥和密钥),我想用这样的密钥对消息进行签名:

nacl.sign(
          Buffer.from(someStringMessage, 'hex'),
          base58.decode(keyPair.secretKey)
        )

base58 来自this 库。

但是我得到这个错误:

bad secret key size

      at Object.<anonymous>.nacl.sign (node_modules/tweetnacl/nacl-fast.js:2257:11)

确实,nacl.sign 函数需要一个 64 位的密钥,而我的 base58 版本不是这种情况。

有没有办法在保留 base58 的同时修复它,或者我应该使用nacl.randomBytes(32) 生成的原始 Ed25519 格式,即未转换的格式?

【问题讨论】:

    标签: javascript cryptography key signature nacl-cryptography


    【解决方案1】:

    话虽如此,NaCl 签名密钥是 64 字节,而不是 32 字节。因此您会得到错误。

    base58KeyPairGenerator 函数中,密钥必须是nacl.sign.keyPair.fromSeed(seed).secretKey(或.privateKey 或其他名称)的输出,而不仅仅是种子。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-04
      • 1970-01-01
      • 1970-01-01
      • 2022-01-28
      相关资源
      最近更新 更多