Skip to content

Instantly share code, notes, and snippets.

@sebassdc
Created September 14, 2016 06:41
Show Gist options
  • Save sebassdc/50ac98e442878facf6b80d8cb44971c4 to your computer and use it in GitHub Desktop.
Save sebassdc/50ac98e442878facf6b80d8cb44971c4 to your computer and use it in GitHub Desktop.
// Everyone knows passphrases. One can choose passphrases from poems, songs, movies names and so on but frequently they can be guessed due to common cultural references. You can get your passphrases stronger by different means. One is the following:
// choose a text in capital letters including or not digits and non alphabetic characters,
// shift each letter by a given number but the transformed letter must be a letter (circular shift),
// replace each digit by its complement to 9,
// keep such as non alphabetic and non digit characters,
// downcase each letter in odd position, upcase each letter in even position (the first character is in position 0),
// reverse the whole result.
// Example:
// your text: "BORN IN 2015!", shift 1
// 1 + 2 + 3 -> "CPSO JO 7984!"
// 4 "CpSo jO 7984!"
// 5 "!4897 Oj oSpC"
// With longer passphrases it's better to have a small and easy program. Would you write it?
function rot (c, n) { // Rotate a character in a circular way (only uppercase acepted)
var num = c.toUpperCase().charCodeAt(0) + n;
if (num > 90) num -= 26;
if (num < 65) num += 25;
return String.fromCharCode(num);
}
function playPass(s, n) {
var c_aux = '';
var s_aux = '';
var l = s.length;
var num = 0;
for (var i = 0; i < l; i += 1) {
var char = s[i];
// looking for alphabetic characters
if (char.match(/[A-Za-z]/)) {
c_aux = rot(char, n);
// if is odd downcase the letter
if (i % 2 != 0) c_aux = c_aux.toLowerCase();
// Looking for digits
} else if (char.match(/\d/)) {
c_aux = Math.abs(eval(char) - 9);
} else { // Default case
c_aux = char;
}
// Concatenate the string
s_aux += c_aux;
}
// Simply return the reversed string
return s_aux.split('').reverse().join('');
}
console.log(playPass("I LOVE YOU!!!", 1)); // "!!!vPz fWpM J"
console.log(playPass("BORN IN 2015!", 1));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment