Created
November 12, 2018 04:28
-
-
Save swarajd/4ce021f3ac5cb04594e8b280addad418 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const hill = (text, matrix) => { | |
if (text.length % matrix.length != 0) { | |
throw "please provide a plaintext that can be cleanly divided into " + matrix.length + " parts" | |
} | |
const chunks = []; | |
for (let i = 0; i < text.length; i += matrix.length) { | |
chunks.push(text.substring(i, i + matrix.length)); | |
} | |
const ciphertext = chunks.map(chunk => chunk.split('')) | |
.map(splitChunk => [splitChunk.map(letter => letterDict[letter])]) | |
.map(transpose) | |
.map(transposedChunk => { | |
console.log(matrix); | |
return modMatrix( | |
matrixMultiply(matrix, transposedChunk), | |
letters.length | |
); | |
}) | |
.map(transpose) | |
.map(splitChunk => splitChunk[0].map(num => letters[num])) | |
.map(chunk => chunk.join('')) | |
.join(''); | |
return { | |
plaintext: text, | |
ciphertext: ciphertext, | |
solution: matrix | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment