Got encrypted, stable communication between the server and the client
This commit is contained in:
parent
2e19f02dc6
commit
62390df244
|
@ -7,3 +7,5 @@ An open source chat server for the chat client
|
||||||
# Required libraries
|
# Required libraries
|
||||||
- net
|
- net
|
||||||
- fs
|
- fs
|
||||||
|
- random-bytes
|
||||||
|
- node-rsa
|
||||||
|
|
188
main.js
188
main.js
|
@ -1,19 +1,197 @@
|
||||||
#!/usr/bin/node
|
#!/usr/bin/node
|
||||||
|
|
||||||
// Get some libraries
|
// Get some libraries
|
||||||
|
const random_bytes = require("random-bytes");
|
||||||
|
const bsplit = require("buffer-split");
|
||||||
|
const node_rsa = require("node-rsa");
|
||||||
const net = require("net");
|
const net = require("net");
|
||||||
const fs = require("fs");
|
const fs = require("fs");
|
||||||
|
|
||||||
|
// Load the settings
|
||||||
|
var settings = require("./settings.json");
|
||||||
|
|
||||||
|
console.log("Ready.");
|
||||||
|
|
||||||
|
function string_decrypt(key, string)
|
||||||
|
{
|
||||||
|
// Convert the string to a buffer
|
||||||
|
var buff = new Buffer.from(string);
|
||||||
|
|
||||||
|
// Create some new data
|
||||||
|
var decrypted = new Buffer.alloc(buff.length);
|
||||||
|
|
||||||
|
// Iterate over the string
|
||||||
|
for(var i=0;i<string.length;i++)
|
||||||
|
{
|
||||||
|
// Convert the string item to a number
|
||||||
|
var d = buff[i]-key.str[key.at.rx];
|
||||||
|
while(d < 0) d += 256;
|
||||||
|
decrypted[i] = d;
|
||||||
|
|
||||||
|
// Add 1 to the key counter
|
||||||
|
key.at.rx += 1;
|
||||||
|
|
||||||
|
// Is the key counter out of range
|
||||||
|
if(key.at.rx >= key.str.length)
|
||||||
|
{
|
||||||
|
// Set to zero
|
||||||
|
key.at.rx = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return the encrypted data
|
||||||
|
return decrypted;
|
||||||
|
}
|
||||||
|
|
||||||
|
function string_encrypt(key, string)
|
||||||
|
{
|
||||||
|
// Convert the string to a buffer
|
||||||
|
var buff = new Buffer.from(string);
|
||||||
|
|
||||||
|
// Create some new data
|
||||||
|
var encrypted = new Buffer.alloc(buff.length);
|
||||||
|
|
||||||
|
// Iterate over the string
|
||||||
|
for(var i=0;i<string.length;i++)
|
||||||
|
{
|
||||||
|
// Convert the string item to a number
|
||||||
|
var e = buff[i]+key.str[key.at.tx];
|
||||||
|
while(e > 255) e -= 256;
|
||||||
|
encrypted[i] = e;
|
||||||
|
|
||||||
|
// Add 1 to the key counter
|
||||||
|
key.at.tx += 1;
|
||||||
|
|
||||||
|
// Is the key counter out of range
|
||||||
|
if(key.at.tx >= key.str.length)
|
||||||
|
{
|
||||||
|
// Set to zero
|
||||||
|
key.at.tx = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return the encrypted data
|
||||||
|
return encrypted;
|
||||||
|
}
|
||||||
|
|
||||||
|
function make_encryption_key(string)
|
||||||
|
{
|
||||||
|
// Make a new key
|
||||||
|
var key = new Object();
|
||||||
|
|
||||||
|
// Set the varibles
|
||||||
|
key.str = string;
|
||||||
|
key.at = new Object();
|
||||||
|
key.at.rx = 0;
|
||||||
|
key.at.tx = 0;
|
||||||
|
|
||||||
|
// Return the key
|
||||||
|
return key;
|
||||||
|
}
|
||||||
|
|
||||||
|
function socket_write(socket, data)
|
||||||
|
{
|
||||||
|
// Send the data encrypted with JSON
|
||||||
|
socket.sock.write(string_encrypt(socket.key, JSON.stringify(data)+"\n"));
|
||||||
|
}
|
||||||
|
|
||||||
|
function socket_ondata(socket, callback)
|
||||||
|
{
|
||||||
|
// Wait for a response
|
||||||
|
socket.on('data', function(data)
|
||||||
|
{
|
||||||
|
// Decode the data
|
||||||
|
data = JSON.parse(data);
|
||||||
|
|
||||||
|
// Call the callback
|
||||||
|
callback(data);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function socket_init(socket, callback, ondata)
|
||||||
|
{
|
||||||
|
// Set the socket
|
||||||
|
var sock = new Object();
|
||||||
|
sock.sock = socket;
|
||||||
|
sock.new = true;
|
||||||
|
|
||||||
|
// Wait for data
|
||||||
|
sock.sock.on('data', function(data)
|
||||||
|
{
|
||||||
|
// Is the socket new
|
||||||
|
if(sock.new)
|
||||||
|
{
|
||||||
|
// Set sock new
|
||||||
|
sock.new = false;
|
||||||
|
|
||||||
|
// Load the key
|
||||||
|
var key = new node_rsa();
|
||||||
|
key.importKey(data.toString(), 'public');
|
||||||
|
console.log("Loaded the RSA key");
|
||||||
|
|
||||||
|
// Get some random bytes
|
||||||
|
random_bytes(settings.encryption_key_size, function(error,string)
|
||||||
|
{
|
||||||
|
// Throw an error if there is one
|
||||||
|
if(error) throw error;
|
||||||
|
|
||||||
|
// Make the key
|
||||||
|
sock.key = make_encryption_key(string);
|
||||||
|
console.log("Created an encryption key")
|
||||||
|
|
||||||
|
// Encrypt the key with RSA
|
||||||
|
var key_encrypted = key.encrypt(sock.key.str, 'base64');
|
||||||
|
console.log("Encrypted the key");
|
||||||
|
|
||||||
|
// Send the size of the key to the client
|
||||||
|
sock.sock.write(key_encrypted.length.toString());
|
||||||
|
console.log("Sent the size of the key:", key_encrypted.length)
|
||||||
|
|
||||||
|
// Send the key to the client
|
||||||
|
sock.sock.write(key_encrypted.toString());
|
||||||
|
console.log("Sent the key to the client");
|
||||||
|
|
||||||
|
// Call the callback
|
||||||
|
setTimeout(callback, 10, sock);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Decrypt the data
|
||||||
|
data = string_decrypt(sock.key, data);
|
||||||
|
|
||||||
|
// Split the data by newlines
|
||||||
|
data = bsplit(data, Buffer("\n"));
|
||||||
|
|
||||||
|
// Iterate over them
|
||||||
|
for(var i=0;i<data.length-1;i++)
|
||||||
|
{
|
||||||
|
// Send the parsed data to the callback
|
||||||
|
ondata(JSON.parse(data[i]));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Return the socket
|
||||||
|
return sock;
|
||||||
|
}
|
||||||
|
|
||||||
// Create a server
|
// Create a server
|
||||||
var server = net.createServer(function(socket)
|
var server = net.createServer(function(socket)
|
||||||
{
|
{
|
||||||
// Send back a connected message
|
var sock;
|
||||||
socket.write(JSON.stringify({connected: true}));
|
|
||||||
console.log("connected");
|
|
||||||
|
|
||||||
socket.on('data', function(data)
|
// Initialise the socket and wait for data
|
||||||
|
socket_init(socket, function(socket)
|
||||||
{
|
{
|
||||||
console.log("recieved: "+data);
|
socket_write(socket, {message: "From the server! :D", array: [1,2,3,4,6,5,"xD",12]});
|
||||||
|
sock = socket;
|
||||||
|
},
|
||||||
|
function(data)
|
||||||
|
{
|
||||||
|
console.log(data);
|
||||||
|
socket_write(sock, {echo: data});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,6 @@
|
||||||
|
language: node_js
|
||||||
|
node_js:
|
||||||
|
- 0.6
|
||||||
|
- 0.8
|
||||||
|
- "0.10"
|
||||||
|
- "0.11"
|
|
@ -0,0 +1,20 @@
|
||||||
|
The MIT License (MIT)
|
||||||
|
|
||||||
|
Copyright (c) 2013 Ryan Day
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||||
|
this software and associated documentation files (the "Software"), to deal in
|
||||||
|
the Software without restriction, including without limitation the rights to
|
||||||
|
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||||
|
the Software, and to permit persons to whom the Software is furnished to do so,
|
||||||
|
subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||||
|
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||||
|
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||||
|
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||||
|
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@ -0,0 +1,29 @@
|
||||||
|
[![Build Status](https://secure.travis-ci.org/soldair/node-buffer-indexof.png)](http://travis-ci.org/soldair/node-buffer-indexof)
|
||||||
|
|
||||||
|
|
||||||
|
buffer-indexof
|
||||||
|
===================
|
||||||
|
|
||||||
|
find the index of a buffer in a buffer. should behave like String.indexOf etc.
|
||||||
|
|
||||||
|
```js
|
||||||
|
|
||||||
|
var bindexOf = require('buffer-indexof');
|
||||||
|
|
||||||
|
var newLineBuffer = new Buffer("\n");
|
||||||
|
|
||||||
|
var b = new Buffer("hi\nho\nsilver");
|
||||||
|
|
||||||
|
|
||||||
|
bindexOf(b,newLineBuffer) === 2
|
||||||
|
|
||||||
|
// you can also start from index
|
||||||
|
|
||||||
|
bindexOf(b,newLineBuffer,3) === 5
|
||||||
|
|
||||||
|
// no match === -1
|
||||||
|
|
||||||
|
bindexOf(b,newLineBuffer,6) === -1
|
||||||
|
|
||||||
|
|
||||||
|
```
|
|
@ -0,0 +1,19 @@
|
||||||
|
module.exports = function bufferIndexOf(buf,search,offset){
|
||||||
|
offset = offset||0
|
||||||
|
|
||||||
|
var m = 0;
|
||||||
|
var s = -1;
|
||||||
|
for(var i=offset;i<buf.length;++i){
|
||||||
|
if(buf[i] == search[m]) {
|
||||||
|
if(s == -1) s = i;
|
||||||
|
++m;
|
||||||
|
if(m == search.length) break;
|
||||||
|
} else {
|
||||||
|
s = -1;
|
||||||
|
m = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (s > -1 && buf.length - s < search.length) return -1;
|
||||||
|
return s;
|
||||||
|
}
|
|
@ -0,0 +1,70 @@
|
||||||
|
{
|
||||||
|
"_args": [
|
||||||
|
[
|
||||||
|
"buffer-indexof@~0.0.0",
|
||||||
|
"/mnt/c/Users/Josua/Desktop/data/nodejs/electron/chat-project/chat-server/node_modules/buffer-split"
|
||||||
|
]
|
||||||
|
],
|
||||||
|
"_from": "buffer-indexof@>=0.0.0 <0.1.0",
|
||||||
|
"_id": "buffer-indexof@0.0.2",
|
||||||
|
"_inCache": true,
|
||||||
|
"_installable": true,
|
||||||
|
"_location": "/buffer-indexof",
|
||||||
|
"_npmUser": {
|
||||||
|
"email": "soldair@gmail.com",
|
||||||
|
"name": "soldair"
|
||||||
|
},
|
||||||
|
"_npmVersion": "1.4.20",
|
||||||
|
"_phantomChildren": {},
|
||||||
|
"_requested": {
|
||||||
|
"name": "buffer-indexof",
|
||||||
|
"raw": "buffer-indexof@~0.0.0",
|
||||||
|
"rawSpec": "~0.0.0",
|
||||||
|
"scope": null,
|
||||||
|
"spec": ">=0.0.0 <0.1.0",
|
||||||
|
"type": "range"
|
||||||
|
},
|
||||||
|
"_requiredBy": [
|
||||||
|
"/buffer-split"
|
||||||
|
],
|
||||||
|
"_resolved": "https://registry.npmjs.org/buffer-indexof/-/buffer-indexof-0.0.2.tgz",
|
||||||
|
"_shasum": "ed0f36b7ae166a66a7cd174c0467ae8dedf008f5",
|
||||||
|
"_shrinkwrap": null,
|
||||||
|
"_spec": "buffer-indexof@~0.0.0",
|
||||||
|
"_where": "/mnt/c/Users/Josua/Desktop/data/nodejs/electron/chat-project/chat-server/node_modules/buffer-split",
|
||||||
|
"author": {
|
||||||
|
"name": "Ryan Day"
|
||||||
|
},
|
||||||
|
"bugs": {
|
||||||
|
"url": "https://github.com/soldair/node-buffer-indexof/issues"
|
||||||
|
},
|
||||||
|
"dependencies": {},
|
||||||
|
"description": "find the index of a buffer in a buffe",
|
||||||
|
"devDependencies": {
|
||||||
|
"tape": "~1.1.0"
|
||||||
|
},
|
||||||
|
"directories": {},
|
||||||
|
"dist": {
|
||||||
|
"shasum": "ed0f36b7ae166a66a7cd174c0467ae8dedf008f5",
|
||||||
|
"tarball": "https://registry.npmjs.org/buffer-indexof/-/buffer-indexof-0.0.2.tgz"
|
||||||
|
},
|
||||||
|
"gitHead": "277ab0cedaf14f6d6ae18571e9dd4d0dea1981a0",
|
||||||
|
"homepage": "https://github.com/soldair/node-buffer-indexof",
|
||||||
|
"main": "index.js",
|
||||||
|
"maintainers": [
|
||||||
|
{
|
||||||
|
"name": "soldair",
|
||||||
|
"email": "soldair@gmail.com"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"name": "buffer-indexof",
|
||||||
|
"optionalDependencies": {},
|
||||||
|
"readme": "ERROR: No README data found!",
|
||||||
|
"repository": {
|
||||||
|
"url": "git://github.com/soldair/node-buffer-indexof.git"
|
||||||
|
},
|
||||||
|
"scripts": {
|
||||||
|
"test": "tape test/*.js"
|
||||||
|
},
|
||||||
|
"version": "0.0.2"
|
||||||
|
}
|
|
@ -0,0 +1,27 @@
|
||||||
|
var test = require('tape');
|
||||||
|
var bindexOf = require('../');
|
||||||
|
|
||||||
|
test("can haz working",function(t){
|
||||||
|
|
||||||
|
|
||||||
|
var newLineBuffer = new Buffer("\n");
|
||||||
|
|
||||||
|
var b = new Buffer("hi\nho\nsilver");
|
||||||
|
|
||||||
|
t.equals(bindexOf(new Buffer('a'), new Buffer('abc')), -1, 'should not match')
|
||||||
|
|
||||||
|
t.equals(bindexOf(new Buffer('aaa'), new Buffer('aa'), 2), -1, 'should not match')
|
||||||
|
|
||||||
|
t.equals(bindexOf(b,newLineBuffer),2,'should find newlines');
|
||||||
|
|
||||||
|
// you can also start from index
|
||||||
|
|
||||||
|
t.equals(bindexOf(b,newLineBuffer,3),5,"should find newlines after offset");
|
||||||
|
|
||||||
|
// no match === -1
|
||||||
|
|
||||||
|
t.equals(bindexOf(b,newLineBuffer,6),-1,"should not find newlines where none are.");
|
||||||
|
|
||||||
|
|
||||||
|
t.end();
|
||||||
|
})
|
|
@ -0,0 +1,4 @@
|
||||||
|
language: node_js
|
||||||
|
node_js:
|
||||||
|
- "0.8"
|
||||||
|
- "0.10"
|
|
@ -0,0 +1,20 @@
|
||||||
|
The MIT License (MIT)
|
||||||
|
|
||||||
|
Copyright (c) 2013 Ryan Day
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||||
|
this software and associated documentation files (the "Software"), to deal in
|
||||||
|
the Software without restriction, including without limitation the rights to
|
||||||
|
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||||
|
the Software, and to permit persons to whom the Software is furnished to do so,
|
||||||
|
subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||||
|
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||||
|
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||||
|
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||||
|
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@ -0,0 +1,40 @@
|
||||||
|
|
||||||
|
[![Build Status](https://secure.travis-ci.org/soldair/node-buffer-split.png)](http://travis-ci.org/soldair/node-buffer-split)
|
||||||
|
|
||||||
|
buffer-split
|
||||||
|
============
|
||||||
|
|
||||||
|
split a buffer by another buffer. think String.split()
|
||||||
|
|
||||||
|
```js
|
||||||
|
|
||||||
|
var bsplit = require('buffer-split')
|
||||||
|
, b = new Buffer("this is a buffer i like to split")
|
||||||
|
, delim = new Buffer('buffer')
|
||||||
|
, result = bsplit(b,delim)
|
||||||
|
;
|
||||||
|
|
||||||
|
result.length === 2
|
||||||
|
|
||||||
|
result[0].toString() === "this is a "
|
||||||
|
|
||||||
|
result[1].toString() === " i like to split"
|
||||||
|
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
you may include the delimiter in the result by passing a thrthy value as the third arg. its more efficient if you need it.
|
||||||
|
|
||||||
|
```js
|
||||||
|
var bsplit = require('buffer-split')
|
||||||
|
, b = new Buffer("this is a buffer i like to split")
|
||||||
|
, delim = new Buffer('buffer')
|
||||||
|
, result = bsplit(b,delim,true)
|
||||||
|
;
|
||||||
|
|
||||||
|
result[0].toString() === "this is a buffer"
|
||||||
|
|
||||||
|
result[1].toString() === " i like to split"
|
||||||
|
|
||||||
|
|
||||||
|
```
|
|
@ -0,0 +1,22 @@
|
||||||
|
var bufferIndexOf = require('buffer-indexof');
|
||||||
|
|
||||||
|
module.exports = function(buf,splitBuf,includeDelim){
|
||||||
|
|
||||||
|
var search = -1
|
||||||
|
, lines = []
|
||||||
|
, move = includeDelim?splitBuf.length:0
|
||||||
|
;
|
||||||
|
|
||||||
|
while((search = bufferIndexOf(buf,splitBuf)) > -1){
|
||||||
|
lines.push(buf.slice(0,search+move));
|
||||||
|
buf = buf.slice(search+splitBuf.length,buf.length);
|
||||||
|
}
|
||||||
|
|
||||||
|
lines.push(buf);
|
||||||
|
|
||||||
|
return lines;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,83 @@
|
||||||
|
{
|
||||||
|
"_args": [
|
||||||
|
[
|
||||||
|
"buffer-split",
|
||||||
|
"/mnt/c/Users/Josua/Desktop/data/nodejs/electron/chat-project/chat-server"
|
||||||
|
]
|
||||||
|
],
|
||||||
|
"_from": "buffer-split@latest",
|
||||||
|
"_id": "buffer-split@1.0.0",
|
||||||
|
"_inCache": true,
|
||||||
|
"_installable": true,
|
||||||
|
"_location": "/buffer-split",
|
||||||
|
"_nodeVersion": "4.4.4",
|
||||||
|
"_npmOperationalInternal": {
|
||||||
|
"host": "packages-12-west.internal.npmjs.com",
|
||||||
|
"tmp": "tmp/buffer-split-1.0.0.tgz_1465569032148_0.7547089036088437"
|
||||||
|
},
|
||||||
|
"_npmUser": {
|
||||||
|
"email": "soldair@gmail.com",
|
||||||
|
"name": "soldair"
|
||||||
|
},
|
||||||
|
"_npmVersion": "3.9.0",
|
||||||
|
"_phantomChildren": {},
|
||||||
|
"_requested": {
|
||||||
|
"name": "buffer-split",
|
||||||
|
"raw": "buffer-split",
|
||||||
|
"rawSpec": "",
|
||||||
|
"scope": null,
|
||||||
|
"spec": "latest",
|
||||||
|
"type": "tag"
|
||||||
|
},
|
||||||
|
"_requiredBy": [
|
||||||
|
"#USER"
|
||||||
|
],
|
||||||
|
"_resolved": "https://registry.npmjs.org/buffer-split/-/buffer-split-1.0.0.tgz",
|
||||||
|
"_shasum": "4427dbff53731b61d7a71aba47f503396613784a",
|
||||||
|
"_shrinkwrap": null,
|
||||||
|
"_spec": "buffer-split",
|
||||||
|
"_where": "/mnt/c/Users/Josua/Desktop/data/nodejs/electron/chat-project/chat-server",
|
||||||
|
"author": {
|
||||||
|
"name": "Ryan Day"
|
||||||
|
},
|
||||||
|
"bugs": {
|
||||||
|
"url": "https://github.com/soldair/node-buffer-split/issues"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"buffer-indexof": "~0.0.0"
|
||||||
|
},
|
||||||
|
"description": "split a buffer by another buffer. think String.split()",
|
||||||
|
"devDependencies": {
|
||||||
|
"tape": "~1.1.0"
|
||||||
|
},
|
||||||
|
"directories": {},
|
||||||
|
"dist": {
|
||||||
|
"shasum": "4427dbff53731b61d7a71aba47f503396613784a",
|
||||||
|
"tarball": "https://registry.npmjs.org/buffer-split/-/buffer-split-1.0.0.tgz"
|
||||||
|
},
|
||||||
|
"gitHead": "6d86666a2f7e4dc1f650f254bb11ba90b2d82567",
|
||||||
|
"homepage": "https://github.com/soldair/node-buffer-split#readme",
|
||||||
|
"keywords": [
|
||||||
|
"binary",
|
||||||
|
"buffer",
|
||||||
|
"chunks",
|
||||||
|
"split"
|
||||||
|
],
|
||||||
|
"main": "index.js",
|
||||||
|
"maintainers": [
|
||||||
|
{
|
||||||
|
"name": "soldair",
|
||||||
|
"email": "soldair@gmail.com"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"name": "buffer-split",
|
||||||
|
"optionalDependencies": {},
|
||||||
|
"readme": "ERROR: No README data found!",
|
||||||
|
"repository": {
|
||||||
|
"url": "git://github.com/soldair/node-buffer-split.git"
|
||||||
|
},
|
||||||
|
"scripts": {
|
||||||
|
"test": "node test/*.js"
|
||||||
|
},
|
||||||
|
"version": "1.0.0"
|
||||||
|
}
|
|
@ -0,0 +1,33 @@
|
||||||
|
var test = require('tape')
|
||||||
|
, bsplit = require('../')
|
||||||
|
;
|
||||||
|
|
||||||
|
test("can split",function(t){
|
||||||
|
|
||||||
|
var b = new Buffer("this is a buffer i like to split")
|
||||||
|
, delim = new Buffer('buffer')
|
||||||
|
, result = bsplit(b,delim)
|
||||||
|
;
|
||||||
|
|
||||||
|
t.equals(result.length,2,'should have found chunks');
|
||||||
|
|
||||||
|
t.equals(result[0].toString(),"this is a ","first chunk should not include delim");
|
||||||
|
|
||||||
|
t.equals(result[1].toString()," i like to split","should have all results");
|
||||||
|
|
||||||
|
|
||||||
|
result = bsplit(b,delim,true);
|
||||||
|
|
||||||
|
t.equals(result[0].toString(),"this is a buffer",'should include delim')
|
||||||
|
|
||||||
|
result = bsplit(new Buffer('foo,'),new Buffer(','));
|
||||||
|
|
||||||
|
t.equals(result.length,2,"should have all results");
|
||||||
|
|
||||||
|
result = bsplit(new Buffer('foo'),new Buffer(','));
|
||||||
|
|
||||||
|
t.equals(result.length,1,"should have all results");
|
||||||
|
|
||||||
|
t.end();
|
||||||
|
|
||||||
|
})
|
|
@ -26,7 +26,8 @@
|
||||||
"type": "tag"
|
"type": "tag"
|
||||||
},
|
},
|
||||||
"_requiredBy": [
|
"_requiredBy": [
|
||||||
"#USER"
|
"#USER",
|
||||||
|
"/"
|
||||||
],
|
],
|
||||||
"_resolved": "https://registry.npmjs.org/fs/-/fs-0.0.1-security.tgz",
|
"_resolved": "https://registry.npmjs.org/fs/-/fs-0.0.1-security.tgz",
|
||||||
"_shasum": "8a7bd37186b6dddf3813f23858b57ecaaf5e41d4",
|
"_shasum": "8a7bd37186b6dddf3813f23858b57ecaaf5e41d4",
|
||||||
|
|
|
@ -25,7 +25,8 @@
|
||||||
"type": "tag"
|
"type": "tag"
|
||||||
},
|
},
|
||||||
"_requiredBy": [
|
"_requiredBy": [
|
||||||
"#USER"
|
"#USER",
|
||||||
|
"/"
|
||||||
],
|
],
|
||||||
"_resolved": "https://registry.npmjs.org/net/-/net-1.0.2.tgz",
|
"_resolved": "https://registry.npmjs.org/net/-/net-1.0.2.tgz",
|
||||||
"_shasum": "d1757ec9a7fb2371d83cf4755ce3e27e10829388",
|
"_shasum": "d1757ec9a7fb2371d83cf4755ce3e27e10829388",
|
||||||
|
|
|
@ -31,7 +31,8 @@
|
||||||
"type": "tag"
|
"type": "tag"
|
||||||
},
|
},
|
||||||
"_requiredBy": [
|
"_requiredBy": [
|
||||||
"#USER"
|
"#USER",
|
||||||
|
"/"
|
||||||
],
|
],
|
||||||
"_resolved": "https://registry.npmjs.org/node-rsa/-/node-rsa-1.0.5.tgz",
|
"_resolved": "https://registry.npmjs.org/node-rsa/-/node-rsa-1.0.5.tgz",
|
||||||
"_shasum": "854dc1b275729d69bc25883f83ca80705db9262e",
|
"_shasum": "854dc1b275729d69bc25883f83ca80705db9262e",
|
||||||
|
|
|
@ -0,0 +1,4 @@
|
||||||
|
1.0.0 / 2016-01-17
|
||||||
|
==================
|
||||||
|
|
||||||
|
* Initial release
|
|
@ -0,0 +1,21 @@
|
||||||
|
The MIT License (MIT)
|
||||||
|
|
||||||
|
Copyright (c) 2016 Douglas Christopher Wilson <doug@somethingdoug.com>
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in
|
||||||
|
all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
THE SOFTWARE.
|
|
@ -0,0 +1,77 @@
|
||||||
|
# random-bytes
|
||||||
|
|
||||||
|
[![NPM Version][npm-image]][npm-url]
|
||||||
|
[![NPM Downloads][downloads-image]][downloads-url]
|
||||||
|
[![Node.js Version][node-version-image]][node-version-url]
|
||||||
|
[![Build Status][travis-image]][travis-url]
|
||||||
|
[![Test Coverage][coveralls-image]][coveralls-url]
|
||||||
|
|
||||||
|
Generate strong pseudo-random bytes.
|
||||||
|
|
||||||
|
This module is a simple wrapper around the Node.js core `crypto.randomBytes` API,
|
||||||
|
with the following additions:
|
||||||
|
|
||||||
|
* A `Promise` interface for environments with promises.
|
||||||
|
* For Node.js versions that do not wait for the PRNG to be seeded, this module
|
||||||
|
will wait a bit.
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
|
||||||
|
```sh
|
||||||
|
$ npm install random-bytes
|
||||||
|
```
|
||||||
|
|
||||||
|
## API
|
||||||
|
|
||||||
|
```js
|
||||||
|
var randomBytes = require('random-bytes')
|
||||||
|
```
|
||||||
|
|
||||||
|
### randomBytes(size, callback)
|
||||||
|
|
||||||
|
Generates strong pseudo-random bytes. The `size` argument is a number indicating
|
||||||
|
the number of bytes to generate.
|
||||||
|
|
||||||
|
```js
|
||||||
|
randomBytes(12, function (error, bytes) {
|
||||||
|
if (error) throw error
|
||||||
|
// do something with the bytes
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
### randomBytes(size)
|
||||||
|
|
||||||
|
Generates strong pseudo-random bytes and return a `Promise`. The `size` argument is
|
||||||
|
a number indicating the number of bytes to generate.
|
||||||
|
|
||||||
|
**Note**: To use promises in Node.js _prior to 0.12_, promises must be
|
||||||
|
"polyfilled" using `global.Promise = require('bluebird')`.
|
||||||
|
|
||||||
|
```js
|
||||||
|
randomBytes(18).then(function (string) {
|
||||||
|
// do something with the string
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
|
### randomBytes.sync(size)
|
||||||
|
|
||||||
|
A synchronous version of above.
|
||||||
|
|
||||||
|
```js
|
||||||
|
var bytes = randomBytes.sync(18)
|
||||||
|
```
|
||||||
|
|
||||||
|
## License
|
||||||
|
|
||||||
|
[MIT](LICENSE)
|
||||||
|
|
||||||
|
[npm-image]: https://img.shields.io/npm/v/random-bytes.svg
|
||||||
|
[npm-url]: https://npmjs.org/package/random-bytes
|
||||||
|
[node-version-image]: https://img.shields.io/node/v/random-bytes.svg
|
||||||
|
[node-version-url]: http://nodejs.org/download/
|
||||||
|
[travis-image]: https://img.shields.io/travis/crypto-utils/random-bytes/master.svg
|
||||||
|
[travis-url]: https://travis-ci.org/crypto-utils/random-bytes
|
||||||
|
[coveralls-image]: https://img.shields.io/coveralls/crypto-utils/random-bytes/master.svg
|
||||||
|
[coveralls-url]: https://coveralls.io/r/crypto-utils/random-bytes?branch=master
|
||||||
|
[downloads-image]: https://img.shields.io/npm/dm/random-bytes.svg
|
||||||
|
[downloads-url]: https://npmjs.org/package/random-bytes
|
|
@ -0,0 +1,101 @@
|
||||||
|
/*!
|
||||||
|
* random-bytes
|
||||||
|
* Copyright(c) 2016 Douglas Christopher Wilson
|
||||||
|
* MIT Licensed
|
||||||
|
*/
|
||||||
|
|
||||||
|
'use strict'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Module dependencies.
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
|
||||||
|
var crypto = require('crypto')
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Module variables.
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
|
||||||
|
var generateAttempts = crypto.randomBytes === crypto.pseudoRandomBytes ? 1 : 3
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Module exports.
|
||||||
|
* @public
|
||||||
|
*/
|
||||||
|
|
||||||
|
module.exports = randomBytes
|
||||||
|
module.exports.sync = randomBytesSync
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generates strong pseudo-random bytes.
|
||||||
|
*
|
||||||
|
* @param {number} size
|
||||||
|
* @param {function} [callback]
|
||||||
|
* @return {Promise}
|
||||||
|
* @public
|
||||||
|
*/
|
||||||
|
|
||||||
|
function randomBytes(size, callback) {
|
||||||
|
// validate callback is a function, if provided
|
||||||
|
if (callback !== undefined && typeof callback !== 'function') {
|
||||||
|
throw new TypeError('argument callback must be a function')
|
||||||
|
}
|
||||||
|
|
||||||
|
// require the callback without promises
|
||||||
|
if (!callback && !global.Promise) {
|
||||||
|
throw new TypeError('argument callback is required')
|
||||||
|
}
|
||||||
|
|
||||||
|
if (callback) {
|
||||||
|
// classic callback style
|
||||||
|
return generateRandomBytes(size, generateAttempts, callback)
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Promise(function executor(resolve, reject) {
|
||||||
|
generateRandomBytes(size, generateAttempts, function onRandomBytes(err, str) {
|
||||||
|
if (err) return reject(err)
|
||||||
|
resolve(str)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generates strong pseudo-random bytes sync.
|
||||||
|
*
|
||||||
|
* @param {number} size
|
||||||
|
* @return {Buffer}
|
||||||
|
* @public
|
||||||
|
*/
|
||||||
|
|
||||||
|
function randomBytesSync(size) {
|
||||||
|
var err = null
|
||||||
|
|
||||||
|
for (var i = 0; i < generateAttempts; i++) {
|
||||||
|
try {
|
||||||
|
return crypto.randomBytes(size)
|
||||||
|
} catch (e) {
|
||||||
|
err = e
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
throw err
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generates strong pseudo-random bytes.
|
||||||
|
*
|
||||||
|
* @param {number} size
|
||||||
|
* @param {number} attempts
|
||||||
|
* @param {function} callback
|
||||||
|
* @private
|
||||||
|
*/
|
||||||
|
|
||||||
|
function generateRandomBytes(size, attempts, callback) {
|
||||||
|
crypto.randomBytes(size, function onRandomBytes(err, buf) {
|
||||||
|
if (!err) return callback(null, buf)
|
||||||
|
if (!--attempts) return callback(err)
|
||||||
|
setTimeout(generateRandomBytes.bind(null, size, attempts, callback), 10)
|
||||||
|
})
|
||||||
|
}
|
|
@ -0,0 +1,94 @@
|
||||||
|
{
|
||||||
|
"_args": [
|
||||||
|
[
|
||||||
|
"random-bytes",
|
||||||
|
"/mnt/c/Users/Josua/Desktop/data/nodejs/electron/chat-project/chat-server"
|
||||||
|
]
|
||||||
|
],
|
||||||
|
"_from": "random-bytes@latest",
|
||||||
|
"_id": "random-bytes@1.0.0",
|
||||||
|
"_inCache": true,
|
||||||
|
"_installable": true,
|
||||||
|
"_location": "/random-bytes",
|
||||||
|
"_npmUser": {
|
||||||
|
"email": "doug@somethingdoug.com",
|
||||||
|
"name": "dougwilson"
|
||||||
|
},
|
||||||
|
"_npmVersion": "1.4.28",
|
||||||
|
"_phantomChildren": {},
|
||||||
|
"_requested": {
|
||||||
|
"name": "random-bytes",
|
||||||
|
"raw": "random-bytes",
|
||||||
|
"rawSpec": "",
|
||||||
|
"scope": null,
|
||||||
|
"spec": "latest",
|
||||||
|
"type": "tag"
|
||||||
|
},
|
||||||
|
"_requiredBy": [
|
||||||
|
"#USER"
|
||||||
|
],
|
||||||
|
"_resolved": "https://registry.npmjs.org/random-bytes/-/random-bytes-1.0.0.tgz",
|
||||||
|
"_shasum": "4f68a1dc0ae58bd3fb95848c30324db75d64360b",
|
||||||
|
"_shrinkwrap": null,
|
||||||
|
"_spec": "random-bytes",
|
||||||
|
"_where": "/mnt/c/Users/Josua/Desktop/data/nodejs/electron/chat-project/chat-server",
|
||||||
|
"bugs": {
|
||||||
|
"url": "https://github.com/crypto-utils/random-bytes/issues"
|
||||||
|
},
|
||||||
|
"contributors": [
|
||||||
|
{
|
||||||
|
"name": "Douglas Christopher Wilson",
|
||||||
|
"email": "doug@somethingdoug.com"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"dependencies": {},
|
||||||
|
"description": "URL and cookie safe UIDs",
|
||||||
|
"devDependencies": {
|
||||||
|
"bluebird": "3.1.1",
|
||||||
|
"istanbul": "0.4.2",
|
||||||
|
"mocha": "2.3.4",
|
||||||
|
"proxyquire": "1.2.0"
|
||||||
|
},
|
||||||
|
"directories": {},
|
||||||
|
"dist": {
|
||||||
|
"shasum": "4f68a1dc0ae58bd3fb95848c30324db75d64360b",
|
||||||
|
"tarball": "https://registry.npmjs.org/random-bytes/-/random-bytes-1.0.0.tgz"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">= 0.8"
|
||||||
|
},
|
||||||
|
"files": [
|
||||||
|
"HISTORY.md",
|
||||||
|
"LICENSE",
|
||||||
|
"README.md",
|
||||||
|
"index.js"
|
||||||
|
],
|
||||||
|
"gitHead": "3dcd47425a3dfe858ee8debcd4db0c1222110bc3",
|
||||||
|
"homepage": "https://github.com/crypto-utils/random-bytes",
|
||||||
|
"keywords": [
|
||||||
|
"bytes",
|
||||||
|
"generator",
|
||||||
|
"random",
|
||||||
|
"safe"
|
||||||
|
],
|
||||||
|
"license": "MIT",
|
||||||
|
"maintainers": [
|
||||||
|
{
|
||||||
|
"name": "dougwilson",
|
||||||
|
"email": "doug@somethingdoug.com"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"name": "random-bytes",
|
||||||
|
"optionalDependencies": {},
|
||||||
|
"readme": "ERROR: No README data found!",
|
||||||
|
"repository": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "git+https://github.com/crypto-utils/random-bytes.git"
|
||||||
|
},
|
||||||
|
"scripts": {
|
||||||
|
"test": "mocha --trace-deprecation --reporter spec --bail --check-leaks test/",
|
||||||
|
"test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --trace-deprecation --reporter dot --check-leaks test/",
|
||||||
|
"test-travis": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --trace-deprecation --reporter spec --check-leaks test/"
|
||||||
|
},
|
||||||
|
"version": "1.0.0"
|
||||||
|
}
|
|
@ -0,0 +1,3 @@
|
||||||
|
{
|
||||||
|
"encryption_key_size": 100000
|
||||||
|
}
|
Loading…
Reference in New Issue