Photon crashing on pubnum publish (red light flashing) - pubnub

I have a program that compiles and flashes to the Photon without problem. It runs well until it publishes a string to PubNub. By changing the string being sent, I get different results.
The following examples are relevant:
This works:
String msg = String(tempc);
client = PubNub.publish(channel, msg);
This works:
String msg = String("24");
client = PubNub.publish(channel, msg);
This causes the photon to crash (flash red):
msg = "24.000:145:654"
client = PubNub.publish(channel, msg);
Can you advise why the introduction of delimiters (=,-,:) causes the photon to crash.
Many thanks.

I managed to solve the problem, but i'm not sure why the previous "msg" construct did not work (If anyone can shed light on this for me, I would greatly appreciate it).
Here is the msg construct that i used to solve the problem. It is now in an object form and quite easy to work with on the Javascript side.
char msgChar[150] = "";
String tempstring = "{\"tN\":\"" + oT.f2s(oT.Now()) + "\",\
\"tH\":\"" + oT.f2s(oT.Low()) + "\",\
\"tL\":\"" + oT.f2s(oT.High()) + "\",\
\"hN\":\"" + oH.f2s(oH.Now()) + "\",\
\"hH\":\"" + oH.f2s(oH.Low()) + "\",\
\"hL\":\"" + oH.f2s(oH.High()) + "\",\
\"lN\":\"" + oL.f2s(oL.Now()) + "\",\
\"lH\":\"" + oL.f2s(oL.Low()) + "\",\
\"lL\":\"" + oL.f2s(oL.High()) + "\"}";
tempstring.toCharArray (msgChar, 150);
Thank you to those of you who contributed to the solution.

Related

Input string was not in a correct format trace = Convert.ToDouble(tracedTemp) + trace

Good day,
Firsty, please understand I did not write this code, I am simply tring to fix an error as the orignal coder is no longer available.
Here is the code:
tracedRecordsTemp = GR.Cells[1].Text;
tracedTemp = GR.Cells[2].Text.Replace("R", "").Replace(",", ".").Replace(" ", "").Trim();
completeRecordsTemp = GR.Cells[3].Text;
completeTemp = GR.Cells[4].Text.Replace("R", "").Replace(",", ".").Replace(" ", "").Trim();
bvCountTemp = GR.Cells[5].Text;
bvPriceTemp = GR.Cells[6].Text.Replace("R", "").Replace(",", ".").Replace(" ", "").Trim();
totalTemp = GR.Cells[7].Text.Replace("R", "").Replace(",", ".").Replace(" ", "").Trim();
tracedRecords = Convert.ToInt16(tracedRecordsTemp) + tracedRecords;
**trace = Convert.ToDouble(tracedTemp) + trace;**
completeRecords = Convert.ToInt16(completeRecordsTemp) + completeRecords;
complete = Convert.ToDouble(completeTemp) + complete;
bvCount = Convert.ToInt16(bvCountTemp) + bvCount;
bvPrice = Convert.ToDouble(bvPriceTemp) + bvPrice;
total = Convert.ToDouble(totalTemp) + total;
The part in bold is causing the error. Could someone please advise on how to rectify this?
Thanking you.

DIGEST-MD5 implementation in NodeJS

