node.js DIGEST-MD5 calculation: calculate 16 octect MD5 hash - node.js

I'm trying to implement DIGEST-MD5 with node.js but it doesn't seem to work correctly.
Currently, I tried to implement it the following way:
function md5(str) {
var hash = crypto.createHash('md5');
hash.update(str);
return hash.digest('binary');
}
var A1 = md5(username + ':' + realm + ':' + password);
When I console.log this value (with username = "test", realm = "" and password = "123), the following appears: "EïSÓ*JÉHF7{"
I compared this with the javascript implementation of strophe.js (which is correct) and this prints the following "EïSÓ*JÉHF7{¢"
Is there another way on how to calculate it? Or is it maybe some wrong encoding of the base string?
Thanks,
Michael

I;m using node v0.4.12 and i am getting:
EïSÓ*JÉHF7{¢
which version of node you are using?
maybe this is shell settings thing try execute unix command:
env
search for:
LANG=en_US.UTF-8

Have a look at this module, I've tested this and it's working.

Got it working now, my code was correct but I used some wrong variables for constructing the string..
Thanks for the help.

Related

JScript Escape an ampersand in payload data for URL

I am attempting to launch a view using the following JS function:
$('#filterTop').click(function () {
var filterValue = $('#filterValueTop').val();
refreshView(`#Url.Action(Model.Action, Model.Controller)?pageSize=#Model.PageSize&pageNumber=#Model.PageNumber&sortDesc=#Model.SortDescending&filterType=#Model.FilterType&filterValue=${filterValue}&showAll=#Model.ShowAll` + `#Model.Payload`, '#Model.ResultView');
});
It worked great until I needed to append a static payload to the end of the URL. The relevant part is line 3 at the end:
&showAll=#Model.ShowAll` + `#Model.Payload`
I am assigning #Model.Payload a value of:
opts.Payload = "&batchID=" + batchID;
or "&batchID=25". The resulting URL is:
https://localhost:44303/Employee/Repaginate?pageSize=20&pageNumber=1&sortDesc=True&filterType=Name&filterValue=Jes&showAll=False&batchID=25
For some reason, it's translating the "&" to "&a.m.p;" (with no periods) which isn't a valid URL. I've tried various methods of escaping the character like using "%26", "/&", and several other garden varieties but alas, my attempts have been in vain. Any suggestions on what I am doing wrong?

Converting match object to string in perl6

I was trying to convert a match object to a string in perl6. The method Str on a match object is defined as:
method Str(Match:D: --> Str:D)
I would think I could use Str($match) to accomplish this. And it seems to convert it to a string, but I'm getting an error using the string with the following code:
my $searchme = "rudolph";
my $match = $searchme ~~ /.*dol.*/;
say $match.WHAT;
my $test1 = Str($match);
say $test1.WHAT;
say $test1;
With the output:
(Match)
(Str)
With the error:
Cannot find method 'gist': no method cache and no .^find_method in
block at .code.tio line 6
However, if I run:
my $searchme = "rudolph";
my $match = $searchme ~~ /.*dol.*/;
say $match.WHAT;
my $test1 = $match.Str;
say $test1.WHAT;
say $test1;
I get no error and the result:
(Match)
(Str)
rudolph
Is this a bug or me misunderstanding how it works?
Thanks for reading.
I'm writing this up as an answer even though it's actually an incomplete discussion of a bug, so not at all normal SO fare. The alternative of lots of comments doesn't seem better.
It's a bug. Perhaps you just golfed this.
dd $test1; instead of say $test1; is helpful in that it displays BOOTStr $test1 = (BOOTStr without .perl method).
Based on that I searched the rakudo repo for BOOTStr and that led to the above issue.
Golfing it further leads to:
say $ = Str(Match.new);
Note that these are all fine:
say Str(Match.new);
say $ = Int(Match.new);
say $ = Str(Date.new: '2015-12-31');
It appears to be a combination of leaking some implementation details regarding how Rakudo/NQP/MoarVM bootstrap; Match being an NQP object; Str() on that being wonky; and assigning it to a Scalar container (the $ is an anonymous one) making that wonkiness visible.
I'll add more when/if I figure it out.

JSON.stringify() a string which is actual NodeJS code

I'm working with the Readline module in NodeJS and would like to parse the content of what the user wrote as code. Meaning if someone writes:
{
name: "David",
age: 34
}
I should be able to JSON.stringify(content) and get:
{
"name": "David",
"age": "34"
}
How can I convert a string in to actual code, so it can be interpreted as a JavaScript object, thus be converted in to JSON using JSON.stringify()?
It's not entirely clear what you're asking, but, would JSON.parse() help you here? You'll want to wrap it in a try catch in case the input is not valid JSON.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse
The trick to make this work is to use the VM module in a undocumented way as seen below.
let vm = require('vm');
let script = new vm.Script('x = ' + full_content);
let sandbox = script.runInThisContext();
converted = JSON.stringify(sandbox);
Basically you have to create a variable that will hold your string (JavaScript), which will be then converted in to proper JavaScript code thanks to .runInThisContext().
In this case the x variable will "disappear" and you don't have to think about that. But if you were to follow the NodeJS documentation example, and use .runInNewContext() then the x variable wouldn't go away, a you would have your object (in my case at least) assigned to the x variable.
Hope this helps :)

Convert string to buffer Node

I am using a library which on call of a function returns the toString of a buffer.
The exact code is
return Buffer.concat(stdOut).toString('utf-8');
But I don't want string version of it.
I just want the buffer
So how to convert string back to buffer.
Something like if
var bufStr = Buffer.concat(stdOut).toString('utf-8');
//convert bufStr back to only Buffer.concat(stdOut).
How to do this?
I tried doing
var buf = Buffer.from(bufStr, 'utf-8');
But it throws utf-8 is not a function.
When I do
var buf = Buffer.from(bufStr);
It throws TypeError : this is not a typed array.
Thanks
You can do:
var buf = Buffer.from(bufStr, 'utf8');
But this is a bit silly, so another suggestion would be to copy the minimal amount of code out of the called function to allow yourself access to the original buffer. This might be quite easy or fairly difficult depending on the details of that library.
You can use Buffer.from() to convert a string to buffer. More information on this can be found here
var buf = Buffer.from('some string', 'encoding');
for example
var buf = Buffer.from(bStr, 'utf-8');
Note: Just reposting John Zwinck's comment as answer.
One issue might be that you are using a older version of Node (for the moment, I cannot upgrade, codebase struck with v4.3.1). Simple solution here is, using the deprecated way:
new Buffer(bufferStr)
Note #2: This is for people struck in older version, for whom Buffer.from does not work
This is working for me, you might change your code like this
var responseData=x.toString();
to
var responseData=x.toString("binary");
and finally
response.write(new Buffer(toTransmit, "binary"));

ActionScript 3: Turning a String into and Operator?

I'm trying to create a simple calculator program in ActionScript for a school project, and I'm struggling to find a concise way to take an equation from an array such as this: "4","+","2"; and manipulate it so that the answer to the equation can be deduced. The problem is taking the String "+" from the equation array and turning it into a usable operator. At the moment, when an operator button is pressed on the calculator GUI it adds the operator to the equation array as a String (i.e. pressing the '÷' button will add "/" to the equation array).
I looked for an answer to this on Google and found something about a JavaScript function that I used in the code below (in lines 1 and 4), but all I end up with in the Output feed is "null", or "0" depending on whether I changed the variable 'answer' to a String, or a Number.
Here's the code I have so far:
import flash.external.ExternalInterface;
var equationArray:Array = new Array("4","+","2");
var answer:Number = ExternalInterface.call("eval",equationArray[0] + equationArray[1] + equationArray[2]);
trace(answer);
I'd prefer to use something like this rather than writing a long if statement to pick between different operators if that's possible. Thanks for the help!
I did some research and realized that your code doesn't work only in Chrome.
There is an error
SecurityError: Error #2060
at flash.external::ExternalInterface$/call()
at FlexTemp/preinitializeHandler()
at FlexTemp/___FlexTemp_Application1_preinitialize()
at flash.events::EventDispatcher/dispatchEvent()
at mx.core::UIComponent/dispatchEvent()
at mx.core::UIComponent/initialize()
at spark.components::Application/initialize()
at FlexTemp/initialize()
at mx.managers.systemClasses::ChildManager/childAdded()
at mx.managers.systemClasses::ChildManager/initializeTopLevelWindow()
at mx.managers::SystemManager/initializeTopLevelWindow()
at mx.managers::SystemManager/http://www.adobe.com/2006/flex/mx/internal::kickOff()
at mx.managers::SystemManager/http://www.adobe.com/2006/flex/mx/internal::preloader_completeHandler()
at flash.events::EventDispatcher/dispatchEvent()
at mx.preloaders::Preloader/timerHandler()
at flash.utils::Timer/tick()
but if I switch of PepperFlash\14.0.0.145\pepflashplayer.dll in chrome://plugins/
all works fine.
I think there are some problems with pepflashplayer.dll
In your flash file:
import flash.external.ExternalInterface;
import flash.text.TextField;
var equationArray:Array = new Array("4","-","8");
var answer:Number = ExternalInterface.call("myJsFunction",equationArray[0] , equationArray[1] , equationArray[2])
var tf:TextField = new TextField();
addChild(tf as TextField);
tf.text = answer.toString();
In the HTML page (into the ):
<script type="text/javascript">
function myJsFunction(arg1,arg2,arg3){
return eval(arg1+arg2+arg3);
}
</script>
Is this what you try to do?
Using eval like in your code I didn't get the correct answer.
When you run as here above on localhost/server this will give you the answer.

Resources