Skip to content
background-image background-image

JWT and crypto examples

This example shows how to use

jsonwebtoken

jsonwebtoken module is imported as jwt

// 1. sign
const token = jwt.sign({ foo: 'bar' }, 'secret');

// 2. verify
const decoded = jwt.verify(token, 'secret');

node:crypto

node:crypto module is imported as crypto

// 1. compute SHA256 hash of string
const hash = crypto.createHash('sha256');
hash.update('data to hash');
const result = hash.digest('hex');

// 2. compute HMAC-SHA256 hash of string
const hash = crypto.createHmac('sha256', 'secret');
hash.update('data to hash');
const result = hash.digest('hex');