Skip to content

Instantly share code, notes, and snippets.

@swarajd
Created November 12, 2018 04:28
Show Gist options
  • Save swarajd/4ce021f3ac5cb04594e8b280addad418 to your computer and use it in GitHub Desktop.
Save swarajd/4ce021f3ac5cb04594e8b280addad418 to your computer and use it in GitHub Desktop.
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