Node.js - Crypto


In Node.js, crypto is a in-built module that provides cryptographic functionalities that are essential for incorporating security aspects into a project, for instance, encryption, decryption, hash functions, and more. This module rely on OpenSSL for its cryptographic operations.

Cryptography is the process of putting data into a form that can only be understood by users who meet certain predetermined conditions.

Cryptography is not only about encoding and decoding the messages, It is about making sure that information remains private, integral, and genuine in the growing globalized context.

Crypto Class

The Crypto Class represents a basic cryptography features available in the current context. It is the built-in library of Node.js includes the Crypto module.

Property

Following is the property of crypto class −

Sr.No. Property & Description
1

crypto.subtle

This property returns the SubtleCrypto interface.

Methods

Following are the methods of Crypto class −

Sr.No. Method & Description
1

crypto.getRandomValues(typedArray)

The same array is passed as typedarray, but its contents are replaced with new random numbers in that place.

2

crypto.randomUUID()

This method returns a string containing a randomly generated.

CryptoKey Class

A crypto-key class is used to represent a blueprint for creating and managing cryptographic keys.

Properties

Following is a list of properties available with the CryptoKey class −

Sr.No. Properties & Description
1

cryptoKey.algorithm

The object returned depends of the algorithm used to generate the key.

2

cryptoKey.extractable

A boolean value that is true if the key can be exported and false if not.

3

cryptoKey.type

It returns the array of strings which is in secret, private, or public.

4

cryptoKey.usages

It returns the array of strings from the list.

CryptoKeyPair Class

The CryptoKeyPair class provides a structured way to manage and utilize public and private key pairs.

Properties

Following is a list of properties of the CryptoKeyPair class −

Sr.No. Properties & Description
1

cryptoKeyPair.privateKey

This property provides access to the private key of the key pair.

2

cryptoKeyPair.publicKey

This property provides access to the public key of the key pair.

SubtleCrypto Class

The SubtleCrypto class is a part of the Web Cryptography API, designed to perform low-level cryptographic operations in web applications. It provides methods for generating keys.

Methods

Following are the list of methods to the SubtleCrypto class −

Sr.No. Methods & Description
1

subtle.decrypt(algorithm, key, data)

It returns a Promise that resolves with the decrypted data as an ArrayBuffer.

2

subtle.deriveBits(algorithm, baseKey, length)

It returns a Promise that resolves with the derived bits as an ArrayBuffer.

3

subtle.deriveKey(algorithm, baseKey, derivedKeyAlgorithm, extractable, keyUsages)

It returns a Promise that resolves with a CryptoKey object.

4

subtle.digest(algorithm, data)

It returns a Promise that resolves with the digest of the given data as an ArrayBuffer.

5

subtle.encrypt(algorithm, key, data)

It returns a Promise that resolves with the encrypted data as an ArrayBuffer.

6

subtle.exportKey(format, key)

It returns a Promise that resolves with key data in the specified format.

7

subtle.generateKey(algorithm, extractable, keyUsages)

It returns a Promise that resolves with a newly generated CryptoKey object representing the generated key.

8

subtle.importKey(format, keyData, algorithm, extractable, keyUsages)

It returns a Promise that resolves with a CryptoKey object representing the imported key.

9

subtle.sign(algorithm, key, data)

It returns a Promise that resolves with the signature of the given data as an ArrayBuffer.

10

subtle.unwrapKey(format, wrappedKey, unwrappingKey, unwrapAlgo, unwrappedKeyAlgo, extractable, keyUsages)

It returns a Promise that resolves with a CryptoKey object representing the unwrapped key.

11

subtle.verify(algorithm, key, signature, data)

It returns a Promise that resolves with a boolean value indicating whether the signature provided matches the expected signature for the given data.

12

subtle.wrapKey(format, key, wrappingKey, wrapAlgo)

It returns a Promise that resolves with an ArrayBuffer or TypedArray containing the wrapped key data.

nodejs_built_in_modules.htm
Advertisements