Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

215
Vistas
How to use Web Crypto API to import privateKey using ECDSA

I have a ECDSA privateKey/publicKey and want to convert it into JWK using node.js and I am using WebCrypto API.

class KeyPair {
    constructor(privateKey, publicKey) {
        const algorithm = {
            name: "ECDSA",
            namedCurve: "P-256",
        };

        subtle
            .importKey(
                "jwk",
                this.getPrivateKeyJWK(privateKey, publicKey),
                algorithm,
                false,
                ["sign"]
            )
            .then((res) => (this._privateKey = res));

        subtle
            .importKey("raw", publicKey, algorithm, false, ["verify"])
            .then((res) => (this._publicKey = res));
    }

    getPrivateKeyJWK(privateKey, publicKey) {
        let jwk = {
            // this is an example jwk key, other key types are Uint8Array objects
            kty: "EC",
            crv: "P-256",
            d: Buffer.from(privateKey)
                .toString("base64")
                .replace("+", "-")
                .replace("/", "_"),
            x: Buffer.from(publicKey.slice(1, 33))
                .toString("base64")
                .replace("+", "-")
                .replace("/", "_"),
            y: Buffer.from(publicKey.slice(33, 66))
                .toString("base64")
                .replace("+", "-")
                .replace("/", "_"),
            ext: true,
        };
        return jwk;
    }
}

However when subtle.importKey is called, I receive the following error:

node:internal/per_context/domexception:70
    ErrorCaptureStackTrace(this);
    ^
DOMException [NotSupportedError]: Unrecognized name.
    at new DOMException (node:internal/per_context/domexception:70:5)
    at __node_internal_ (node:internal/util:477:10)
    at normalizeAlgorithm (node:internal/crypto/util:235:9)
    at SubtleCrypto.importKey (node:internal/crypto/webcrypto:471:15)
    at new EcdsaWallet (./EcdsaWallet.js:<line of first subtle.importKey call>:14) 
    at Object.<anonymous> (./EcdsaWallet.js:<line of object creation>:14)
    at Module._compile (node:internal/modules/cjs/loader:1097:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1151:10)
    at Module.load (node:internal/modules/cjs/loader:975:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)

The key which I want to import is a keypair of public and private key which are given as strings.

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda