I write some test codes.
var strChr = String.fromCharCode; var msg = "1234567890"; for(var lp=0; lp<8;lp++){ // mask var mask = []; var masked1 = ''; for (var ix = 0; ix < 4; ix++){ var rnd = Math.floor( Math.random() * 255 ); mask[ix] = rnd; masked1 += strChr(rnd); } // masked1: for (var ix = 0; ix < msg.length; ix++){ masked1 += strChr(msg.charCodeAt(ix) ^ mask[ix & 3]); } // masked1 char code var masked1code = []; for(var i = 0;i<masked1.length; i++){ masked1code[i] = masked1.charCodeAt(i); } // masked2 var masked2 = new Uint32Array(1+((msg.length+3)>>2)); var m8 = new Uint8Array(masked2.buffer, 0, (4 + msg.length)); m8.set(mask,0); m8.set(msg,4); var m = masked2[0]; for (var i=1;i<masked2.length;i++) { masked2[i]^=m; } console.log("----------------------lp(" + lp + ")--------------------"); console.log(mask); console.log(masked1code); console.log(masked2); console.log(m8); }
result:
>----------------------lp(0)-------------------- [ 244, 247, 131, 224 ] [ 244, 247, 131, 224, 197, 197, 176, 212, 193, 193, 180, 216, 205, 199 ] new Uint32Array([3766745076, 3568354757, 3635724737, 3766732749]) new Uint8Array([244, 247, 131, 224, 197, 197, 176, 212, 193, 193, 180, 216, 205, 199]) ----------------------lp(1)-------------------- [ 203, 75, 141, 225 ] [ 203, 75, 141, 225, 250, 121, 190, 213, 254, 125, 186, 217, 242, 123 ] new Uint32Array([3784133579, 3586030074, 3652877822, 3784145906]) new Uint8Array([203, 75, 141, 225, 250, 121, 190, 213, 254, 125, 186, 217, 242, 123]) ----------------------lp(2)-------------------- [ 172, 35, 218, 166 ] [ 172, 35, 218, 166, 157, 17, 233, 146, 153, 21, 237, 158, 149, 19 ] new Uint32Array([2799313836, 2464747933, 2666337689, 2799309717]) new Uint8Array([172, 35, 218, 166, 157, 17, 233, 146, 153, 21, 237, 158, 149, 19]) ----------------------lp(3)-------------------- [ 231, 205, 151, 99 ] [ 231, 205, 151, 99, 214, 255, 164, 87, 210, 251, 160, 91, 222, 253 ] new Uint32Array([1670893031, 1470431190, 1537276882, 1670905310]) new Uint8Array([231, 205, 151, 99, 214, 255, 164, 87, 210, 251, 160, 91, 222, 253]) ----------------------lp(4)-------------------- [ 121, 220, 55, 245 ] [ 121, 220, 55, 245, 72, 238, 4, 193, 76, 234, 0, 205, 64, 236 ] new Uint32Array([4114078841, 3238325832, 3439389260, 4114082880]) new Uint8Array([121, 220, 55, 245, 72, 238, 4, 193, 76, 234, 0, 205, 64, 236]) ----------------------lp(5)-------------------- [ 165, 47, 134, 99 ] [ 165, 47, 134, 99, 148, 29, 181, 87, 144, 25, 177, 91, 156, 31 ] new Uint32Array([1669738405, 1471487380, 1538333072, 1669734300]) new Uint8Array([165, 47, 134, 99, 148, 29, 181, 87, 144, 25, 177, 91, 156, 31]) ----------------------lp(6)-------------------- [ 9, 142, 128, 143 ] [ 9, 142, 128, 143, 56, 188, 179, 187, 60, 184, 183, 183, 48, 190 ] new Uint32Array([2407566857, 3149118520, 3082270780, 2407579184]) new Uint8Array([9, 142, 128, 143, 56, 188, 179, 187, 60, 184, 183, 183, 48, 190]) ----------------------lp(7)-------------------- [ 217, 225, 170, 12 ] [ 217, 225, 170, 12, 232, 211, 153, 56, 236, 215, 157, 52, 224, 209 ] new Uint32Array([212525529, 949605352, 882759660, 212521440]) new Uint8Array([217, 225, 170, 12, 232, 211, 153, 56, 236, 215, 157, 52, 224, 209]) =undefined
It seems that your codes works well.
@Aifer started
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
I write some test codes.
result:
It seems that your codes works well.