Diffie Hellman algorithm on browser - node.js

NodeJS have crypto module where DiffieHellman is a class. So, I can use this method to generate key and compute key.
But, client also need to create another instance of diffiehellman class. But how to do that? Can I use crypto module on client side? If yes then how, any solution? Here are my client side code...
const crypto = require('crypto');
const express = require('express');
const app = express();
// Generate server's keys...
const server = crypto.createDiffieHellman(139);
const serverKey = server.generateKeys();
//send p=prime and g=generator to the client

Node.js has own "crypto" module to use DiffieHellman algorithm, so you can watch it and write it on browser on your own.
Second way is take library ready for use (on github or else), e.g. this one.

Related

Best Practice to create REST APIs using node.js

I am from .Net and C# background and I am new to Node.js. I am working on a project, which is mix of MongoDB and Node.JS.
In MongoDB, data from various tools is stored in different different collections. I have to create multiple REST APIs using Node.JS for CRUD operation on that data, these APIs will be called from React.JS application.
I want to keep APIs into separate files for seperate tool and then calling including all files into app.js file.
Please help me with best approach.
For POC purpose, I created a node.js application, where I created app.js file and written all my code for GET|POST|DELETE APIs. This is working fine.
var _expressPackage = require("express");
var _bodyParserPackage = require("body-parser");
var _sqlPackage = require("mssql");
var app = _expressPackage();
var cors = require("cors");
var auth = require('basic-auth');
var fs = require('fs');
const nodeMailer = require('nodemailer');
//Lets set up our local server now.
var server = app.listen(process.env.PORT || 4000, function () {
var port = server.address().port;
console.log("App now running on port", port);
});
app.get("/StudentList", function(_req ,_res){
console.log("Inside StudentList");
var Sqlquery = "select * from tbl_Host where HostId='1'";
GetQueryToExecuteInDatabase(_req,_res, Sqlquery,function(err,data){
console.log(data);
});
});
Don't know exactly what your app intends to do, but usually if you are not serving webpages and your API is not too complex, there is no need to use express. You can build a simple server natively in NodeJS to serve data.
Additionally, if your app has many routes (or is likely to in the future), it is a good idea to put helper functions like GetQueryToExecuteInDatabase() in a separate file outside of app.js such as utils.js.
Based on what I have understood about what you want to do, your file structure should look something like this:
data (db related files)
services (contains one file per api service)
app.js
utils.js
Hope this helps.

How do I utilize certificates/auth with web-grpc?

I am working with grpc-web, essentially trying to port a working server side vanilla grpc/node example to the browser. My existing node client example connects to the grpc service like so.
var services = require('./my_services_grpc_pb');
var grpc = require('grpc');
var secureClient = new services.MyServicesServiceClient(
(host + ":" + port),
grpc.credentials.createSsl(
fs.readFileSync(certFile)
)
);
That seems to do what I want. However, when I try to include the line
var grpc = require('grpc');
And then bundle my code with webpack, I get an error "cannot resolve child_process". I did a bit of googling and found that child_process does not work in the browser. I'm in a bit of a bind now, because the code examples that I saw on the web-grpc-tutorial do not seem show how to establish a secure connection. They look like this
var echoService = new EchoServiceClient('http://localhost:8080');
How would I go about passing certs to the call/establishing a secure channel?
There is an example of using grpc-web with TLS here: https://github.com/salrashid123/grpc_web_with_gke. There's a section on how to use certs. You do not have to use GKE if you don't need to.

Connect Browser Microphone stream to Google Speech Api via node

I just started with node.js and am trying to connect the generated microphone stream from the browser with the google speech api running on my node server and the microphone-stream package.
I successfully packed the necessary modules with browserify, but now don't know how to proceed. I got the microphone stream to work on the node server as well (as explained here: Streaming Speech Recognition on an Audio Stream ).
How can I transmit the audiostream? I read about using websockets in one issue, but didn't really understand if it's the right way in my case. Or RPC?
For now I'm using these packages on the server:
const express = require('express');
const path = require('path');
const bodyParser = require('body-parser');
const fs = require('fs');
const record = require('node-record-lpcm16');
const google = require('googleapis');
const getUserMedia = require('get-user-media-promise');
const MicrophoneStream = require('microphone-stream');
This is my first time using node / a server, so hopefully this question isn't too naive.
Thanks! :)
I built a playground to tackle this task. It doesn't use any of the previous plugins (node record 16 / microphone-stream / ...) but sends a 16 bit audio stream to the node server via socket.io.
https://github.com/vin-ni/Google-Cloud-Speech-Node-Socket-Playground

Cant use Crypto in SuiteCommerce Advanced Service?

My SuiteScript Service MyService.Service.ss file is attempting to use the Node.js library Crypto but I get the error Error: No crypto when I run my service.
Does the Node.js version that SCA uses not have the Crypto library installed? Do I need to explicitly add Crypto as a dependency to distro.json? If so where? Do I need to install Crypto? If so, any advice how - I'm new to Node.js.
I am using pretty much standard Crypto functions, see below for the code that causes the problem:
function service (request)
{
'use strict';
var crypto = require('crypto'); // Error here
var token = crypto.createHmac('md5', public_key)
.update(private_key)
.digest('hex');
...
}
Netsuite doesn't use the V8 engine so you are pretty much out of luck with crypto.
I've used Paul Johnston's md5 package for hmac calc for years and it is fast enough and interoperates well. Name says MD5 but it includes SHA-1,256,512 as well.

Any Easy to extend Web-based File Manager for node.js?

Want to find one and modify that to read, write file on mongoDB gridfs store
You could try Cloud Commander node.js-based orthodox file manager. It could be used as middleware for express this way:
var http = require('http'),
cloudcmd = require('cloudcmd'),
express = require('express'),
io = require('socket.io'),
app = express(),
PORT = 1337,
server,
socket;
server = http.createServer(app);
socket = io.listen(server);
app.use(cloudcmd({
socket: socket, /* used by Config, Edit (optional) and Console (required) */
config: { /* config data (optional) */
prefix: '/cloudcmd', /* base URL or function which returns base URL (optional) */
}
}));
server.listen(PORT);
When you need a file tree you could try this angular/node based example.
Nodepad:
This tutorial will give you a good idea on how it was made: http://dailyjs.com/2010/11/01/node-tutorial/ As knowing how it was made it should make it heaps easer to extend it. :) It is quite basic, I'm re-writing the whole thing myself :P
Nodepad on github: https://github.com/alexyoung/nodepad
It even uses MongoDB, I'm going to do the opposite to what you wanted to do and that is make it not use MongoDB. :P
Take a look at https://github.com/OpusCapita/filemanager
It has a NodeJS server implementation and react client implementation.
Can be easily extended using "connectors"

Resources