How to create hmac sha512 hash? - firefox-addon-sdk

I'm developing an addon and have to create a hmac sha512 hash but jpm says that the crypto module can't be found (https://nodejs.org/api/crypto.html#crypto_crypto_createhmac_algorithm_key) - can't I use standard node modules in firefox addon development?
Sorry I'm very new to this.
If not, is there another way to create the hash?

One can only use npm-hoisted third-party SDK modules and currently there are not many modules of this type e.g menuitem.
If you want to create hash then you can use crypto.js library.But you cannot use it directly in index.js.For that you have to create pageworker and do message passing whenever you want to create hash.
To create pageworker in index.js your code will something look like this :
var HashWorker = require('sdk/page-worker').Page({
contentURL: "./hash.html",
contentScriptFile : ["./crypto.js","./myfile.js"]
});
In myfile.js you will use crypto.js functions to create hash.Note that all the files hash.html,crypto.js and myfile.js must be in data directory of your addon.
hash.html will look something look like this :
<html>
<head>
<title></title>
</head>
<body>
</body>
</html>
Once all this setup,you can communicate from index.js to myfile.js or vice versa through message passing.
To create hash of something you can send message to myfile.js that will look something like this :
index.js
//send message to myfile.js to create hash of some value
HashWorker.port.emit('createHash',{data : "whose hash to create"});
//message received from myfile.js containing hash of specified value
HashWorker.port.on('hash_result',function(message){
console.log(message.hash);
});
In myfile.js message passing will look something like this :
myfile.js
//message received from index.js to create hash of specified value
self.port.on('createHash',function(message){
var value = message.data
var hash = ...//cryptojs function to create hash of above value.
//send hash created to index.js
self.port.emit('hash_result',{hash : hash});
});

can't I use standard node modules in firefox addon development?
What you mean are core modules and no you cannot.
You need to look for a browser-compatible crypto module. Here is one

You can use Mozilla's own Components
const { Cu } = require("chrome");
Cu.importGlobalProperties(["crypto"]);
imports the crypto object into the addon. Since you just asked for that, you might be aware of how to use it to create a hash. The Example says to use it like
function sha256(str) {
// We transform the string into an arraybuffer.
var buffer = new TextEncoder("utf-8").encode(str);
return crypto.subtle.digest("SHA-256", buffer).then(function (hash) {
// whatever you want to do with hash;
});
}

Related

How do I generate a content hash using sha256 hash of the request body content, using the private api key with Wix Corvid

I am seeking any information attainable in reference to creating a content hash. I am very new to the coding world and would appreciate any feedback. I have searched the Wix API Documentation and am unable to find any information pertaining to this in the forum either.
You need to install npm package into Wix. For example:
js-sha256
Then you can use this code only in backend side:
var sha256 = require('js-sha256');
export async function testSha256(param) {
let p = await sha256.sha256(param);
return p;
}
Then call this script from frontend, like as:
let hashed = await testSha256('test');

How to inject data from db into jade template?

I am trying to import data from mongodb into jade template, in order to show them in graph. I am using chart.js inline script, in order to render data on graph. So data from Mongodb is on the page, and I can access it like this:
each city in cities
p #{city.name}
And here is how I pass data to page:
exports.index = function(req,res){
var cities = City.find({}).limit(20);
cities.exec(function(err,doc) {
res.render("index",{cities:doc});
});
};
I created same app with php, by simply passing data to page and injecting data into javascript graph(with json_encode)
Here is final result with php:
It was easy, since php data are global to HTML page. How to do that with Jade ?
Thanks
Ah, so your goal is to both use the city data to generate HTML on the server but ALSO make it available in the browser as raw javascript object data. To do that you format it as JSON and stuff it into a <script> tag. There are modules to help with this such as sharify,
but the basic idea in your jade template do something like:
In your express route handler javascript:
exports.index = function(req,res){
var cities = City.find({}).limit(20);
cities.exec(function(err,doc) {
//There are tricky rules about safely embedding JSON within HTML
//see http://stackoverflow.com/a/4180424/266795
var encodedCityData = 'window.cities = ' + JSON.stringify(cities)
.replace(/</g, '\\u003c')
.replace(/-->/g, '--\\>');
res.render("index",{cities:doc, encodedCityData: encodedCityData});
});
};
In your jade template:
script!= encodedCityData
In the browser, you will have access to the data via the cities variable which has been set onto the window object.

Restangular sends empty payload for keys starting with "$"

I'm using Restangular to connect with Mongolab. I'd like to use the following code to push an update:
var theData = {"$push":{exercises :{type: "running"}}};
Restangular.all('employees').one(user._id.$oid).customPUT(theData ,null, {apiKey: apiKey});
When I run this and look at the XHR request, the payload is always set to {}.
However, if I pull out the $, my payload looks like this:
{"push":{exercises :{type: "running"}}}
In that instance, the payload looks fine, but mongolab thinks I'm wanting to add a field named "push" instead of pushing to the excercises array since I'm not using the "$push" keyword.
I can have the "$" anywhere in the string except at the front (e.g " $push" and "push$" work) but unfortunately, that's what mongo requires in order to push an update. Is there some setting that I'm missing, or is this a bug in restangular?
yes, the $ will be striped: before the data is send, the data will be transformed with angular.toJson function:
#name angular.toJson
#function
#description
Serializes input into a JSON-formatted string. Properties with leading $ characters will be
stripped since angular uses this notation internally.
If you don't want this behavior you have to provide a transformRequest function (http://docs.angularjs.org/api/ng.$http). If your data is already json you may just write:
transformRequest: function(data){
return data;
}
the transformRequest must be provided as option during resource configuration. see
http://docs.angularjs.org/api/ngResource.$resource

from javascript to jinja2

Is it possible to access data obtained via .getJSON call in jquery as a jinja2 variable ?
$.getJSON(
$SCRIPT_ROOT +"/gitem/"+node.id,
function(data){
if (data.length > 0){
$.each(data, function(index,val_dict) {
var button_id = "button_"+String(index);
var popup_id = "element_to_pop_up_"+String(index);
var append_string = sprintf('<div class="icony"><img src="%s" height="75" id="%s" >%s</div>',val_dict.img_url,button_id,val_dict.img_caption);
var bpopup_element = sprintf('<div id="%s"><a class="bClose"><img src="%s" width="500px">X<a/></div>',popup_id,val_dict.img_url)
$('.data_area').append(append_string+bpopup_element);
$('#'+popup_id).hide();
$('#'+button_id).bind('click', function(e) {
e.preventDefault();
$('#'+popup_id).bPopup();
});
});
}
else
{
var append_string = '<div class="icony">No Images to display for this category</div>';
$('.data_area').append(append_string);
}
$("#list_viewer").css("display", "block");
});
For example in the above I am getting the data and then constructing the DOM within js. While what would be best would be to import data via .getJSON and then set the data as a jinja2 variable.
Later I can use that variable within jinja2 template ?
it that possible ?
or better still...
can a jinja macro be called from within the .getJSON function ? that can also allow embedding of json data within jinja2....
thanks for any pointer...
Probably not. Jinja generally runs on the server, rendering your web site templates before sending them off to the client. The client (i.e. your web browser) will not have access to the templates.
If you'd like to use Jinja templates to render client-side JSON objects, you could take a look at Jasinja, which is a tool to convert your Jinja templates to JavaScript, which you can then use in the browser. A number of similar tools exist.
Finally, another solution is to send your JSON data to the web server, render them to HTML via Jinja, then send back the resulting HTML in the XMLHttpRequest response. However, if you've obtained this JSON data from the same server you use to render templates, you might be better off just having HTML sent to the client directly and added to the DOM from there.

How to get parameter come after '#' in Perl CGI?

How can we fetch the value of folders from below mentioned url:
http://my.indiamart.com/cgi/​my-enquiries.mp#folders=1
I've tried CGI object, %ENV variable and so many things, but still not been able to get it.
Please suggest..
You can't, the browser interprets the fragment (#folders=1) without sending it to the server. So if http://my.indiamart.com/cgi/​my-enquiries.mp is your script, then it will never see the #folders=1 part of the URL as the browser won't send it. If you need the fragment on the server then you'll have to change it to a CGI parameter:
http://my.indiamart.com/cgi/​my-enquiries.mp?folders=1
or embed it in the URL path, something like one of these:
http://my.indiamart.com/cgi/​my-enquiries.mp/1
http://my.indiamart.com/cgi/​my-enquiries.mp/folders=1
You can't, # is recognized by JavaScript only,
apache will ignore this, that's the reason it does not contains any value in ENV variable.
You can use JavaScript: window.location.hash to capture this hash value.
Only with javascript.
You can use something like that to redirect to another script
<script>
if(window.location.hash) {
var str = window.location.hash.substring(1);
window.location.href = 'http://other_script.pl?param=' + str;
}
</script>