Skip to content

didkovsky/keys-converter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Elliptic curve's keys converter

Tool that can convert binary private or public key to pem and der formats. Also includes factory for KeyObject from @node/crypto.

Install

npm i @didkovsky/keys-converter

Example

random private key for (secp256k1)

9d8ce6ab12507d729b9d160d352a0be329ec73dbc8afa65d6e5570bb14a8f71f

pem

-----BEGIN EC PRIVATE KEY-----
MHQCAQEEIJ2M5qsSUH1ym50WDTUqC+Mp7HPbyK+mXW5VcLsUqPcfoAcGBSuBBAAK
oUQDQgAE4zjQ9EYcK1uUoF/eCboVjNVlCOFH58VV6RmyZfZzpSrQjltCJQVubOmt
LLD1FWKZv76d4H4UNc/0mN0LkMKGDw==
-----END EC PRIVATE KEY-----

der (hex)

307402010104209d8ce6ab12507d729b9d160d352a0be329ec73dbc8afa65d6e5570bb14a8f71fa00706052b8104000aa14403420004e338d0f4461c2b5b94a05fde09ba158cd56508e147e7c555e919b265f673a52ad08e5b4225056e6ce9ad2cb0f5156299bfbe9de07e1435cff498dd0b90c2860f

Supported curves

