I'm spinning my wheels here trying to find the C# equivalent of this NodeJS code. This has been extracted from a much bigger authentication function that has been migrated into C#. I'm unsure what data type Buffer.from produces and tried to replicate the output by using Encoding.UTF8.GetBytes but as I wrote the bytes in the console, I ended up with a different output.
I just need the same final output from out2 to get produced in my equivalent c# function for it to finally work. Any help would be appreciated!!
const bitwise = require('bitwise');
testkey = "gEracj9I248GF3yS";
timestamp = "1649302201249";
testid = "8621349";
var testkeyBuffer = new Buffer.from(testkey);
// OUTPUT : 67 45 72 61 63 6a 39 49 32 34 38 47 46 33 79 53
var testkeyArrByte = Uint8Array.from(testkeyBuffer)
// OUTPUT : 103 69 114 97 99 106 57 73 50 52 56 71 70 51 121 83
var timestampBuffer = new Buffer.from(timestamp);
// OUTPUT : 31 36 34 39 33 30 32 32 30 31 32 34 39
var testidBuffer = new Buffer.from(testid);
out1 = bitwise.buffer.or(testkeyBuffer,timestampBuffer);
// OUTPUT : wwvysz;{25:w⌂3yS
out2 = bitwise.buffer.xor(out1,testidBuffer);
// OUTPUT : OADH@N☻{25:w⌂3yS
CodePudding user response:
This looks like it just applies a bitwise operator on two lists, element by element. If one list is longer, then nothing happens to the remaining elements in the other list.
This might be a helper method like
public IEnumerable<byte> OpOrNop(List<byte> a, List<byte> b, Func<byte, byte, byte> func)
{
var longer = a;
if (b.Count > a.Count)
{
longer = b;
}
for (int i=0; i<a.Count; i )
{
if (i < b.Count)
{
yield return func(a[i], b[i]);
}
else
{
yield return a[i];
}
}
}
so
string testkey = "gEracj9I248GF3yS";
string timestamp = "1649302201249";
string testid = "8621349";
var testkeyBuffer = System.Text.Encoding.ASCII.GetBytes(testkey);
var timestampBuffer = System.Text.Encoding.ASCII.GetBytes(timestamp);
var testidBuffer = System.Text.Encoding.ASCII.GetBytes(testid);
var out1 = OpOrNop(testkeyBuffer.ToList(), timestampBuffer.ToList(), (a, b) => (byte)(a | b));
var out2 = OpOrNop(out1.ToList(), testidBuffer.ToList(), (a, b) => (byte)(a ^ b));
System.Text.Encoding.ASCII.GetString(out2.ToArray())
System.Text.Encoding.ASCII.GetString(out1.ToArray())
console output
"wwvysz;{25:w\u007f3yS"
"OADH@N\u0002{25:w\u007f3yS"