Skip to content

Instantly share code, notes, and snippets.

View GuillermoPena's full-sized avatar

Guillermo Peña GuillermoPena

  • Green Box Software
  • Madrid, Spain
View GitHub Profile
@GuillermoPena
GuillermoPena / nodeJs.json.objectAndFiles.js
Created May 28, 2014 09:55
NodeJS - JSON : Objects and files
//Read json object from json file.
//Write json object to json file
var fs = require('fs')
var util = require('util')
// __dirname is folder where script is running
// NOTE: Use always " in json file content
var cfgFile = __dirname + "/anything.json"
@GuillermoPena
GuillermoPena / nodeJs.scheduledLoop.js
Created May 28, 2014 09:59
NodeJS - Scheduled Loop
// How wait inside of loop...
// Countdown
var counter = 10
var countdown = function() {
console.log(counter)
counter--
return (counter > 0)
}
@GuillermoPena
GuillermoPena / nodeJs.crypto.symmetricCryptographyOfStrings.js
Created May 28, 2014 10:08
NodeJS - CRYPTO : Symetric Cryptography of Strings
var crypto = require('crypto')
// Encrypt/Decrypt a string
var key = "SecretKey"
, message = "This is the text to cipher!"
// Encrypting
var cipher = crypto.createCipher('des-ede3-cbc', key)
, cryptedMessage = cipher.update(message, 'utf8', 'hex')
cryptedMessage+= cipher.final('hex')
@GuillermoPena
GuillermoPena / nodeJs.dynamicNamedVariables.js
Created May 28, 2014 10:10
NodeJS - Variables with dynamic name
var varName1 = "varName2"
global[varName1] = "Hello World" // This is the important line ;)
var staticVarName = global[varName1]
console.log("Var1: Name/Value : varName1/" + varName1)
console.log("Var2: Name/Value : " + varName1 + "/" + global[varName1])
console.log("Static Var: Name/Value : staticVarName/" + staticVarName)
@GuillermoPena
GuillermoPena / nodeJs.crypto.simmetricCryptographyOfFiles.js
Created May 28, 2014 10:12
NodeJS - CRYPTO : Symmetric Cryptography of Files
var crypto = require('crypto')
, fs = require('fs')
var sourceFile = __dirname + "/testFile.txt"
, targetFile = __dirname + "/testEncryptedFile.txt"
, key = "SecretKey"
, algorithm = 'aes192' // Another algorithms: 'des-ede3-cbc'
, cipher = crypto.createCipher(algorithm, key)
, decipher = crypto.createDecipher(algorithm, key)
, rs = fs.ReadStream(sourceFile)
@GuillermoPena
GuillermoPena / nodeJs.ursa.asymmetricCriptography.js
Created May 28, 2014 10:13
NodeJS - URSA : Asymmetric Cryptology
// How to encrypt/decrypt with ursa module (asymmetric cryptology)
var ursa = require('ursa')
, message = 'Hello world!'
, encoding = 'base64'
// Creating a pair of keys (a private key contains both keys...)
console.log('Generating Keys Pair...')
var keys = ursa.generatePrivateKey()
// Making a private key
@GuillermoPena
GuillermoPena / nodeJs.mocha.unitTesting.js
Created May 28, 2014 10:14
NodeJS - MOCHA : Unit Testing
// All you need to know about Mocha testing in 60 lines
// Mocha Testing: http://visionmedia.github.io/mocha/
// Installation: npm install -g mocha
// Run: mocha [test folder or test file]
// Example: mocha . (Run all js tests in folder)
//
// Usefull parameters:
// mocha -w . : Watch file changes and re-run tests
// mocha -d . : Debug mode
@GuillermoPena
GuillermoPena / nodeJs.ursa.asymmetricCryptologyWithSignature.js
Created May 28, 2014 10:16
NodeJS - URSA : Asymmetric Cryptology With Signature
// How to send message from A user to B user, encrypting it (asymetric cryptology) and sign it
var ursa = require('ursa')
, message = 'Hello world!'
, encoding = 'base64'
, buffer = new Buffer(message, 'ascii')
// Creating a pair of keys of user A (a private key contains both keys...)
console.log('Generating Keys Pair of user A...')
var keysA = ursa.generatePrivateKey()
@GuillermoPena
GuillermoPena / nodeJs.socketio.managingWebSockets.js
Created May 28, 2014 10:17
NodeJS - SOCKET.IO : Managing websockets
// Managing websocket with socket.io
// Setting a web socket and sending a message from client to server.
// Server side
var app = require('express')()
, util = require('util')
, server = require('http').createServer(app)
, io = require('socket.io').listen(server)
, port = 8013
@GuillermoPena
GuillermoPena / nodeJs.express.simpleWebService.js
Created May 28, 2014 10:19
NodeJS - EXPRESS : Simple Web Service
// How to create a simple web service with Express
// In App.js
// Dependencies
var express = require('express')
, http = require('http')
, port = 8080
// Application
var app = express()