Added new codebook page
This commit is contained in:
@@ -1,15 +1,8 @@
|
||||
// encryption.js
|
||||
|
||||
// Phonetic alphabet for table names
|
||||
const PHONETIC = ['Alpha', 'Bravo', 'Charlie', 'Delta', 'Echo', 'Foxtrot', 'Golf', 'Hotel',
|
||||
'India', 'Juliet', 'Kilo', 'Lima', 'Mike', 'November', 'Oscar', 'Papa',
|
||||
'Quebec', 'Romeo', 'Sierra', 'Tango', 'Uniform', 'Victor', 'Whiskey',
|
||||
'Xray', 'Yankee', 'Zulu'];
|
||||
|
||||
const CHARS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
|
||||
const LETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
||||
const PHONETIC = ['Alpha', 'Bravo', 'Charlie', 'Delta', 'Echo', 'Foxtrot', 'Golf', 'Hotel',
|
||||
'India', 'Juliet', 'Kilo', 'Lima', 'Mike', 'November', 'Oscar', 'Papa', 'Quebec', 'Romeo',
|
||||
'Sierra', 'Tango', 'Uniform', 'Victor', 'Whiskey', 'Xray', 'Yankee', 'Zulu'];
|
||||
|
||||
// Seeded RNG using SHA256
|
||||
class SeededRandom {
|
||||
constructor(seed) {
|
||||
this.seed = seed;
|
||||
@@ -32,17 +25,15 @@ class SeededRandom {
|
||||
}
|
||||
}
|
||||
|
||||
// HOTP Generation - 3 digit nonce -> 3 letter code
|
||||
function generateHOTP(secret, nonce) {
|
||||
const rng = new SeededRandom(secret + ':hotp:' + nonce);
|
||||
let code = '';
|
||||
for (let i = 0; i < 3; i++) {
|
||||
code += rng.choice(LETTERS.split(''));
|
||||
for (let i = 0; i < 2; i++) {
|
||||
code += rng.choice(CHARS.split(''));
|
||||
}
|
||||
return code;
|
||||
}
|
||||
|
||||
// Auth Tables
|
||||
function generateAuthTable(seed, tableIndex) {
|
||||
const rng = new SeededRandom(seed + ':table:' + tableIndex);
|
||||
const rows = 'NOPQRSTUVWXYZ'.split('');
|
||||
@@ -60,12 +51,11 @@ function generateAuthTable(seed, tableIndex) {
|
||||
return table;
|
||||
}
|
||||
|
||||
// One-Time Pad (for encode/decode)
|
||||
function generatePad(seed, padKey, length = 60) {
|
||||
const rng = new SeededRandom(seed + ':pad:' + padKey);
|
||||
const pad = [];
|
||||
for (let i = 0; i < length; i++) {
|
||||
pad.push(rng.nextInt(0, 256));
|
||||
pad.push(rng.nextInt(0, 1000));
|
||||
}
|
||||
return pad;
|
||||
}
|
||||
@@ -73,8 +63,8 @@ function generatePad(seed, padKey, length = 60) {
|
||||
function generatePadDisplay(seed, padKey) {
|
||||
const rng = new SeededRandom(seed + ':pad:' + padKey);
|
||||
const groups = [];
|
||||
for (let i = 0; i < 12; i++) {
|
||||
const num = rng.nextInt(0, 100000).toString().padStart(5, '0');
|
||||
for (let i = 0; i < 24; i++) {
|
||||
const num = rng.nextInt(0, 1000).toString().padStart(3, '0');
|
||||
groups.push(num);
|
||||
}
|
||||
return groups.join(' ');
|
||||
@@ -84,30 +74,28 @@ function encodeMessage(message, key, padKey, useStream = true) {
|
||||
const messageBytes = new TextEncoder().encode(message);
|
||||
const padLength = useStream ? messageBytes.length : 60;
|
||||
const pad = generatePad(key, padKey, padLength);
|
||||
|
||||
const encoded = [];
|
||||
for (let i = 0; i < messageBytes.length; i++) {
|
||||
const padByte = pad[i % pad.length];
|
||||
encoded.push(messageBytes[i] ^ padByte);
|
||||
const padValue = pad[i % pad.length];
|
||||
const result = (messageBytes[i] + padValue) % 1000;
|
||||
encoded.push(result.toString().padStart(3, '0'));
|
||||
}
|
||||
|
||||
return Array.from(encoded).map(b => b.toString(16).padStart(2, '0')).join('');
|
||||
return encoded.join('');
|
||||
}
|
||||
|
||||
function decodeMessage(encoded, key, padKey, useStream = true) {
|
||||
const bytes = [];
|
||||
for (let i = 0; i < encoded.length; i += 2) {
|
||||
bytes.push(parseInt(encoded.substr(i, 2), 16));
|
||||
const values = [];
|
||||
for (let i = 0; i < encoded.length; i += 3) {
|
||||
values.push(parseInt(encoded.substr(i, 3), 10));
|
||||
}
|
||||
|
||||
const padLength = useStream ? bytes.length : 60;
|
||||
const padLength = useStream ? values.length : 60;
|
||||
const pad = generatePad(key, padKey, padLength);
|
||||
|
||||
const decoded = [];
|
||||
for (let i = 0; i < bytes.length; i++) {
|
||||
const padByte = pad[i % pad.length];
|
||||
decoded.push(bytes[i] ^ padByte);
|
||||
for (let i = 0; i < values.length; i++) {
|
||||
const padValue = pad[i % pad.length];
|
||||
let result = (values[i] - padValue) % 1000;
|
||||
if (result < 0) result += 1000;
|
||||
decoded.push(result);
|
||||
}
|
||||
|
||||
return new TextDecoder().decode(new Uint8Array(decoded));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user