简体   繁体   English

在 NodeJS 中将私有以太坊密钥更改为 PEM 或 HMAC 算法

[英]Changing a Private Ethereum Key into a PEM or HMAC algorithm in NodeJS

I have a private key, which is:我有一个私钥,即:

0x66228b427f07b168c7cb1380aa5554403b9e24bf935a9364c89711a306a23ddb

NOTE: The Original Key was the Public Key when the question was posted: 0x6a2043113D5640F9F50dC593D0DcD8CC1c970EB1注意:原始密钥是发布问题时的公钥: 0x6a2043113D5640F9F50dC593D0DcD8CC1c970EB1
I generated it randomly for this question, so don't worry that it's public on the Internet.我为这个问题随机生成了它,所以不用担心它在互联网上是公开的。

I want to use it as the encryption scheme for a JWT token, using the ES256 algorithm.我想使用 ES256 算法将其用作 JWT 令牌的加密方案。

To get it into my library, I have to have it in one these formats.要将其放入我的库中,我必须以其中一种格式保存它。

secretOrPrivateKey is a string, buffer, or object containing either the secret for HMAC algorithms or the PEM encoded private key for RSA and ECDSA. secretOrPrivateKey 是一个字符串、缓冲区或 object,其中包含 HMAC 算法的秘密或 RSA 和 ECDSA 的 PEM 编码私钥。 In case of a private key with passphrase an object { key, passphrase } can be used (based on crypto documentation), in this case be sure you pass the algorithm option.对于带有密码的私钥,可以使用 object { key, passphrase }(基于加密文档),在这种情况下,请确保您通过了算法选项。

Is there any easy way to move the hex value to one of these formats?有没有简单的方法将十六进制值移动到这些格式之一? Or, will this not work because I'm not understanding the difference between an Ethereum Private key and a ES256?或者,这会不会因为我不了解以太坊私钥和 ES256 之间的区别而不起作用?

Etherium keys are actually ES256K and not ES256 so in this case it might not be possible. Etherium 密钥实际上是 ES256K 而不是 ES256,所以在这种情况下它可能是不可能的。

If you need to turn the key in PEM in NodeJS there is a library called key-encoder for this specific purpose.如果您需要在 NodeJS 中打开 PEM 中的密钥,则有一个名为 key-encoder 的库用于此特定目的。 Which can be accomplished with the following code.这可以通过以下代码来完成。

var KeyEncoder = require('key-encoder');
console.log(KeyEncoder.default);
let keyEncoder = new KeyEncoder.default('secp256k1')

var pemPrivateKey = keyEncoder.encodePrivate('0x66228b427f07b168c7cb1380aa5554403b9e24bf935a9364c89711a306a23ddb', 'raw', 'pem')
console.log(pemPrivateKey);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM