Word Array to String - javascript

how to do this in Javascript or Jquery?
Please suggest in 2 steps:
1.- Word Array to Single Byte Array.
2.- Byte Array to String.
Maybe this can help:
function hex2a(hex) {
var str = '';
for (var i = 0; i < hex.length; i += 2)
str += String.fromCharCode(parseInt(hex.substr(i, 2), 16));
return str;
}

What you are trying to achieve is already implemented in CryptoJS. From the documentation:
You can convert a WordArray object to other formats by explicitly calling the toString method and passing an encoder.
var hash = CryptoJS.SHA256("Message");
alert(hash.toString(CryptoJS.enc.Base64));
alert(hash.toString(CryptoJS.enc.Hex));
Honestly I have no idea why you want to implement that yourself... But if you absolutely need to do it "manually" in the 2 steps you mentioned, you could try something like this:
function wordToByteArray(wordArray) {
var byteArray = [], word, i, j;
for (i = 0; i < wordArray.length; ++i) {
word = wordArray[i];
for (j = 3; j >= 0; --j) {
byteArray.push((word >> 8 * j) & 0xFF);
}
}
return byteArray;
}
function byteArrayToString(byteArray) {
var str = "", i;
for (i = 0; i < byteArray.length; ++i) {
str += escape(String.fromCharCode(byteArray[i]));
}
return str;
}
var hash = CryptoJS.SHA256("Message");
var byteArray = wordToByteArray(hash.words);
alert(byteArrayToString(byteArray));
The wordToByteArray function should work perfectly, but be aware that byteArrayToString will produce weird results in almost any case. I don't know much about encodings, but ASCII only uses 7 bits so you won't get ASCII chars when trying to encode an entire byte. So I added the escape function to at least be able to display all those strange chars you might get. ;)
I'd recommend you use the functions CryptoJS has already implemented or just use the byte array (without converting it to string) for your analysis.

Related

decode numbers in an array and compare to different array

I have 2 arrays, one containing numbers and another one containing encoded numbers.
I need to match the encoded numbers to the normal numbers to find a match. The trick is that I need to decode the numbers to find a match.
let encoded = ['310254#3543129', '03254#03715P5'];
let numbers = ['3102540003543129', '0325400003715445', ...]
I have tried using a for loop with recursion for when there are multiple special characters in the string, but I am not getting the results correctly.
for (let i = 0; i < encrypted.length; i++) {
for (let j = 0; j < numbers.length; j++) {
function removeSpecialChars(referenceNumber) {
if(!/^\d*$/.test(referenceNumber)){
for (let x = 0, invalidChar = ''; invalidChar = referenceNumber.charAt(x); x++) {
let newString = '';
// which char is invalid
if (!/^\d*$/.test(invalidChar)) {
for(let y = 0; i < numbers[j].split('').length; y++){
if(numbers[j].split('')[x] === numbers[j].split('')[x + y]){
newString += numbers[j].split('')[x];
} else break;
}
return removeSpecialChars(referenceNumber.replace(invalidChar, newString));
}
}
}
return referenceNumber;
} // end function
if (removeSpecialChars(encrypted[i]) === numbers[j]) {
count++;
}
}
}
I am not sure if I am approaching this in the wrong way entirely or if I am just missing something.
Here is a fiddle to reproduce my results.
https://jsfiddle.net/6ezuwhdb/1/
it seems that there are at least 2 rules at play here (I haven't looked at the fiddle in any detail but what there is here can be easily extended):
000 is encoded with a #, and
44 is encoded with a P
I would first get a complete list of these rules together and then use regular expressions to replace all instances of the special characters with their numeric equivalents.
The regular expressions use the g modifier so that all instances of the encoded value get replaced within the string.
const encoded = ['310254#3543129', '03254#03715P5'];
const decoded = encoded.map(en => {
// replace the special characters using multiple regular expressions
let result = en;
// swap all #'s for
result = result.replace(/#/g, '000');
// swap all P's for 44
result = result.replace(/P/g, '44');
// add more rules here ...
return result;
});
console.log('Decoded numbers: ', decoded);
To then check whether the normal numbers can be found within the decoded set of numbers is fairly straight forward.
// From previous step ;o)
const decoded = [
"3102540003543129",
"0325400003715445"
];
const numbers = ['3102540003543129', '0325400003715445'];
const matchedNumbers = numbers.filter(number => {
return decoded.indexOf(number) >= 0;
});
console.log(matchedNumbers);

Longitudinal redundancy check in Javascript

I'm working with a system that integrates a Point of Sell (POS) device, I use chrome serial to scan ports and be able to read credit card data.
The problem I'm facing is that I need to concat the LRC from a string in this format:
STX = '\002' (2 HEX) (Start of text)
LLL = Length of data (doesn't include STX or ETX but command).
Command C50 {C = A message from PC to POS, 50 the actual code that "prints" a message on POS}
ETX = '\003' (3 HEX) (End of text)
LRC = Longitudinal Redundancy Check
A message example would be as follows:
'\002014C50HELLO WORLD\003'
Here we can see 002 as STX, 014 is the length from C50 to D, and 003 as ETX.
I found some algorithms in C# like this one or this one and even this one in Java, I even saw this question that was removed from SO on Google's cache, which actually asks the same as I but had no examples or answers.
I also made this Java algorithm:
private int calculateLRC(String str) {
int result = 0;
for (int i = 0; i < str.length(); i++) {
String char1 = str.substring(i, i + 1);
char[] char2 = char1.toCharArray();
int number = char2[0];
result = result ^ number;
}
return result;
}
and tried passing it to Javascript (where I have poor knowledge)
function calculateLRC2(str) {
var result = 0;
for (var i = 0; i < str.length; i++) {
var char1 = str.substring(i, i + 1);
//var char2[] = char1.join('');
var number = char1;
result = result ^ number;
}
return result.toString();
}
and after following the Wikipedia's pseudocode I tried doing this:
function calculateLRC(str) {
var buffer = convertStringToArrayBuffer(str);
var lrc;
for (var i = 0; i < str.length; i++) {
lrc = (lrc + buffer[i]) & 0xFF;
}
lrc = ((lrc ^ 0xFF) + 1) & 0xFF;
return lrc;
}
This is how I call the above method:
var finalMessage = '\002014C50HELLO WORLD\003'
var lrc = calculateLRC(finalMessage);
console.log('lrc: ' + lrc);
finalMessage = finalMessage.concat(lrc);
console.log('finalMessage: ' + finalMessage);
However after trying all these methods, I still can't send a message to POS correctly. I have 3 days now trying to fix this thing and can't do anything more unless I finish it.
Is there anyone that knows another way to calculate LRC or what am I doing wrong here? I need it to be with Javascritpt since POS comunicates with PC through NodeJS.
Oh btw the code from convertStringToArrayBuffer is on the chrome serial documentation which is this one:
var writeSerial=function(str) {
chrome.serial.send(connectionId, convertStringToArrayBuffer(str), onSend);
}
// Convert string to ArrayBuffer
var convertStringToArrayBuffer=function(str) {
var buf=new ArrayBuffer(str.length);
var bufView=new Uint8Array(buf);
for (var i=0; i<str.length; i++) {
bufView[i]=str.charCodeAt(i);
}
return buf;
}
Edit After testing I came with this algorithm which returns a 'z' (lower case) with the following input: \002007C50HOLA\003.
function calculateLRC (str) {
var bytes = [];
var lrc = 0;
for (var i = 0; i < str.length; i++) {
bytes.push(str.charCodeAt(i));
}
for (var i = 0; i < str.length; i++) {
lrc ^= bytes[i];
console.log('lrc: ' + lrc);
//console.log('lrcString: ' + String.fromCharCode(lrc));
}
console.log('bytes: ' + bytes);
return String.fromCharCode(lrc);
}
However with some longer inputs and specialy when trying to read card data, LRC becomes sometimes a Control Character which in my case that I use them on my String, might be a problem. Is there a way to force LRC to avoid those characters? Or maybe I'm doing it wrong and that's why I'm having those characters as output.
I solved LRC issue by calculating it with the following method, after reading #Jack A.'s answer and modifying it to this one:
function calculateLRC (str) {
var bytes = [];
var lrc = 0;
for (var i = 0; i < str.length; i++) {
bytes.push(str.charCodeAt(i));
}
for (var i = 0; i < str.length; i++) {
lrc ^= bytes[i];
}
return String.fromCharCode(lrc);
}
Explanation of what it does:
1st: it converts the string received to it's ASCII equivalent (charCodeAt()).
2nd: it calculates LRC by doing a XOR operation between last calculated LRC (0 on 1st iteration) and string's ASCII for each char.
3rd: it converts from ASCII to it's equivalent chat (fromCharCode()) and returns this char to main function (or whatever function called it).
Your pseudocode-based algorithm is using addition. For the XOR version, try this:
function calculateLRC(str) {
var buffer = convertStringToArrayBuffer(str);
var lrc = 0;
for (var i = 0; i < str.length; i++) {
lrc = (lrc ^ buffer[i]) & 0xFF;
}
return lrc;
}
I think your original attempt at the XOR version was failing because you needed to get the character code. The number variable still contained a string when you did result = result ^ number, so the results were probably not what you expected.
This is a SWAG since I don't have Node.JS installed at the moment so I can't verify it will work.
Another thing I would be concerned about is character encoding. JavaScript uses UTF-16 for text, so converting any non-ASCII characters to 8-bit bytes may give unexpected results.

Javascript breaking up string and reverse it

I am trying to figure out how to break up a sting to groups of five and reverse each one individually. I want it to work for any string, (there is no delimiter for splitting)
For example, if the variable is:
Iwanttobreakthisintogroupsoffiveandreverse
I would want it to return:
tnawI erbot ihtka otnis puorg iffos dnaev rever es
How do I go about this?
var str = "Iwanttobreakthisintogroupsoffiveandreverse"
var result = [];
str.replace(/.{1,5}/g, function(m) {
result.push(m.split('').reverse().join(''));
});
result.join(' ');
// "tnawI erbot ihtka otnis puorg iffos dnaev rever es"
var input="Iwanttobreakthisintogroupsoffiveandreverse";
var matches = input.match(/.{1,5}/g);
for (i = 0; i < matches.length; ++i) {
matches[i] = matches[i].split("").reverse().join("");
}
alert(matches);
It pops up tnawI,erbot,ihtka,otnis,puorg,iffos,dnaev,rever,es
You could try this:
var chars = "Iwanttobreakthisintogroupsoffiveandreverse".split('')
var str_rev = []
for (i = 0; i < chars.length; i += 5)
str_rev.push( chars.slice(i, i + 5).reverse().join('') )
Convert to char array using split(''). This allows you to use array methods like reverse and slice
Loop through the char array taking 5 element slices
reverse the elements, join the chars to create a string, and add it to rev_str

Javascript array from string of numbers

I am trying to work with arrays in javascript. Consider the following code:
var visList = '1234,5678,9'
var visListArray = new Array(visList);
for (i = 0; i <= visListArray.length - 1; i++)
{
alert(visListArray[i]);
}
Why doesn't this split the array into individual numbers instead of all of them clumped together?
Any help would be really appreciated.
Many thanks
Create the array by calling split() on the string:
var visList = '1234,5678,9'
var visListArray = visList.split(",");
You cannot substitue a string that looks like code for actual code. While this would work:
var visListArray = new Array(1234,5678,9);
Yours doesn't because the string is not interpreted by the Array constructor as 3 comma separated arguments, it is interpreted as one string.
Edit: Note that calling split() on a string results in an Array of strings. If you want an Array of numbers, you'll need to iterate the Array converting each string to a number. One convenient way to do that is to use the map() method:
visListArray = visList.split(",").map(function (item) {
return +item;
});
See the compatibility note for using map() in older browsers.
because its an string, try this:
var visList = '1234,5678,9'
var visListArray = [].concat(visList.split(','));
for (i = 0; i <= visListArray.length - 1; i++) {
alert(visListArray[i]);
}
You have to use string.split
var visList = '1234,5678,9'
var visListArray = visList.split(",");
for (i = 0; i <= visListArray.length - 1; i++)
{
alert(visListArray[i]);
}
To convert a symbol-separated list into an array, you may use split(symbol):
var list = "1221,2323,4554,7667".split(",");
for (var i = 0, il = list.length; i < il; i++) {
alert( +list[i] ); // a casting from string to number
}

How do I implement hex2bin()?

I need to communicate between Javascript and PHP (I use jQuery for AJAX), but the output of the PHP script may contain binary data. That's why I use bin2hex() and json_encode() on PHP side.
How do I convert the hexadecimal string in binary string, with JavaScript?
To answer your question:
function Hex2Bin(n){if(!checkHex(n))return 0;return parseInt(n,16).toString(2)}
Here are some further functions you may find useful for working with binary data:
//Useful Functions
function checkBin(n){return/^[01]{1,64}$/.test(n)}
function checkDec(n){return/^[0-9]{1,64}$/.test(n)}
function checkHex(n){return/^[0-9A-Fa-f]{1,64}$/.test(n)}
function pad(s,z){s=""+s;return s.length<z?pad("0"+s,z):s}
function unpad(s){s=""+s;return s.replace(/^0+/,'')}
//Decimal operations
function Dec2Bin(n){if(!checkDec(n)||n<0)return 0;return n.toString(2)}
function Dec2Hex(n){if(!checkDec(n)||n<0)return 0;return n.toString(16)}
//Binary Operations
function Bin2Dec(n){if(!checkBin(n))return 0;return parseInt(n,2).toString(10)}
function Bin2Hex(n){if(!checkBin(n))return 0;return parseInt(n,2).toString(16)}
//Hexadecimal Operations
function Hex2Bin(n){if(!checkHex(n))return 0;return parseInt(n,16).toString(2)}
function Hex2Dec(n){if(!checkHex(n))return 0;return parseInt(n,16).toString(10)}
JavaScript doesn't have support for binary data. Nevertheless you can emulate this with regular strings.
var hex = "375771", // ASCII HEX: 37="7", 57="W", 71="q"
bytes = [],
str;
for(var i=0; i< hex.length-1; i+=2){
bytes.push(parseInt(hex.substr(i, 2), 16));
}
str = String.fromCharCode.apply(String, bytes);
alert(str); // 7Wq
function hex2bin(hex)
{
var bytes = [], str;
for(var i=0; i< hex.length-1; i+=2)
bytes.push(parseInt(hex.substr(i, 2), 16));
return String.fromCharCode.apply(String, bytes);
}
thanks to Andris!
Other useful information about this topic (dex2bin,bin2dec) can be found here.
According to that, here is a bin2hex solution:
parseInt(1100,2).toString(16); //--> c
Although not an answer to the actual question, it is perhaps useful in this case to also know how to reverse the process:
function bin2hex (bin)
{
var i = 0, l = bin.length, chr, hex = ''
for (i; i < l; ++i)
{
chr = bin.charCodeAt(i).toString(16)
hex += chr.length < 2 ? '0' + chr : chr
}
return hex
}
As an example, using hex2bin on b637eb9146e84cb79f6d981ac9463de1 returns ¶7ëFèL·mÉF=á, and then passing this to bin2hex returns b637eb9146e84cb79f6d981ac9463de1.
It might also be useful to prototype these functions to the String object:
String.prototype.hex2bin = function ()
{
var i = 0, l = this.length - 1, bytes = []
for (i; i < l; i += 2)
{
bytes.push(parseInt(this.substr(i, 2), 16))
}
return String.fromCharCode.apply(String, bytes)
}
String.prototype.bin2hex = function ()
{
var i = 0, l = this.length, chr, hex = ''
for (i; i < l; ++i)
{
chr = this.charCodeAt(i).toString(16)
hex += chr.length < 2 ? '0' + chr : chr
}
return hex
}
alert('b637eb9146e84cb79f6d981ac9463de1'.hex2bin().bin2hex())
All proposed solutions use String.fromCharCode, why not simply using unescape?
String.prototype.hex2bin = function()
{
var i = 0, len = this.length, result = "";
//Converting the hex string into an escaped string, so if the hex string is "a2b320", it will become "%a2%b3%20"
for(; i < len; i+=2)
result += '%' + this.substr(i, 2);
return unescape(result);
}
and then:
alert( "68656c6c6f".hex2bin() ); //shows "hello"
With reference to node.js ( not in browser ).
Basically it's all over-engineered and does not work well.
responses are out of alignment and though text-wise they are the same bit wise everything is all over the place :
curl http://phpimpl.domain.com/testhex.php | xxd
00000000: de56 a735 4739 c01d f2dc e14b ba30 8af0 .Q.%G9.....;.0..
curl http://nodejs.domain.com/ | xxd
00000000: c39e 56c2 a725 4739 c380 c3ad c3b1 c39c ..Q..%G9........
00000010: c3a1 37c2 6b30 c28f c3b0 ..;..0....
The proper way to implement this in node is :
function hex2bin(hex){
return new Buffer(hex,"hex");
}
curl http://nodejs.domain.com/ | xxd
00000000: de56 a735 4739 c01d f2dc e14b ba30 8af0 .Q.%G9.....;.0..
Hope this helps.
Here is an implementation of hex2bin in JS that takes a string and returns Uint8Array, works both in browsers and nodejs,
function hex2bin(hex) {
var length = hex.length / 2;
var result = new Uint8Array(length);
for (var i = 0; i < length; ++i) {
result[i] = parseInt(hex.slice(i * 2, i * 2 + 2), 16);
}
return result;
}
And its inverse,
function bin2hex(bin) {
return Array.from(bin).map(function (x) {
return x.toString(16).padStart(2, '0');
}).join('');
}
If someone needs the other direction (bin to hex), here is it:
function bin2hex(bin) {
return new Buffer(bin).toString("hex");
}
JavaScript does actually contain support for binary data. See Uint8Array.
Just read each byte from the array and convert it into hexadecimal.

Categories

Resources