Name Priv length (bytes) Pub length (bytes) Priv header Priv options Pub header
SM2 32 65 0x30770201010420 0xa00a06082a811ccf5501822da144034200 0x3059301306072a8648ce3d020106082a811ccf5501822d034200
brainpoolP160r1 20 41 0x30540201010414 0xa00b06092b2403030208010101a12c032a00 0x3042301406072a8648ce3d020106092b2403030208010101032a00
brainpoolP160t1 20 41 0x30540201010414 0xa00b06092b2403030208010102a12c032a00 0x3042301406072a8648ce3d020106092b2403030208010102032a00
brainpoolP192r1 24 49 0x30600201010418 0xa00b06092b2403030208010103a134033200 0x304a301406072a8648ce3d020106092b2403030208010103033200
brainpoolP192t1 24 49 0x30600201010418 0xa00b06092b2403030208010104a134033200 0x304a301406072a8648ce3d020106092b2403030208010104033200
brainpoolP224r1 28 57 0x306c020101041c 0xa00b06092b2403030208010105a13c033a00 0x3052301406072a8648ce3d020106092b2403030208010105033a00
brainpoolP224t1 28 57 0x306c020101041c 0xa00b06092b2403030208010106a13c033a00 0x3052301406072a8648ce3d020106092b2403030208010106033a00
brainpoolP256r1 32 65 0x30780201010420 0xa00b06092b2403030208010107a144034200 0x305a301406072a8648ce3d020106092b2403030208010107034200
brainpoolP256t1 32 65 0x30780201010420 0xa00b06092b2403030208010108a144034200 0x305a301406072a8648ce3d020106092b2403030208010108034200
brainpoolP320r1 40 81 0x3081900201010428 0xa00b06092b2403030208010109a154035200 0x306a301406072a8648ce3d020106092b2403030208010109035200
brainpoolP320t1 40 81 0x3081900201010428 0xa00b06092b240303020801010aa154035200 0x306a301406072a8648ce3d020106092b240303020801010a035200
brainpoolP384r1 48 97 0x3081a80201010430 0xa00b06092b240303020801010ba164036200 0x307a301406072a8648ce3d020106092b240303020801010b036200
brainpoolP384t1 48 97 0x3081a80201010430 0xa00b06092b240303020801010ca164036200 0x307a301406072a8648ce3d020106092b240303020801010c036200
brainpoolP512r1 64 129 0x3081da0201010440 0xa00b06092b240303020801010da1818503818200 0x30819b301406072a8648ce3d020106092b240303020801010d03818200
brainpoolP512t1 64 129 0x3081da0201010440 0xa00b06092b240303020801010ea1818503818200 0x30819b301406072a8648ce3d020106092b240303020801010e03818200
c2pnb163v1 21 43 0x30560201010415 0xa00a06082a8648ce3d030001a12e032c00 0x3043301306072a8648ce3d020106082a8648ce3d030001032c00
c2pnb163v2 21 43 0x30560201010415 0xa00a06082a8648ce3d030002a12e032c00 0x3043301306072a8648ce3d020106082a8648ce3d030002032c00
c2pnb163v3 21 43 0x30560201010415 0xa00a06082a8648ce3d030003a12e032c00 0x3043301306072a8648ce3d020106082a8648ce3d030003032c00
c2pnb208w1 24 53 0x3064020101041900 0xa00a06082a8648ce3d03000aa138033600 0x304d301306072a8648ce3d020106082a8648ce3d03000a033600
c2pnb272w1 32 69 0x307c020101042100 0xa00a06082a8648ce3d030010a148034600 0x305d301306072a8648ce3d020106082a8648ce3d030010034600
c2pnb304w1 36 77 0x308188020101042500 0xa00a06082a8648ce3d030011a150034e00 0x3065301306072a8648ce3d020106082a8648ce3d030011034e00
c2pnb368w1 44 93 0x3081a0020101042d00 0xa00a06082a8648ce3d030013a160035e00 0x3075301306072a8648ce3d020106082a8648ce3d030013035e00
c2tnb191v1 24 49 0x305f0201010418 0xa00a06082a8648ce3d030005a134033200 0x3049301306072a8648ce3d020106082a8648ce3d030005033200
c2tnb191v2 24 49 0x305f0201010418 0xa00a06082a8648ce3d030006a134033200 0x3049301306072a8648ce3d020106082a8648ce3d030006033200
c2tnb191v3 24 49 0x305f0201010418 0xa00a06082a8648ce3d030007a134033200 0x3049301306072a8648ce3d020106082a8648ce3d030007033200
c2tnb239v1 30 61 0x3071020101041e 0xa00a06082a8648ce3d03000ba140033e00 0x3055301306072a8648ce3d020106082a8648ce3d03000b033e00
c2tnb239v2 30 61 0x3071020101041e 0xa00a06082a8648ce3d03000ca140033e00 0x3055301306072a8648ce3d020106082a8648ce3d03000c033e00
c2tnb239v3 30 61 0x3071020101041e 0xa00a06082a8648ce3d03000da140033e00 0x3055301306072a8648ce3d020106082a8648ce3d03000d033e00
c2tnb359v1 45 91 0x30819e020101042d 0xa00a06082a8648ce3d030012a15e035c00 0x3073301306072a8648ce3d020106082a8648ce3d030012035c00
c2tnb431r1 53 109 0x3081b80201010435 0xa00a06082a8648ce3d030014a170036e00 0x308185301306072a8648ce3d020106082a8648ce3d030014036e00
prime192v1 24 49 0x305f0201010418 0xa00a06082a8648ce3d030101a134033200 0x3049301306072a8648ce3d020106082a8648ce3d030101033200
prime192v2 24 49 0x305f0201010418 0xa00a06082a8648ce3d030102a134033200 0x3049301306072a8648ce3d020106082a8648ce3d030102033200
prime192v3 24 49 0x305f0201010418 0xa00a06082a8648ce3d030103a134033200 0x3049301306072a8648ce3d020106082a8648ce3d030103033200
prime239v1 30 61 0x3071020101041e 0xa00a06082a8648ce3d030104a140033e00 0x3055301306072a8648ce3d020106082a8648ce3d030104033e00
prime239v2 30 61 0x3071020101041e 0xa00a06082a8648ce3d030105a140033e00 0x3055301306072a8648ce3d020106082a8648ce3d030105033e00
prime239v3 30 61 0x3071020101041e 0xa00a06082a8648ce3d030106a140033e00 0x3055301306072a8648ce3d020106082a8648ce3d030106033e00
prime256v1 32 65 0x30770201010420 0xa00a06082a8648ce3d030107a144034200 0x3059301306072a8648ce3d020106082a8648ce3d030107034200
secp112r1 14 29 0x303e020101040e 0xa00706052b81040006a120031e00 0x3032301006072a8648ce3d020106052b81040006031e00
secp112r2 14 29 0x303e020101040e 0xa00706052b81040007a120031e00 0x3032301006072a8648ce3d020106052b81040007031e00
secp128r1 16 33 0x30440201010410 0xa00706052b8104001ca124032200 0x3036301006072a8648ce3d020106052b8104001c032200
secp128r2 16 33 0x30440201010410 0xa00706052b8104001da124032200 0x3036301006072a8648ce3d020106052b8104001d032200
secp160k1 20 41 0x3051020101041500 0xa00706052b81040009a12c032a00 0x303e301006072a8648ce3d020106052b81040009032a00
secp160r1 20 41 0x3051020101041500 0xa00706052b81040008a12c032a00 0x303e301006072a8648ce3d020106052b81040008032a00
secp160r2 20 41 0x3051020101041500 0xa00706052b8104001ea12c032a00 0x303e301006072a8648ce3d020106052b8104001e032a00
secp192k1 24 49 0x305c0201010418 0xa00706052b8104001fa134033200 0x3046301006072a8648ce3d020106052b8104001f033200
secp224k1 28 57 0x3069020101041d00 0xa00706052b81040020a13c033a00 0x304e301006072a8648ce3d020106052b81040020033a00
secp224r1 28 57 0x3068020101041c 0xa00706052b81040021a13c033a00 0x304e301006072a8648ce3d020106052b81040021033a00
secp256k1 32 65 0x30740201010420 0xa00706052b8104000aa144034200 0x3056301006072a8648ce3d020106052b8104000a034200
secp384r1 48 97 0x3081a40201010430 0xa00706052b81040022a164036200 0x3076301006072a8648ce3d020106052b81040022036200
secp521r1 66 133 0x3081dc0201010442 0xa00706052b81040023a1818903818600 0x30819b301006072a8648ce3d020106052b8104002303818600
sect113r1 14 31 0x3041020101040f00 0xa00706052b81040004a122032000 0x3034301006072a8648ce3d020106052b81040004032000
sect113r2 14 31 0x3041020101040f00 0xa00706052b81040005a122032000 0x3034301006072a8648ce3d020106052b81040005032000
sect131r1 17 35 0x30470201010411 0xa00706052b81040016a126032400 0x3038301006072a8648ce3d020106052b81040016032400
sect131r2 17 35 0x30470201010411 0xa00706052b81040017a126032400 0x3038301006072a8648ce3d020106052b81040017032400
sect163k1 21 43 0x30530201010415 0xa00706052b81040001a12e032c00 0x3040301006072a8648ce3d020106052b81040001032c00
sect163r1 21 43 0x30530201010415 0xa00706052b81040002a12e032c00 0x3040301006072a8648ce3d020106052b81040002032c00
sect163r2 21 43 0x30530201010415 0xa00706052b8104000fa12e032c00 0x3040301006072a8648ce3d020106052b8104000f032c00
sect193r1 24 51 0x305f020101041900 0xa00706052b81040018a136033400 0x3048301006072a8648ce3d020106052b81040018033400
sect193r2 24 51 0x305f020101041900 0xa00706052b81040019a136033400 0x3048301006072a8648ce3d020106052b81040019033400
sect233k1 29 61 0x306d020101041d 0xa00706052b8104001aa140033e00 0x3052301006072a8648ce3d020106052b8104001a033e00
sect233r1 29 61 0x306e020101041e00 0xa00706052b8104001ba140033e00 0x3052301006072a8648ce3d020106052b8104001b033e00
sect239k1 30 61 0x306e020101041e 0xa00706052b81040003a140033e00 0x3052301006072a8648ce3d020106052b81040003033e00
sect283k1 36 73 0x3081800201010424 0xa00706052b81040010a14c034a00 0x305e301006072a8648ce3d020106052b81040010034a00
sect283r1 36 73 0x3081800201010424 0xa00706052b81040011a14c034a00 0x305e301006072a8648ce3d020106052b81040011034a00
sect409k1 51 105 0x3081af0201010433 0xa00706052b81040024a16c036a00 0x307e301006072a8648ce3d020106052b81040024036a00
sect409r1 51 105 0x3081b0020101043400 0xa00706052b81040025a16c036a00 0x307e301006072a8648ce3d020106052b81040025036a00
sect571k1 72 145 0x3081ee0201010448 0xa00706052b81040026a1819503819200 0x3081a7301006072a8648ce3d020106052b8104002603819200
sect571r1 72 145 0x3081ee0201010448 0xa00706052b81040027a1819503819200 0x3081a7301006072a8648ce3d020106052b8104002703819200
wap-wsg-idm-ecid-wtls1 14 31 0x3040020101040e 0xa0070605672b010401a122032000 0x3034301006072a8648ce3d02010605672b010401032000
wap-wsg-idm-ecid-wtls10 29 61 0x306d020101041d 0xa0070605672b01040aa140033e00 0x3052301006072a8648ce3d02010605672b01040a033e00
wap-wsg-idm-ecid-wtls11 29 61 0x306e020101041e00 0xa0070605672b01040ba140033e00 0x3052301006072a8648ce3d02010605672b01040b033e00
wap-wsg-idm-ecid-wtls12 28 57 0x3068020101041c 0xa0070605672b01040ca13c033a00 0x304e301006072a8648ce3d02010605672b01040c033a00
wap-wsg-idm-ecid-wtls3 21 43 0x30530201010415 0xa0070605672b010403a12e032c00 0x3040301006072a8648ce3d02010605672b010403032c00
wap-wsg-idm-ecid-wtls4 14 31 0x3041020101040f00 0xa0070605672b010404a122032000 0x3034301006072a8648ce3d02010605672b010404032000
wap-wsg-idm-ecid-wtls5 21 43 0x30530201010415 0xa0070605672b010405a12e032c00 0x3040301006072a8648ce3d02010605672b010405032c00
wap-wsg-idm-ecid-wtls6 14 29 0x303e020101040e 0xa0070605672b010406a120031e00 0x3032301006072a8648ce3d02010605672b010406031e00
wap-wsg-idm-ecid-wtls7 20 41 0x3051020101041500 0xa0070605672b010407a12c032a00 0x303e301006072a8648ce3d02010605672b010407032a00
wap-wsg-idm-ecid-wtls8 14 29 0x303f020101040f00 0xa0070605672b010408a120031e00 0x3032301006072a8648ce3d02010605672b010408031e00
wap-wsg-idm-ecid-wtls9 20 41 0x3051020101041500 0xa0070605672b010409a12c032a00 0x303e301006072a8648ce3d02010605672b010409032a00

