Skip to content

Improved ciphers #954

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 17 additions & 25 deletions Ciphers/Atbash.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,22 @@
/*
The Atbash cipher is a particular type of monoalphabetic cipher
formed by taking the alphabet and mapping it to its reverse,
so that the first letter becomes the last letter,
the second letter becomes the second to last letter, and so on.
*/

/**
* Decrypt a Atbash cipher
* @param {String} str - string to be decrypted/encrypt
* @return {String} decrypted/encrypted string
* @function Atbash - Decrypt a Atbash cipher
* @description - The Atbash cipher is a particular type of monoalphabetic cipher formed by taking the alphabet and mapping it to its reverse, so that the first letter becomes the last letter, the second letter becomes the second to last letter, and so on.
* @param {string} str - string to be decrypted/encrypt
* @return {string} decrypted/encrypted string
* @see - [wiki](https://en.wikipedia.org/wiki/Atbash)
*/
function Atbash (message) {
let decodedString = ''
for (let i = 0; i < message.length; i++) {
if (/[^a-zA-Z]/.test(message[i])) {
decodedString += message[i]
} else if (message[i] === message[i].toUpperCase()) {
decodedString += String.fromCharCode(90 + 65 - message.charCodeAt(i))
} else {
decodedString += String.fromCharCode(122 + 97 - message.charCodeAt(i))
}
const Atbash = (str) => {
if (typeof str !== 'string') {
throw new TypeError('Argument should be string')
}
return decodedString
}

export { Atbash }
return str.replace(/[a-z]/gi, (char) => {
if (/[A-Z]/.test(char)) {
return String.fromCharCode(90 + 65 - char.charCodeAt())
}

return String.fromCharCode(122 + 97 - char.charCodeAt())
})
}

// > Atbash('HELLO WORLD')
// 'SVOOL DLIOW'
export default Atbash
17 changes: 17 additions & 0 deletions Ciphers/test/Atbash.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import Atbash from '../Atbash'

describe('Testing Atbash function', () => {
it('Test - 1, passing the non-string as an argument', () => {
expect(() => Atbash(0x345)).toThrow()
expect(() => Atbash(123)).toThrow()
expect(() => Atbash(123n)).toThrow()
expect(() => Atbash(false)).toThrow()
expect(() => Atbash({})).toThrow()
expect(() => Atbash([])).toThrow()
})

it('Test - 2, passing all alphabets', () => {
expect(Atbash('HELLO WORLD')).toBe('SVOOL DLIOW')
expect(Atbash('The quick brown fox jumps over the lazy dog')).toBe('Gsv jfrxp yildm ulc qfnkh levi gsv ozab wlt')
})
})