Open In App

Node.js keyObject.symmetricKeySize Property

The keyObject.symmetricKeySize property is an inbuilt application programming interface of class keyObject within the crypto module. It is used to find the key size of symmetric secret keys. The crypto module is present in Node.js and wrappers for OpenSSL’s hash, HMAC, cypher, decode, sign, and verify methods.

Syntax:



keyObject.symmetricKeySize

Returns: It returns the size of secret keys. It returns undefined for asymmetric keys.

Example:






const {
    webcrypto: { subtle },
    KeyObject,
} = await import("crypto");
  
(async function () {
    const key = await subtle.generateKey(
        {
            name: "HMAC",
            hash: "SHA-256",
            length: 256,
        },
        true,
        ["sign", "verify"]
    );
  
    try {
        const keyObject = KeyObject.from(key);
        console.log(
            keyObject.symmetricKeySize
        );
    } catch (error) {
        console.log("Error has been occurred");
    }
})();

Output:

32

Example:




const {
 KeyObject,
 webcrypto: { subtle, getRandomValues }
} = await import('node:crypto');
  
(async function(){
    const keyData=getRandomValues(new Uint8Array(16));
    const key=await subtle.importKey("raw",keyData,"AES-GCM",false,["encrypt"])
    const keyObject = KeyObject.from(key);
        console.log(
            keyObject.symmetricKeySize
        );
})();

Output:

16

Reference: https://nodejs.org/api/crypto.html#keyobjectsymmetrickeysize


Article Tags :