60 lines
2.7 KiB
TypeScript
60 lines
2.7 KiB
TypeScript
import test from 'node:test';
|
|
import * as path from 'node:path';
|
|
import * as assert from 'node:assert/strict';
|
|
|
|
import { range, fileToBuff } from '../utils';
|
|
import { hexToBuff, buffTo64, xorBuffers, encryptSingleByte, decryptSingleByte, crackSingleByteKeyMsg, detectSingleByteXor, encryptRepeatingXOR, decryptRepeatingXOR } from '../basics';
|
|
|
|
const DATA_PATH = path.join(__dirname, '..', 'data');
|
|
|
|
test('range', () => {
|
|
assert.deepEqual(range(0, 10), [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
|
|
assert.deepEqual(range(5, 8), [5, 6, 7, 8]);
|
|
});
|
|
|
|
test('hexToBase64', () => {
|
|
const input = '49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d';
|
|
const expected = 'SSdtIGtpbGxpbmcgeW91ciBicmFpbiBsaWtlIGEgcG9pc29ub3VzIG11c2hyb29t';
|
|
assert.equal(buffTo64(hexToBuff(input)), expected);
|
|
});
|
|
|
|
test('xorBuffers', () => {
|
|
const input = '1c0111001f010100061a024b53535009181c';
|
|
const key = '686974207468652062756c6c277320657965';
|
|
const expected = '746865206b696420646f6e277420706c6179';
|
|
assert.equal(xorBuffers(hexToBuff(input), hexToBuff(key)).toString('hex'), expected);
|
|
});
|
|
|
|
test('encrypt/decrypt single byte', () => {
|
|
const input = '1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736';
|
|
const key = 55;
|
|
assert.deepEqual(decryptSingleByte(encryptSingleByte(Buffer.from(input, 'hex'), key), key).toString('hex'), input);
|
|
});
|
|
|
|
test('crack produces english sentence', () => {
|
|
const input = '1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736';
|
|
const expected = 'Cooking MC\'s like a pound of bacon';
|
|
assert.equal(crackSingleByteKeyMsg(input), expected);
|
|
});
|
|
|
|
test('detect single byte xor', () => {
|
|
const input = fileToBuff(path.join(DATA_PATH, 'xorSingleByte.txt'), 'hex');
|
|
const expected = 'Now that the party is jumping';
|
|
assert.equal(detectSingleByteXor(input), expected);
|
|
});
|
|
|
|
test('encrypt/decrypt repeating xor is symmetric', () => {
|
|
const input = 'random input to test this stuff outeqw e991231293 129083 h1207018 9';
|
|
const key = 'also random';
|
|
assert.equal(
|
|
decryptRepeatingXOR(encryptRepeatingXOR(Buffer.from(input, 'utf-8'), Buffer.from(key, 'utf-8')), Buffer.from(key, 'utf-8')).toString('utf-8'), input);
|
|
});
|
|
|
|
test('encrypt repeating xor', () => {
|
|
const input = fileToBuff(path.join(DATA_PATH, 'repeatingXOR.txt'), 'utf-8');
|
|
const parsedInput = Buffer.from(input.map(buff => buff.toString('utf-8')).join('\n'));
|
|
const key = 'ICE';
|
|
const expected = '0b3637272a2b2e63622c2e69692a23693a2a3c6324202d623d63343c2a26226324272765272a282b2f20430a652e2c652a3124333a653e2b2027630c692b20283165286326302e27282f';
|
|
assert.equal(encryptRepeatingXOR(parsedInput, Buffer.from(key, 'utf-8')).toString('hex'), expected);
|
|
});
|