Create a base64 md5 hash in nodejs equivalent to this openssl command - node.js

I have a linux command to create argument value, but I dont know how to convert it in nodejs. This is linux command line:
echo -n '2147483647/s/link127.0.0.1 secret' | \
openssl md5 -binary | openssl base64 | tr +/ -_ | tr -d =
and result when execute it in terminal
_e4Nc3iduzkWRm01TBBNYw
Please tell me how to make it in nodejs without child process.

Any terminal command can be executed in Node.js by using the exec or spawn. In this case, exec will probably be your best bet. Follow the pattern below just replace my command to list the directories in /home/username with whatever command you want:
var exec = require('child_process').exec;
exec("ls /home/username", function (error, stdout, stderr) {
console.log("error: ", error);
console.log("stdout: ", stdout);
console.log("stderr: ", stderr);
});

Done
var mysecretkey = "secret";
var path = "/s/link";
var ip = '127.0.0.1';
var time = '2147483647';
var path = time + path + ip + ' ' + mysecretkey;
var crypto = require('crypto');
var md5sum = crypto.createHash('md5');
var d = md5sum.update(path).digest('base64');
//#echo -n '2147483647/s/link127.0.0.1 secret' | openssl md5 -binary | openssl base64 | tr +/ -_ | tr -d =
var test = d.replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, '');
console.log(test);

Related

Mimicking the openssl call in node with another library

I am running this openssl code to generate a token to be used to encrypt my hls stream.
"openssl rand 16 > '" + folder + "/master.key' && echo '" + hex + "' | xxd -r -p > '" + folder + "/master.key'"
The openssl library has issues on windows for I am looking for a way to mimic the above call in a different node library.
I have tried crypto with node js with the call below but it doesn't seem to work.
require('crypto').randomBytes(16, function(err, buffer) {
var hex = buffer.toString('hex');
console.log(hex);
});
Can anyone suggest a way to do this?
Thanks
function hex2bin(hex){
return new Buffer(hex, "hex");
}
require('crypto').randomBytes(16, function(err, buffer) {
var hex = buffer.toString('hex');
fs.writeFile(folder + "/master.key", hex2bin(hex), function (err) {
});
});
Sussed it

How to exec script to set iterm2 Badge from nodejs?

I get this bash script from Iterm2 official site.
printf "\e]1337;SetBadgeFormat=%s\a" $(echo "text" | base64)
I tried exec like bellow, there is no error, but failed to set iterm2 Badge
var exec = require('child_process').exec;
exec('printf "\e]1337;SetBadgeFormat=%s\a" $(echo "text" | base64)');
setBadgeFormat.js =>
#!/usr/bin/env node
var rawBadgeFormat = 'test'
var base64BadgeFormat = new Buffer(rawBadgeFormat).toString('base64')
var setBadgeFormatCmd = 'printf "\\e]1337;SetBadgeFormat=' + base64BadgeFormat + '\\a"'
require('child_process').exec(setBadgeFormatCmd, function(error, stdout, stderr) {
if (error) console.log(error);
process.stdout.write(stdout); // this line actually do the trick
process.stderr.write(stderr);
});

Node child_process.spawn multiple commands

