UNPKG

keygentoolshed

Version:

Key generation utilities for cryptographic operations. QUANTUM ENCRYPTION FOLDER UPDATE!!! See its folder for all <3

55 lines (48 loc) 2 kB
import crypto from 'crypto' // Function to generate a random 256-bit key (32 bytes) function generateSerpentKey() { return crypto.randomBytes(32); // 32 bytes = 256 bits } // S-boxes for the Serpent algorithm const S_BOXES = [ // S0 [0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76], // S1 [0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0x4c, 0x5c, 0x3d], // S2 [0x7f, 0x94, 0x9b, 0x4a, 0x0f, 0x3a, 0x4d, 0x2c, 0x5e, 0xa0, 0x8c, 0xb1, 0x7b, 0x8f, 0x10, 0x3b], // S3 [0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0x9d, 0x3e, 0x4f, 0x5c, 0x6d, 0x7e, 0x8f, 0x9a, 0x0b], // Additional S-boxes would be defined here... ]; // Function to apply S-box transformation function applySBox(byte, sBoxIndex) { const row = (byte >> 4) & 0x0F; // Get the row (first 4 bits) const col = byte & 0x0F; // Get the column (last 4 bits) return S_BOXES[sBoxIndex][col]; // Use the specified S-box } // Function to perform key scheduling function keySchedule(key) { const roundKeys = []; const numRounds = 32; // Serpent uses 32 rounds // Initial key expansion for (let i = 0; i < numRounds; i++) { const roundKey = Buffer.alloc(32); for (let j = 0; j < key.length; j++) { // Apply S-box transformation and XOR with the round index roundKey[j] = applySBox(key[(j + i) % key.length], j % S_BOXES.length) ^ i; } roundKeys.push(roundKey); } return roundKeys; } // Function to generate the Serpent key and its round keys function generateSerpentKeyAndSchedule() { const key = generateSerpentKey(); const roundKeys = keySchedule(key); return { key, roundKeys }; } // Generate the key and round keys const result = generateSerpentKeyAndSchedule(); console.log("Generated Serpent Key (HEX):", result.key.toString('hex')); console.log("Round Keys (HEX):", result.roundKeys.map(k => k.toString('hex')));