Web Crypto API ECDSA Generate Keys Sign & verify Message

by Anish

Posted on Monday september 24, 2018


This sample chapter extracted from the book, Cryptography for JavaScript Developers.


The Web crypto api ECDSA algorithm identifier is used to perform signing and verification using the ECDSA algorithm specified in RFC6090 and using the SHA hash functions and elliptic curves defined in this specification.

ECDSA Web crypto api Demo

SelectNamed Curve Input Text to Signed
Signature Output (Hex)
ECDSA Public Key (JWK) :
ECDSA Private Key (JWK):

This is the web cryptography api example of performing ecdsa message signing and verifying the signature

ECDSA Javascript example of using webcrypto api

Generate ECDSA Keys using the named curved P-256, P-384, or P-521, The generated ECDSA keys is outputed in JWK format for demo purpose only

  window.crypto.subtle.generateKey({
                name: "ECDSA",
                namedCurve: curve, //can be "P-256", "P-384", or "P-521"
            },
            true, //whether the key is extractable (i.e. can be used in exportKey)
            ["sign", "verify"] //can be any combination of "sign" and "verify"
        )
        .then(function(key) {

        publicKey = key.publicKey;
        privateKey = key.privateKey;
        // For Demo Purpos Only Exported in JWK format
        window.crypto.subtle.exportKey("jwk", key.publicKey).then(
            function(keydata) {
                publicKeyhold = keydata;
                publicKeyJson = JSON.stringify(publicKeyhold);
                document.getElementById("ecdsapublic").value = publicKeyJson;
            }
        );

        window.crypto.subtle.exportKey("jwk", key.privateKey).then(
            function(keydata) {
                privateKeyhold = keydata;
                privateKeyJson = JSON.stringify(privateKeyhold);
                document.getElementById("ecdsaprivate").value = privateKeyJson;
            }
        );

ECDSA Sign the Message and generate signature

        window.crypto.subtle.sign({
                    name: "ECDSA",
                    hash: {
                        name: "SHA-256"
                    }, //can be "SHA-1", "SHA-256", "SHA-384", or "SHA-512"
                },
                privateKey, //from generateKey or importKey above
                asciiToUint8Array(plainText) //ArrayBuffer of data you want to sign
            )
            .then(function(signature) {
                //returns an ArrayBuffer containing the signature
                document.getElementById("cipherText").value = bytesToHexString(signature);
            })
            .catch(function(err) {
                console.error(err);
            });


    })
    .catch(function(err) {
        console.error(err);
    });

ECDSA Verify the message signature

    window.crypto.subtle.verify({
            name: "ECDSA",
            hash: {name: "SHA-256"}, //can be "SHA-1", "SHA-256", "SHA-384", or "SHA-512"
        },
        publicKey, //from generateKey or importKey above
        hexStringToUint8Array(cipherText), //ArrayBuffer of the data
        asciiToUint8Array(plainText)
    )
    .then(function(decrypted) {
        alert("Verified   " + decrypted);
    })
    .catch(function(err) {
        console.error(err);
    });

Download the sample code here
Next Reading RSA Generate Keys and perform Encryption Decryption

Thanku for reading !!! Give a Share for Support

Asking for donation sound bad to me, so i'm raising fund from The Modern Cryptography CookBook for Just $9. Leanpub books Discount coupon first 100 reader. No hurry read the sample chapters here then decide.

Alternatively to support you can buy My all four Cryptography book Just $10.99


Referefce
  • The Modern Cryptography Book.
  • Go lang Cryptography for developers
  • Python Cryptography
  • Cryptography for JavaScript Developer


python Cryptography Topics
Topics
For Coffee/ Beer/ Amazon Bill and further development of the project Support by Purchasing, The Modern Cryptography CookBook for Just $9 Coupon Price

Kubernetes for DevOps

Cryptography for Python Developers

Cryptography for JavaScript Developers

Go lang ryptography for Developers

Here