Usage

See /examples folder.

convert private key

const { PrivConverter, getCurves } = require('@didkovsky/keys-converter')

/**
 * Get list of supported curves
 */
const curves = getCurves()

/**
 * Create converter for specified curve.
 */
const converter = PrivConverter.for(curves.secp256k1)

/**
 * Some private key.
 */
const priv = Buffer.alloc(32, 0x1)

/**
 * GetPem string.
 */
const pem = converter.toPem(priv)

/*
-----BEGIN EC PRIVATE KEY-----
MHQCAQEEIAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBoAcGBSuBBAAK
oUQDQgAEG4TFVnsSZECZXT7VqroFZdceGDRgSBn/nBf16dXdB49wvq+PWItUFQf+
1qZCxatC39+BIKf2Od5RItR6aajo0Q==
-----END EC PRIVATE KEY-----
*/
console.log(pem)

/**
 * Get der Buffer.
 */
const der = converter.toDer(priv)

// Buffer(118) [Uint8Array] [ ... ]
console.dir(der)

/**
 * Get KeyObject from @node/crypto
 */
const keyObj = converter.toKeyObject(priv)

// PrivateKeyObject { [Symbol(kKeyType)]: 'private' }
console.dir(keyObj)

convert public key

const { PubConverter, getCurves } = require('@didkovsky/keys-converter')
const { createECDH } = require('crypto')

