
- Node.js - Home
- Node.js - Introduction
- Node.js - Environment Setup
- Node.js - First Application
- Node.js - REPL Terminal
- Node.js - Command Line Options
- Node.js - Package Manager (NPM)
- Node.js - Callbacks Concept
- Node.js - Upload Files
- Node.js - Send an Email
- Node.js - Events
- Node.js - Event Loop
- Node.js - Event Emitter
- Node.js - Debugger
- Node.js - Global Objects
- Node.js - Console
- Node.js - Process
- Node.js - Scaling Application
- Node.js - Packaging
- Node.js - Express Framework
- Node.js - RESTFul API
- Node.js - Buffers
- Node.js - Streams
- Node.js - File System
- Node.js MySQL
- Node.js - MySQL Get Started
- Node.js - MySQL Create Database
- Node.js - MySQL Create Table
- Node.js - MySQL Insert Into
- Node.js - MySQL Select From
- Node.js - MySQL Where
- Node.js - MySQL Order By
- Node.js - MySQL Delete
- Node.js - MySQL Update
- Node.js - MySQL Join
- Node.js MongoDB
- Node.js - MongoDB Get Started
- Node.js - MongoDB Create Database
- Node.js - MongoDB Create Collection
- Node.js - MongoDB Insert
- Node.js - MongoDB Find
- Node.js - MongoDB Query
- Node.js - MongoDB Sort
- Node.js - MongoDB Delete
- Node.js - MongoDB Update
- Node.js - MongoDB Limit
- Node.js - MongoDB Join
- Node.js Modules
- Node.js - Modules
- Node.js - Built-in Modules
- Node.js - Utility Modules
- Node.js - Web Module
NodeJS - crypto.subtle Property
The NodeJs Crypto.subtle is an instance and read-only property of the crypto interface. It is used to retrieve the subtle crypto which can be used to perform low-level cryptographic operations.
The SubtleCrypto is an interface in the NodeJs that provides several low-level cryptographic functions.
Cryptography operations are basic procedures used to protect information by transforming it into an unrecognizable form to somebody who cannot access it.
These operations include data encryption, data integrity, identity verification, information authentication, and non-repudiation.
Syntax
Following is the syntax of the NodeJs Crypto.subtle property −
Crypto.subtle
Parameters
- As it is property does not accept any parameter.
Return value
This property returns the SubtleCrypto interface.
Example 1
The following is the basic example of the NodeJs Crypto.subtle property.
const crypto = require('crypto').webcrypto; const message = 'Hello, world!'; async function createHash(message) { const encoder = new TextEncoder(); const data = encoder.encode(message); const hashBuffer = await crypto.subtle.digest('SHA-256', data); const hashArray = Array.from(new Uint8Array(hashBuffer)); const hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join(''); return hashHex; } createHash(message).then(hash => console.log('SHA-256 hash of ', message, 'is: ', hash));
Output
The above program produces the following output −
SHA-256 hash of Hello, world! is: 315f5bdb76d078c43b8ac0064e4a0164612b1fce77c869345bfc94c75894edd3
Example 2
The following is another example of the NodeJs Crypto.subtle property. We use this property to retrieve the CryptoSubtle to perform low-level cryptographic operations such as data encryption and decryption.
const { webcrypto } = require('crypto'); async function generateKey() { return webcrypto.subtle.generateKey({ name: 'AES-GCM', length: 256 }, true, ['encrypt', 'decrypt']); } async function encryptData(data, key) { const iv = webcrypto.getRandomValues(new Uint8Array(12)); const encryptedData = await webcrypto.subtle.encrypt({ name: 'AES-GCM', iv }, key, new TextEncoder().encode(data)); return { iv, encryptedData: new Uint8Array(encryptedData) }; } async function decryptData(encryptedData, iv, key) { const decryptedDataBuffer = await webcrypto.subtle.decrypt({ name: 'AES-GCM', iv }, key, encryptedData); return new TextDecoder().decode(decryptedDataBuffer); } (async () => { const key = await generateKey(); const data = 'Tutorialspoint!'; const { iv, encryptedData } = await encryptData(data, key); console.log('Encrypted Data:', encryptedData); const decryptedData = await decryptData(encryptedData, iv, key); console.log('Decrypted Data:', decryptedData); })();
Output
After executing the above program it will display the following output −
Encrypted Data: Uint8Array(31) [ 223, 193, 14, 42, 142, 251, 166, 92, 192, 167, 135, 65, 143, 66, 41, 20, 51, 227, 13, 10, 151, 81, 221, 140, 41, 183, 157, 1, 181, 252, 251 ] Decrypted Data: Tutorialspoint!