Gathering detailed insights and metrics for gjw-ec-key
Gathering detailed insights and metrics for gjw-ec-key
Gathering detailed insights and metrics for gjw-ec-key
Gathering detailed insights and metrics for gjw-ec-key
npm install gjw-ec-key
Typescript
Module System
Min. Node Version
Node Version
NPM Version
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
1
3
This project defines a wrapper for Elliptic Curve (EC) private and public keys.
This wrapper supports onl the three main curves listed below:
OpenSSL Curve Name | RFC-7518 (6.2.1.1) | ASN.1 OID |
---|---|---|
prime256v1 | P-256 | 1.2.840.10045.3.1.7 |
secp384k1 | P-256K non standard | 1.3.132.0.10 |
secp384r1 | P-384 | 1.3.132.0.34 |
secp521r1 | P-521 | 1.3.132.0.35 |
Both the OpenSSL names and RFC-7518 (JWA/JWK) names can be used as parameters
to the methods in the ECKey
class.
Please be aware that NodeJS (and OpenSSL) support a large number of curves
(see openssl ecparam -list_curves
for a full list), but for brevity this
implementation restricts to the three mentioned above.
PLEASE NOTE: The
P-256K
curve name (crv
parameter) used when serializing a key using thesecp384k1
curve is not standard, and NOT interoperable with other systems.See the IANA registry for all known (and interoperable) curve names.
The
P-256K
name used might change at ANY time.
To use, start importing the main ECKey
class:
1const ECKey = require('ec-key');
To create a random ECKey
instance simply call the createECKey
static
method, optionally specifying a curve name (defaults to prime256v1
):
1// Create a new (random) ECKey instance using the secp521r1 curve 2var randomKey = ECKey.createECKey('P-521');
To import an existing private or public key, simply invoke the constructor
with a String
or a Buffer
and the format in which the key is encoded:
1// Create a new ECKey instance from a base-64 spki string 2var key = new ECKey('MFkwEw ... 3w06qg', 'spki');
For Buffer
s and base64-encoded String
s the constructor supports both the
pkcs8
(or rfc5208
) and spki
(or rfc5280
) formats.
Additionally, the pem
format is supported for unencoded String
s and
Buffer
s:
1// Load up a PEM file and wrap it into a 2var pem = fs.readFileSync('./key.pem'); 3var key = new ECKey(pem, 'pem');
Instances of the ECKey
class can also be created from very simple object.
For example JWKs can be used directly, and whereas in the example below the
crv
, x
and y
values will be considered, kty
and kid
will be ignored.
1/// Simply create from a JWK object 2var key = new ECKey({ 3 "kty":"EC", 4 "crv":"P-256", 5 "x":"f83OJ3D2xF1Bg8vub9tLe1gHMzV76e8Tus9uPHvRVEU", 6 "y":"x_FEzRu9m36HLN_tue659LNpXW6pCyStikYjKIWI5a0", 7 "kid":"Public key used in JWS spec Appendix A.3 example" 8})
The following values are recognized:
curve
or crv
: the curve type for this key, identified either by
its OpenSSL or its RFC-7518 (JWA/JWK) name.With regards to coordinates:
d
: the private d coordinate for the elliptic curve point, either as a
Buffer
, or a base64-encoded String
of the coordinate's big endian
representationx
: the public x coordinate for the elliptic curve point, either as a
Buffer
, or a base64-encoded String
of the coordinate's big endian
representationy
: the public y coordinate for the elliptic curve point, either as a
Buffer
, or a base64-encoded String
of the coordinate's big endian
representationAnd also:
publicKey
: the uncompressed and prefixed (0x04) concatenation of
the x and y public coordinates' big endian representation, as described
in SEC-1 ECC section 2.3.3privateKey
: the private d coordinate for the elliptic curve point,
either as a Buffer
, or a base64-encoded String
of the coordinate's big
endian representationThe following enumerable properties are available for instances of ECKey
:
curve
: the EC key curve name in OpenSSL format (e.g. prime256v1
)isPrivateECKey
: a boolean indicating whther this instance represents
a private or public EC key.x
: the public x coordinate's big endian representation for the
elliptic curve point as a Buffer
y
: the public y coordinate's big endian representation for the
elliptic curve point as a Buffer
d
: the private d coordinate's big endian representation for the
elliptic curve point as a Buffer
Additionally the following properties are available, but not enumerable:
jsonCurve
: the EC key curve name in RFC-7518 format (e.g. P-256
)publicCodePoint
: the uncompressed and prefixed (0x04) concatenation of
the x and y public coordinates' big endian representation, as described
in SEC-1 ECC section 2.3.3.asPublicECKey()
Return this
instance if this key is a public key, or create a new ECKey
instance not including the private components of the key.
computeSecret(otherKey)
A simple shortcut for createECDH().computeSecret(otherKey)
as explained below.
createECDH()
Create a standard Node ECDH
object instance whose computeSecret(...)
function accepts also ECKey
(as in, this class) instances.
createSign(hash)
Create a standard Node Sign
object whose sign(...)
function is automatically populated with this instance.
hash
: the hashing function to use for generating the signature, normally one
of SHA256
, SHA384
or SHA512
.1// Create a signature of the message "the quick brown fox" with a random key 2var message = "the quick brown fox"; 3var key = ECKey.createECKey('P-384'); 4var signature = key.createSign('SHA384') 5 .update(message) 6 .sign('base64');
createVerify(hash)
Create a standard Node Verify
object whose verify(...)
function is automatically populated with this instance.
hash
: the hashing function to use for generating the signature, normally one
of SHA256
, SHA384
or SHA512
.1// Verify the signature calcuated above 2key.createVerify('SHA384') 3 .update(message) 4 .verify(signature, 'base64');
toBuffer(format)
Encode this EC key, optionally using the specified format (defaults to pem
).
Formats supported are as follows:
pem
: return a Buffer
containing the ascii
represtation of the
OpenSSL PEM format
new Buffer(key.toString('pem'), 'ascii')
and provided for
convenience onlyrfc5951
: (private keys only) returns the encoding of this key as
specified by RFC-5951pkcs8
or rfc5208
: (private keys only) returns the PKCS8 encoding
of this key as specified by RFC-5208spki
or rfc5280
: (public keys only) returns the SPKI encoding
of this key as specified by RFC-5280toString(format)
Encode this EC key, optionally using the specified format (defaults to pem
).
Formats supported are as follows:
pem
: return the key in OpenSSL's PEM formatrfc5951
: (private keys only) returns the encoding of this key as
specified by RFC-5951, wrapped with
a header and footer as outlined in section 4pkcs8
or rfc5208
: (private keys only) returns the PKCS8 encoding
of this key as specified by RFC-5208
encoded in base64spki
or rfc5280
: (public keys only) returns the SPKI encoding
of this key as specified by RFC-5280
encoded in base64toJSON()
Formats this ECKey
as a JSON Web Key as specified by
RFC-7517.
Please note that his function will also be called by the JSON.stringify(...)
function.
1// Convert a PEM to a JWK in one easy step 2 3var pem = `-----BEGIN PRIVATE KEY----- 4MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgzr+Twxehecu0VYoC 5XUBL1Z4h3H28gPnJ5MP0AcOixAOhRANCAAS6pMWMMndZxOPSC9ui6sUUbmeK6dIi 6k3ZwTmm0SE7G+tYon5C57aVek5qH4y4OipbSLfbsIQuOkt0G8Vu1KZ3u 7-----END PRIVATE KEY-----`; 8 9var key = new ECKey(pem, 'pem'); 10 11var jwk = JSON.stringify(key, null, 2); 12 13console.log(jwk); 14 15// This will result in the following output: 16// { 17// "kty": "EC", 18// "crv": "P-256", 19// "x": "uqTFjDJ3WcTj0gvbourFFG5niunSIpN2cE5ptEhOxvo", 20// "y": "1iifkLntpV6TmofjLg6KltIt9uwhC46S3QbxW7Upne4", 21// "d": "zr-Twxehecu0VYoCXUBL1Z4h3H28gPnJ5MP0AcOixAM" 22// }
No vulnerabilities found.
No security vulnerabilities found.