/**
 * Get list of supported curves
 */
const curves = getCurves()

/**
 * Create converter for specified curve.
 */
const converter = PubConverter.for(curves.secp256k1)

/**
 * Create keypair
 */
const priv = Buffer.alloc(32, 0x1)
const ecdh = createECDH('secp256k1')
ecdh.setPrivateKey(priv)
const pub = ecdh.getPublicKey()

// BBuExVZ7EmRAmV0+1aq6BWXXHhg0YEgZ/5wX9enV3QePcL6vj1iLVBUH/tamQsWrQt/fgSCn9jneUSLUemmo6NE=
console.log(pub.toString('base64'))

/**
 * GetPem string.
 */
const pem = converter.toPem(pub)

/*
-----BEGIN PUBLIC KEY-----
MFYwEAYHKoZIzj0CAQYFK4EEAAoDQgAEG4TFVnsSZECZXT7VqroFZdceGDRgSBn/
nBf16dXdB49wvq+PWItUFQf+1qZCxatC39+BIKf2Od5RItR6aajo0Q==
-----END PUBLIC KEY-----
*/
console.log(pem)

/**
 * Get der Buffer.
 */
const der = converter.toDer(pub)

// Buffer(88) [Uint8Array] [ ... ]
console.dir(der)

/**
 * Get KeyObject from @node/crypto
 */
