From d861db044888cffc38feb0a1c5c88f23d63455cc Mon Sep 17 00:00:00 2001 From: cheetah Date: Sat, 15 May 2021 17:36:21 +0000 Subject: [PATCH] test --- index.js | 130 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 index.js diff --git a/index.js b/index.js new file mode 100644 index 0000000..e228f14 --- /dev/null +++ b/index.js @@ -0,0 +1,130 @@ +const chalk = require('chalk') +function comparePrint(shoudlbe, myimpl) { + let overeenstemming = 0 + for (let i=0;i< shoudlbe.length;i++) { + if (myimpl[i] == shoudlbe[i]) overeenstemming++; else break; + } + + console.log(' myimpl: ', chalk.greenBright(myimpl.substring(0, overeenstemming)) + chalk.red(myimpl.substring(overeenstemming))) + console.log(' should: ', chalk.blue(shoudlbe)) + console.log() +} + +const crc = require('node-crc') +const crypto = require("crypto") +//console.log(crypto.getCiphers()) + + + +function getNullBytes(count) { + return '\x00'.repeat(count) +} +const TEST_KEY = '7C524984DA2A43607483ED3EFF92365FFF1C374024961F83CDA14A8DECD3D93A'.toLowerCase() +const key = Buffer.from(TEST_KEY, 'hex') + +const iv = 0x16FB7A1C +const timestamp = 0x8E83DB0D +const ivPadBuffer = Buffer.from("00".repeat(8), 'hex') +const ivWithTimestamp = Buffer.from(timestamp.toString(16) + iv.toString(16), 'hex') +const ivWithTimestampPadded = Buffer.concat([ ivWithTimestamp, ivPadBuffer ]) +// right hash +const uncPayload = "Test" +console.log(uncPayload.length) +const uncPayloadHash = Buffer.from(crypto.createHash("sha1").update(uncPayload).digest('hex').substring(0,10), 'hex') // first 40bits. 2*40/8 + +let preparedPayload = "ENCR" + uncPayload + getNullBytes(16) // random null-fill + +console.log('Key:') +comparePrint(TEST_KEY, key.toString('hex')) + +console.log('IV with Timestamp:') +comparePrint( + '8e83db0d16fb7a1c', // should + ivWithTimestamp.toString('hex') // is +) + +console.log('SHA1(Cleartext):') +comparePrint( + '640ab2bae0', //should + uncPayloadHash.toString('hex') // is +) + + +console.log('1) EncIndWithClearText', preparedPayload.length + ' bytes') +comparePrint( + '454e43525465737400000000000000000000000000000000', // should + Buffer.from(preparedPayload).toString('hex') // is +) + +let compressedPreparedPayload = preparedPayload.split('').map(x => { //foreach char run this + return x.charCodeAt(0) //get byte + .toString(2) // to base2/bcd/"binary" + .padStart(8, '0') // pad to 8 digits (dec4 = 100 => 00000100) + .split('') // to array + .reverse() // reverse + .join('') // back to string + .substring(0, 7) //take first 7 chars +}).join('') //join to one complete string + +//compressedPreparedPayload += '0'.repeat(8-(compressedPreparedPayload.length % 8)) + +let compressedPayload = [] +for (let i=0;i