Useful Node.js snippets

Here are some useful node.js snippets I’ve been messing with in configs that use node.js

I will periodically update this list as I gather and use more features.

OAuth Generation

Output Variable = noderesult : Type String

const crypto = require("crypto");

function base64URLEncode(str) {
return str.toString('base64')
.replace(/\+/g, '-')
.replace(/\//g, '_')
.replace(/=/g, '');
}

function sha256(buffer) {
return crypto.createHash('sha256').update(buffer).digest();
}

var VERIFIER = base64URLEncode(crypto.randomBytes(32));
var CHALLENGE = base64URLEncode(sha256(VERIFIER));
var noderesult = {'VERIFIER': VERIFIER,'CHALLENGE': CHALLENGE};

Output Variable: noderesult


Encrypt text using PKCS#7 get output as bytes

Input Variables: certificate,input

var forge = require('node-forge');

// create cert object
var cert = forge.pki.certificateFromPem(certificate);
// create envelop data
var p7 = forge.pkcs7.createEnvelopedData();
// add certificate as recipient
p7.addRecipient(cert);
// set content 
p7.content = forge.util.createBuffer();
p7.content.putString(input);

// encrypt
p7.encrypt();

// obtain encrypted data with DER format
var bytes = forge.asn1.toDer(p7.toAsn1()).getBytes();

Output Variable: bytes


Encrypt text using PKCS#7 get output as string

Input Variables: certificate,input

var forge = require('node-forge');

// create cert object
var cert = forge.pki.certificateFromPem(certificate);
// create envelop data
var p7 = forge.pkcs7.createEnvelopedData();
// add certificate as recipient
p7.addRecipient(cert);
// set content 
p7.content = forge.util.createBuffer();
p7.content.putString('content to be encrypted');

// encrypt
p7.encrypt();

// obtain encrypted data with DER format
var bytes = forge.asn1.toDer(p7.toAsn1()).getBytes();
var resultant = Buffer.from(bytes, 'binary').toString('utf8');

Output Variables: resultant


Encrypt text using AES/CBC/PKCS#5 PADDING algorithm and Base64 Encoded in Node.js

Input Variables: input,keyy

const crypto = require('crypto');

const initialization_vector_length = 16;

function encrypt(text_to_encrypt, key) {

    var keyBuffer = Buffer.from(key);

    var plainTextBuffer = Buffer.from(text_to_encrypt, 'utf8');

    var cipher = crypto.createCipheriv(algorithm(keyBuffer), keyBuffer, new Buffer(initialization_vector_length))

    var encrypted = cipher.update(plainTextBuffer)

    return Buffer.concat([encrypted, cipher.final()]);

}

function algorithm(key) {

    var algo = ""

    switch (key.length) {

        case 16:

            algo = "128"

            break;

        case 24:

            algo = "192"

            break;

        case 32:

            algo = "256"

            break;

    }

    return `aes-${algo}-cbc`

}

var resultant = encrypt(input , keyy)

Output Variable: resultant

9 Likes

Kiss me hard baby @Pure XD

1 Like

this is a snippet to AES encrypt with CBC mode, PKCS7 padding, and 128 key-size. the Output is Base64 and URL encoded.

BLOCK:Script

INTERPRETER:NodeJS

INPUT

BEGIN SCRIPT

const crypto = require('crypto');

var key = "randomkeyasanexample123456789";

var iv = "randomivasanexample123456789";

const phrase = "hello world!";

var encrypt = ((val) => {

let cipher = crypto.createCipheriv('aes-128-cbc', key, iv);

let encrypted = cipher.update(val, 'utf8', 'base64');

encrypted += cipher.final('base64');

return encrypted;

});

var AESPASS = encodeURIComponent(encrypt(phrase));

console.log(AESPASS);

END SCRIPT

OUTPUT String @AESPASS

ENDBLOCK
3 Likes