const keyObj = converter.toKeyObject(pub)

// PublicKeyObject { [Symbol(kKeyType)]: 'public' }
console.dir(keyObj)

sign

With createSign from @node/crypto

const { createSign } = require('crypto')
const { PrivConverter, getCurves } = require('@didkovsky/keys-converter')

/**
 * Get list of supported curves
 */
const curves = getCurves()

/**
 * Create converter for specified curve.
 */
const converter = PrivConverter.for(curves.secp256k1)
 
/**
 * Some private key.
 */
const priv = Buffer.alloc(32, 0x1)

/**
 * Get keyObject from @node/crypto
 */
const privObj = converter.toKeyObject(priv)

/**
 * Signing some message.
 */
const message = 'Hello world!'
const signer = createSign('sha256')
signer.update(message).end()
const signature = signer.sign(privObj)

// MEUCIDHplD47Iy3PaSspcMmAScvE8Acdwc+4nrlPJwq64oynAiEA5+kGFrOqaihg7OQekCae5eKNprwweT02cKNQ5MPBrAE=
console.log(signature.toString('base64'))

verify

With createVerify from @node/crypto

const { createVerify } = require('crypto')
const { PubConverter, getCurves } = require('@didkovsky/keys-converter')

/**
 * Get list of supported curves
 */
const curves = getCurves()

/**
 * Create converter for specified curve.
 */
const converter = PubConverter.for(curves.secp256k1)

/**
 * Raw public key in base64 (65 bytes)
 */
const pub = 'BBuExVZ7EmRAmV0+1aq6BWXXHhg0YEgZ/5wX9enV3QePcL6vj1iLVBUH/tamQsWrQt/fgSCn9jneUSLUemmo6NE='

/**
 * Get keyObject from @node/crypto
 */
const pubObj = converter.toKeyObject(Buffer.from(pub, 'base64'))

/**
 * Our test signature and message
 */
const message = 'Hello world!'
const signature = 'MEUCIDHplD47Iy3PaSspcMmAScvE8Acdwc+4nrlPJwq64oynAiEA5+kGFrOqaihg7OQekCae5eKNprwweT02cKNQ5MPBrAE='

/**
 * Verifiyng signature
 */
const verifier = createVerify('sha256')
verifier.update(message).end()
const result = verifier.verify(pubObj, Buffer.from(signature, 'base64'))

// true
console.log(result)

About

Elliptic curve's keys converter for NodeJS

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published