I'm trying to approach a local XMPP server (Openfire) with a NodeJS application.
I would like to use the DIGEST-MD5 mechanism (I am aware that it has been declared obsolete).
I found this article explaining how the mechanism works:
https://wiki.xmpp.org/web/SASL_and_DIGEST-MD5
However, when implementing the mechanism as described in the article, my calculated response is incorrect.
I have done my best to find out what I'm doing wrong, but I can't seem to figure it out.
I am certain that the rest of my stanza is correct, it's just the response that isn't right.
Here is my code for calculating the response:
var x = username + ':' + realm + ':' + pswd;
var y = crypto.createHash('md5').update(x).digest();
var a1 = y + ':' + nonce + ':' + cnonce + ':' + authzid;
var a2 = 'AUTHENTICATE:' + digestUri;
var ha1 = crypto.createHash('md5').update(a1).digest("hex");
var ha2 = crypto.createHash('md5').update(a2).digest("hex");
var kd = ha1 + ':' + nonce + ':00000001:' + cnonce + ':auth:' + ha2;
var z = crypto.createHash('md5').update(kd).digest("hex");
Where z is the final response.
As you can see I am making use of the crypto library for my hashing.
The example mentioned in the article above is a follows:
username="rob",realm="cataclysm.cx",nonce="OA6MG9tEQGm2hh",cnonce="OA6MHXh6VqTrRk",nc=00000001,qop=auth,digesturi="xmpp/cataclysm.cx",response=d388dad90d4bbd760a152321f2143af7,charset=utf-8,authzid="rob#cataclysm.cx/myResource"
When I plug all these values into my own implementation (with the password being 'secret'), my calculated response is:
5093acf6b3bc5687231539507cc2fb20
instead of the expected d388dad90d4bbd760a152321f2143af7.
Other examples don't give me the right result either.
So, what on earth am I doing wrong?
Any and all help would be greatly appreciated!
When calculating the response, the third line concatenates the buffer y (which contains an MD5 hash) with strings, whereby y is implicitly converted to a string using UTF-8.
However, an arbitrary byte sequence, such as a hash, will be corrupted by a UTF8 encoding, s. here. To prevent this, the individual parts should be concatenated as buffers rather than strings.
In contrast, the remaining concatenations (in this and the other lines) are not critical, since they are true strings:
var crypto = require('crypto');
var charset = 'utf-8';
var username = 'chris';
var realm = 'elwood.innosoft.com';
var nonce = 'OA6MG9tEQGm2hh';
var nc = '00000001';
var cnonce = 'OA6MHXh6VqTrRk';
var digestUri = 'imap/elwood.innosoft.com';
var response = 'd388dad90d4bbd760a152321f2143af7';
var qop = 'auth'
var pswd = 'secret';
var x = username + ':' + realm + ':' + pswd;
var y = crypto.createHash('md5').update(x).digest();
var a1 = Buffer.concat([y, Buffer.from(':' + nonce + ':' + cnonce, 'utf8')]); // + ':' + authzid; // Concat buffers instead of strings
var a2 = 'AUTHENTICATE:' + digestUri;
var ha1 = crypto.createHash('md5').update(a1).digest('hex');
var ha2 = crypto.createHash('md5').update(a2).digest('hex');
var kd = ha1 + ':' + nonce + ':00000001:' + cnonce + ':auth:' + ha2;
var z = crypto.createHash('md5').update(kd).digest('hex');
console.log(z); // d388dad90d4bbd760a152321f2143af7
Note that the sample data in the code snippet is not from the website linked in the question, but from RFC2831 (Using Digest Authentication as a SASL Mechanism), chapter 4:
charset=utf-8,username="chris",realm="elwood.innosoft.com",nonce="OA6MG9tEQGm2hh",nc=00000001,cnonce="OA6MHXh6VqTrRk",digest-uri="imap/elwood.innosoft.com",response=d388dad90d4bbd760a152321f2143af7,qop=auth
The code returns the result specified in response, which evidences the correctness of the calculated result.
I don't think that the example of the website linked in the question is consistent. The expected result is identical to that from the RFC example, although some of the relevant input data for the digests is different.
A coincidental match is very unlikely (s. collision probability for MD5), so with high probability the input data and expected result of the linked website do not belong together. In addition, the password is not specified there.
The algorithm applied is described in detail in chapter 2.1.2.1 Response-value. Note that in the RFC example authzid is not specified in contrast to the example off the linked website. According to the description in chapter 2.1.2.1, in this case ':' + authzid is simply to be omitted.
As already mentioned in the question, MD5 and thus the algorithm used here is deprecated (s. also RFC6331).

fs.readFileSync adding + signs?

I am working on an HTTP fileserver and stumbled upon a problem with fs.readFileSync(). However, the function adds + signs which prohibits me from writing the value to another value.
(This is just to show what happens and does not represent the full functionality of the intended code)
samplejs.js
var content = fs.readFileSync("D:/sampleproj/example.txt", {encoding:"utf8"});
console.log(content);
output:
"\t'#0000CC',\r" +
"\n\t'#0000FF',\r" +
"\n\t'#0033CC',\r" +
"\n\t'#0033FF',\r" +
"\n\t'#0066CC',\r" +
"\n\t'#0066FF',\r" +
"\n\t'#0099CC',\r" +
"\n\t'#0099FF',\r" +
"\n\t'#00CC00',\r" +
"\n\t'#00CC33',\r" +
"\n\t'#00CC66',\r" +
"\n\t'#00CC99',\r" +
"\n\t'#00CCCC',\r" +
"\n\t'#00CCFF',\r" +
"\n\t'#3300CC',\r" +
"\n\t'#3300FF',\r" +
"\n\t'#3333CC',\r" +
"\n\t'#3333FF',\r" +
"\n\t'#3366CC',\r" +
"\n\t'#3366FF',\r" +
"\n\t'#3399CC',\r" +
"\n\t'#3399FF',\r" +
"\n\t'#33CC00',\r" +
"\n\t'#33CC33',\r" +
"\n\t'#33CC66',\r" +
"\n\t'#33CC99',\r" +
"\n\t'#33CCCC',\r" +
"\n\t'#33CCFF',\r" +
"\n\t'#6600CC',\r" +
"\n\t'#6600FF',\r" +
"\n\t'#6633CC',\r" +
"\n\t'#6633FF',\r" +
"\n\t'#66CC00',\r" +
The problem here is, of course, the + signs. How can these be "avoided"/removed?
I've tried to .split("+") but that doesn't work since the + signs aren't inside a pair of quotes.
the text file:
'#0000CC',
'#0000FF',
'#0033CC',
'#0033FF',
'#0066CC',
'#0066FF',
'#0099CC',
'#0099FF',
'#00CC00',
'#00CC33',
'#00CC66',
'#00CC99',
'#00CCCC',
'#00CCFF',
'#3300CC',
'#3300FF',
'#3333CC',
'#3333FF',
'#3366CC',
'#3366FF',
'#3399CC',
'#3399FF',
'#33CC00',
'#33CC33',
'#33CC66',
'#33CC99',
'#33CCCC',
'#33CCFF',
'#6600CC',
'#6600FF',
'#6633CC',
'#6633FF',
'#66CC00',
'#66CC33',
'#9900CC',
'#9900FF',
'#9933CC',
'#9933FF',
'#99CC00',
'#99CC33',
'#CC0000',
'#CC0033',
'#CC0066',
'#CC0099',
'#CC00CC',
Are you sure you are doing console.log?
I did console.log and didn't get any "+", but I got those when I use console.dir.
Anyway, these "+", are not part of the string and you can't remove them by using split.
And just use the string as if they were not there.
To get each line simply use:
const fs = require("fs");
let file = fs.readFileSync(__dirname + "/text.txt", { encoding: "utf8" });
const lines = file.split("\n");

