Skip to content

Instantly share code, notes, and snippets.

View deepal's full-sized avatar

Deepal Jayasekara deepal

  • CompareTheMarket.com
  • London, United Kingdom
View GitHub Profile
@deepal
deepal / crypto-ctr.js
Created June 23, 2016 08:53 — forked from chris-rock/crypto-ctr.js
Encrypt and decrypt text in nodejs
// Part of https://github.com/chris-rock/node-crypto-examples
// Nodejs encryption with CTR
var crypto = require('crypto'),
algorithm = 'aes-256-ctr',
password = 'd6F3Efeq';
function encrypt(text){
var cipher = crypto.createCipher(algorithm,password)
var crypted = cipher.update(text,'utf8','hex')
@deepal
deepal / crypto-buffer.js
Created June 23, 2016 08:53 — forked from chris-rock/crypto-buffer.js
Encrypt and decrypt buffers in nodejs
// Part of https://github.com/chris-rock/node-crypto-examples
var crypto = require('crypto'),
algorithm = 'aes-256-ctr',
password = 'd6F3Efeq';
function encrypt(buffer){
var cipher = crypto.createCipher(algorithm,password)
var crypted = Buffer.concat([cipher.update(buffer),cipher.final()]);
return crypted;
@deepal
deepal / crypto-stream.js
Created June 23, 2016 08:53 — forked from chris-rock/crypto-stream.js
Encrypt and decrypt streams
// Part of https://github.com/chris-rock/node-crypto-examples
// Nodejs encryption of buffers
var crypto = require('crypto'),
algorithm = 'aes-256-ctr',
password = 'd6F3Efeq';
var fs = require('fs');
var zlib = require('zlib');
@deepal
deepal / crypto-gcm.js
Created June 23, 2016 08:53 — forked from chris-rock/crypto-gcm.js
Use GCM for authenticated encryption in nodejs
// Nodejs encryption with GCM
// Does not work with nodejs v0.10.31
// Part of https://github.com/chris-rock/node-crypto-examples
var crypto = require('crypto'),
algorithm = 'aes-256-gcm',
password = '3zTvzr3p67VC61jmV54rIYu1545x4TlY',
// do not use a global iv for production,
// generate a new one for each encryption
iv = '60iP0h6vJoEa'
@deepal
deepal / custom-error.js
Created April 24, 2017 06:20 — forked from justmoon/custom-error.js
Creating custom Error classes in Node.js
'use strict';
module.exports = function CustomError(message, extra) {
Error.captureStackTrace(this, this.constructor);
this.name = this.constructor.name;
this.message = message;
this.extra = extra;
};
require('util').inherits(module.exports, Error);
@deepal
deepal / mocha-guide-to-testing.js
Created June 19, 2017 11:03 — forked from samwize/mocha-guide-to-testing.js
Explain Mocha's testing framework - describe(), it() and before()/etc hooks
// # Mocha Guide to Testing
// Objective is to explain describe(), it(), and before()/etc hooks
// 1. `describe()` is merely for grouping, which you can nest as deep
// 2. `it()` is a test case
// 3. `before()`, `beforeEach()`, `after()`, `afterEach()` are hooks to run
// before/after first/each it() or describe().
//
// Which means, `before()` is run before first it()/describe()
@deepal
deepal / v8-compile-javascript-example.cc
Created October 12, 2019 11:27 — forked from ghaiklor/v8-compile-javascript-example.cc
Simple example how V8 can compile JavaScript source and run it
// Create a new context.
Local<Context> context = Context::New(isolate);
// Enter the context for compiling and running the hello world script.
Context::Scope context_scope(context);
// Create a string containing the JavaScript source code.
Local<String> source = String::NewFromUtf8(isolate, "'Hello' + ', World!'");
// Compile the source code.
@deepal
deepal / v8-templates-example.cc
Created October 12, 2019 11:27 — forked from ghaiklor/v8-templates-example.cc
Simple example how V8 Function\Object Templates can be used
// Create a template for the global object and set the built-in global functions
Local<ObjectTemplate> global = ObjectTemplate::New(isolate);
global->Set(
String::NewFromUtf8(isolate, "log"),
FunctionTemplate::New(isolate, LogCallback)
);
// Each processor gets its own context so different processors do not affect each other
Persistent<Context> context = Context::New(isolate, NULL, global);
@deepal
deepal / nodejs-binding-method.cc
Created October 13, 2019 13:28 — forked from ghaiklor/nodejs-binding-method.cc
NodeJS method that returns JavaScript object from bindings
static void Binding(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
Local<String> module = args[0]->ToString(env->isolate());
node::Utf8Value module_v(env->isolate(), module);
Local<Object> cache = env->binding_cache_object();
Local<Object> exports;
if (cache->Has(module)) {