I wan to automate the creation and extracting of keystore.
The problem I'm facing is how to join the commands using the ' | ' symbol or similar solution.
//Original Command
var command='keytool -exportcert -storepass mypass -keypass mypass
-alias myalias -keystore mykey.keystore | openssl sha1 -binary | openssl base64';
//Arguments for the spawn
var keyArgs = [
'-exportcert',
'-storepass','mypass',
'-keypass','mypass',
'-alias','myalias',
'-keystore',"myjey.keystore",
'openssl','sha1',
'-binary',
'openssl','base64',
];
exec('keytool',keyArgs,{cwd:appCreateFolder+"/"+opt.id+"/Certificates"},function(e){
console.log(chalk.cyan('Key created'));
})
From Node.js v6 you can specify a shell option in spawn method which will run command using shell and thus it is possible to chain commands using spawn method.
For example this:
var spawn = require('child_process').spawn;
var child = spawn('ls && ls && ls', {
shell: true
});
child.stderr.on('data', function (data) {
console.error("STDERR:", data.toString());
});
child.stdout.on('data', function (data) {
console.log("STDOUT:", data.toString());
});
child.on('exit', function (exitCode) {
console.log("Child exited with code: " + exitCode);
});
Will trigger an error on node.js version less than 6:
Error: spawn ls && ls && ls ENOENT
But on version 6 and higher it will return expected result:
node app.js
STDOUT: app.js
STDOUT: app.js
app.js
Child exited with code: 0
The | symbol on the command line is called "piping" because it's like piping streams of data together. What you want is to get ahold of the stdin (Standard In) and stdout (Standard Out) streams for the commands you're executing.
For example, this is how you would spawn the echo command and pipe it's output to grep:
var spawn = require('child_process').spawn;
var echo = spawn('echo', ['The quick brown fox\njumped over the lazy dog.']);
var grep = spawn('grep', ['brown']);
echo.stdout.pipe(grep.stdin);
grep.stdout.pipe(process.stdin);
The above example spawns both the "echo" and "grep" commands. It pipes any output from the echo process's stdout stream to the grep process's stdin stream. Finally we pipe the grep process's stdout stream to the parent process's (your node process) stdin stream so you can see the output in your terminal.
The output would be "The quick brown fox" because I put a newline character in the middle and the grep only matched the first line containing "brown".
You could use the exec function to achieve the same result. Just might be harder to maintain in the future, but if all you need is to quickly run a set of piped commands, you can enter the full command line string (including pipe symbols) and pass it to exec.
var exec = require('child_process').exec;
var cmdString = 'grep "The quick brown fox\njumped over the lazy dog." | grep "brown"';
exec(cmdString, (err, stdout, stderr) => {
console.log(stdout);
});
Or instead of passing in the callback function you could just pipe the output to process.stdin if all you care about is seeing the command output.
exec(cmdString).stdout.pipe(process.stdin);
Here's a quick example of what I believe your code should look like using spawn. May require tweaks since it seems specific to what you're doing.
var keyArgs = [
'-exportcert',
'-storepass','mypass',
'-keypass','mypass',
'-alias','myalias',
'-keystore',"myjey.keystore",
'openssl','sha1',
'-binary',
'openssl','base64',
];
var keyOpts = {
cwd: `${appCreateFolder}/${opt.id}/Certificates`
};
var spawn = require('child_process').spawn;
var keytool = spawn('keytool', keyArgs, keyOpts);
var opensslBinary = spawn('openssl', ['sha1', '-binary']);
var opensslBase64 = spawn('openssl', ['base64']);
keytool.stdout.pipe(opensslBinary.stdin);
opensslBinary.stdout.pipe(opensslBase64.stdin);
opensslBase64.stdout.pipe(process.stdin);
opensslBase64.on('close', () => {
console.log(chalk.cyan('Key created.'));
});
Or using exec:
var exec = require('child_process').exec;
var cmdString = 'keytool -exportcert -storepass mypass -keypass mypass -alias myalias -keystore mykey.keystore | openssl sha1 -binary | openssl base64';
var cmdOpts = {
cwd: `${appCreateFolder}/${opt.id}/Certificates`
};
exec(cmdString, cmdOpts, () => {
console.log(chalk.cyan('Key created.'));
});

How to use wildcards in nodejs child_process.exec

Why does this not work? How to do it right?
var exec = require("child_process").exec;
exec("ls data/tile_0_{0..63}.jpg", function(error, stdout, stderr){
console.log('stdout: ' + stdout);
console.log('stderr: ' + stderr)}
);
// stdout:
// stderr: ls: cannot access data/tile_0_{0..63}.jpg: No such file or directory
In bash terminal this lists all files like tile_0_0.jpg, tile_0_1.jpg, etc.
This executed from node works as expected but does not do the thing I want.
ls data/tile_61_[0-9].jpg
Please help me ...
I'm using Linux Mint 14.
Why not do
var _ = require('underscore'),
fs = require('fs'),
files = fs.readdirSync('data');
var filtered = _.filter(files, function(filename){
return filename.indexOf('tile_0_') == 0;
});

Node.js cypher and OpenSSL differ

Nodejs Code
var crypto = require("crypto");
var cypher = crypto.createCipher("aes192", "pass");
var out = cypher.update("TEST1","utf8", "binary");
out += cypher.final("binary");
console.log(out);
NODE OUTPUT:
´_ËT~R dE{
Command Line:
echo -n "TEST1" | openssl enc -aes192
CLI OUTPUT:
Salted__?
????X-N??R?*a8 P9?t%
What am I doing wrong?
PD: Yeah, I know those are binary outputs but still they clearly don't match.
You're missing the -nosalt flag to openssl.

Resources