Groovy script overwriting first byte?

Hi so this string I'm creating and appending in groovy is somehow corrupting the first byte and I have no idea why this happening. Its the second string creation. In this script I'm making a query and the first one works but the second initialization somehow messes up the first byte in the string and I have to do a substring of an extra index (it's two because I'm initializing a comma). Any insight would be very appreciated!!
Note: I'm using mulesoft runtime 3.8.5 in Anypoint studio 6.4.4. Not sure if this is the reason but it is a candidate in my mind...
flowVars.queryIds = "Id IN ("
for (Integer i = 0; i < payload.size(); i++) {
flowVars.queryIds += "\'" + payload[i].Id + "\',"
}
flowVars.queryIds = flowVars.queryIds.substring(0,flowVars.queryIds.size() - 1) + ")"
//Assigning comma because a random byte is getting inserted and this makes that error explicit & deterministic
flowVars.queryFields = ",";
for (String key : payload[0].keySet()) {
flowVars.queryFields += key + ",";
}
//Skipping over custom field isMatch
flowVars.queryFields = flowVars.queryFields.substring(2, flowVars.queryFields.size() - 9);
return payload
I can't reproduce your problem but since you use groovy, you can write your code a bit shorter:
flowVars.queryIds = "Id IN ("
flowVars.queryIds += payload.collect{"'${it.Id}'"}.join(", ")
flowVars.queryIds += ")"
flowVars.queryFields = payload[0].keySet().join(", ");
this should produce the same output in a more understandable way
So I found out the reason this issue was happening is actually because the csv file I am parsing is corrupted (I thought it was mulesoft and was mistaken). This blog does a greater job explaining the issue than I could. Thanks for your review on the groovy code though Rdmueller! Is definitely a lot cleaner with your suggestions. https://medium.freecodecamp.org/a-quick-tale-about-feff-the-invisible-character-cd25cd4630e7

Function doesn't execute inside loop

I'm making a simple terminal calculator but for some reason a function isn't executing inside a while loop but executes outside the loop.
Given this input: ((1 + 2) + (3 + 4))
It should output:10
But gets stuck in an infinite loop because it doesn't replace the innermost expressions with their result.
The function that doesn't execute is s.replace(basicOp, answer);
Here is a snippet of the problem:
public static function processInput(s:String):String
{
var result:Null<Float> = parseNumber(s);
if (result != null)
{
return Std.string(result);
}
var closeParPos:Int = 0;
var openParPos:Int = 0;
var basicOp:String;
var answer:String = "";
// ERROR HERE
while (Std.string(answer) != s)
{
closeParPos = s.indexOf(")");
openParPos = s.lastIndexOf("(", closeParPos);
basicOp = s.substring(openParPos, closeParPos + 1);
answer = processBasicOp(basicOp);
// This isn't executed
s.replace(basicOp, answer);
trace("Input: " + s + " basicOp: " + basicOp + " Answer: " + answer);
}
return (result == null)? "": Std.string(result);
}
All the code is here just run make test
The input syntax is: ([number] [operator] [number]) or ([operator] [number])
There must be a single space between number and operators.
There shouldn't be any space between numbers and parenthesis
Supported operations:
+ - / *
% (remainder),
div (quotient),
sqr (square),
sqroot (square root),
sin cos tan (in degrees, bugged)
fact (factorial)
It isn't completed yet, there may be other problems, but this problem prevents me from advancing.
Can someone help me find the solution?
Thank you.
I can't actually get this to run, but StringTools.replace() doesn't modify the string in-place.
Try changing s.replace(basicOp, answer); to s = s.replace(basicOp, answer);

Resources