CNode

如何用node.js实现ECDSA?

问答
Mmashford发布于8 年前最后回复8 年前2 回复5320 浏览0 收藏

api里有ECDH封装,如何优雅地实现ECDSA?

查看回复

回复 (2)

M
mashford#1·8 年前

indutny/elliptic 这个库有人用过吗

Z
zy445566#2·8 年前

正好之前写过,本来打算写一个区块链玩玩,后来实在太懒没坚持下去。 就相对完整实现了钱包,其它都半实现,钱包实现的核心就是这个算法 https://github.com/zy445566/funcoin-core/blob/master/lib/Wallet.js

const ecEncodeName = "secp256k1";
const ec = new require("elliptic").ec(ecEncodeName);

/**
 * sign
 */
    sign(msg,encoding = 'hex')
    {
        let buffer = Buffer.from(ec.sign(msg, this.getPrivateKey(), {canonical: true}).toDER());
        if (['latin1','base64','hex'].indexOf(encoding)>-1){return buffer.toString(encoding)}
        return buffer;
    }

/**
 * verify
 */
    verify(msg,signature,otherPublicKey,signatureEncoding='hex',otherPublicKeyEncoding='hex')
    {
        return ec.verify(msg, Buffer.from(signature,signatureEncoding), Buffer.from(otherPublicKey,otherPublicKeyEncoding));
    }
参与回复
登录后即可参与回复。登录