如何用node.js实现ECDSA?
发布于 6 年前 作者 mashford 4560 次浏览 来自 问答

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

2 回复

indutny/elliptic 这个库有人用过吗

正好之前写过,本来打算写一个区块链玩玩,后来实在太懒没坚持下去。 就相对完整实现了钱包,其它都半实现,钱包实现的核心就是这个算法 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));
    }
回到顶部