Last active
December 28, 2015 19:09
-
-
Save plainas/7548366 to your computer and use it in GitHub Desktop.
node.js crypto stuff, signing and verifying
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var bufferXOR = function(a,b){ | |
c = Buffer(20); | |
for(i = 0; i<20;i++){ | |
c[i] = a[i] ^ b[i]; | |
} | |
return c; | |
} | |
var getKBucketId = function(buf){ | |
for (i = 0; i < 20; i++){ | |
if(buf[i]>0){ | |
return 8*i+getPosFirsOneInByte(buf[i]) | |
} | |
} | |
return 0; | |
} | |
function getPosFirsOneInByte(byte){ | |
var mask = 128; //10000000 | |
for (i = 0; i < 8; i++){ | |
if ((mask & byte) != 0){ | |
return i; | |
} | |
mask = mask >> 1; | |
} | |
return 0 | |
} | |
exports.getKBucketId = getKBucketId; | |
exports.bufferXOR = bufferXOR; | |
exports.getPosFirsOneInByte = getPosFirsOneInByte; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var dgram = require("dgram"); | |
var parseRequest = function(message){ | |
if (message.length != 22){ | |
console.log('message is not the right side' + message.length); | |
} | |
else{ | |
console.log(message); | |
} | |
} | |
var server = dgram.createSocket("udp4",parseRequest); | |
server.bind(41234); | |
// server listening 0.0.0.0:41234 | |
var a = new Buffer( [0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,255,0,0,0,0]); | |
var b = new Buffer( [0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,255,0,0,0,0]); | |
function bufferXOR(a,b){ | |
c = Buffer(20); | |
for(i = 0; i<20;i++){ | |
c[i] = a[i] ^ b[i]; | |
} | |
return c; | |
} | |
function getKBucketId(buf){ | |
for (i = 0; i < 20; i++){ | |
if(buf[i]>0){ | |
return 8*i+buf[i] | |
} | |
} | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var dgram = require("dgram"); | |
//~ a = new Buffer( [0,0,200,0,0,0,0,0,0,0,0,1,0,0,0,8,0,0,0,0]); | |
//~ b = new Buffer( [0,5,0,0,0,0,0,0,0,0,0,1,0,0,0,8,0,0,0,0]); | |
//~ c = new Buffer( [255,5,0,0,0,0,0,0,0,0,0,1,0,0,0,255,0,0,0,0]); | |
d = new Buffer([127,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1]); | |
var client = dgram.createSocket("udp4"); | |
//~ client.send(a, 0, a.length, 41234, "localhost"); | |
//~ client.send(b, 0, b.length, 41234, "localhost"); | |
//~ client.send(c, 0, c.length, 41234, "localhost"); | |
client.send(d, 0, d.length, 41234, "localhost"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//falta ver como criar os pems usando oi node.js | |
EDIT: instalar este módulo para gerar as chaves! BAM problem solved!!! | |
// https://github.com/juliangruber/keypair | |
var crypto = require('crypto'); | |
var fs = require('fs'); | |
var pem = fs.readFileSync('private_key.pem'); | |
var key = pem.toString('ascii'); | |
var sign = crypto.createSign('RSA-SHA256'); | |
sign.update('aaaaaaaaaaa'); // data from your file would go here | |
//We created a signature | |
var sig = sign.sign(key, 'hex'); | |
console.log("printing the signature!!!") | |
console.log(sig); | |
var public_key_pem = fs.readFileSync('public_key.pem') | |
var verif = crypto.createVerify('RSA-SHA256'); | |
verif.update('aaaaaaaaaaa') | |
console.log(verif.verify(public_key_pem, sig, 'hex')); | |
//BAM!!! verificamos que a string 'aaaaaaaaaaa' foi assinada pela | |
//+esspa que tem a provate key correspondente a public key nosrecebemos |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var dgram = require("dgram"), | |
bufutils = require("./bufferutils.js"); | |
var parseRequest = function(message){ | |
if (message.length != 20){ | |
console.log('message is not the right side' + message.length); | |
} | |
else{ | |
console.log("this is on bucket: " + bufutils.getKBucketId(message)); | |
} | |
} | |
var server = dgram.createSocket("udp4",parseRequest); | |
server.bind(41234); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment