-
-
Save christophemarois/bc5ecc876080748112a8402ee6f7499a to your computer and use it in GitHub Desktop.
Pronounceable hashes from integers with optional hash. Demo: http://jsbin.com/tinevo
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
html, body { | |
margin: 15px; | |
font-family: 'Helvetica Neue'; | |
font-weight: 200; | |
} | |
* { | |
box-sizing: border-box; | |
} | |
h1 { | |
font-size: 4em; | |
width: 100%; | |
max-width: 500px; | |
font-weight: 200; | |
margin: 0 auto 2rem auto; | |
text-align: center; | |
} | |
input { | |
width: 100%; | |
max-width: 500px; | |
margin-left: auto; | |
margin-right: auto; | |
display: block; | |
font-size: 1.5em; | |
padding: 5px 20px; | |
margin-bottom: 10px; | |
} | |
#result { | |
border: 0; | |
font-size: 2em; | |
text-align: center; | |
color: #0059FF; | |
} |
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
$(function(){ | |
$('#int, #salt').on('input', function () { | |
if ( $('#int').val() == '' ) return $('#result').val(''); | |
var res = speakCrypt($('#int').val(), $('#salt').val()); | |
$('#result').val(res); | |
}); | |
$('#int').val(Math.ceil(Math.random() * 10000)).trigger('input'); | |
}); | |
function speakCrypt (int, salt, dict) { | |
dict = dict || [ | |
'a,e,i,o,u,oi,ou,ai,au,on,in', | |
'b,c,d,f,g,j,k,l,m,n,p,r,s,t,v,w,x,y,z,bl,ch,cl,cr,dr,fl,tr,br' | |
]; | |
if (!isFinite(int) || int < 0) | |
throw new TypeError('First argument must be a positive finite number.'); | |
// Alternate vowel-first and consonant-first syllables | |
// depending on whether the int is pair or not | |
var mod = (int % 2 === 0) ? 0 : 1; | |
// Generate all syllables | |
var syllables = []; | |
dict[ mod ].split(',').forEach(function (first) { | |
dict[ (mod+1) % 2 ].split(',').forEach(function (second) { | |
syllables.push(first + second); | |
}); | |
}); | |
// If there's a salt, use it to consistently shuffle the syllables | |
if (salt) { | |
var charCode, j, i, v, p; salt += ''; | |
for (i = syllables.length - 1, v = 0, p = 0; i > 0; i--, v++) { | |
v %= salt.length; | |
p += charCode = salt.charAt(v).charCodeAt(0); | |
j = (charCode + v + p) % i; | |
syllables.splice(j, 0, syllables.splice(i, 1)[0]); | |
} | |
} | |
// Find how many steps to look for | |
var steps = Math.ceil(Math.log(int) / Math.log(syllables.length)); | |
if (steps < 2) steps = 1; | |
// Deduce syllables from steps | |
var result = '', steppedIndex; | |
for (var i = steps; i > 0; i--) { | |
steppedIndex = Math.floor(int / Math.pow(syllables.length, i - 1)); | |
result += syllables[ Math.floor((steppedIndex % syllables.length) / 2) ]; | |
} | |
return result; | |
} |
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<script src="https://code.jquery.com/jquery-2.1.4.js"></script> | |
<meta charset="utf-8"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<h1>speakCrypt</h1> | |
<input id="int" type="number" tabindex="0" placeholder="Integer to encrypt" /> | |
<input id="salt" type="text" tabindex="0" placeholder="Salt" /> | |
<input id="result" type="text" tabindex="0" disabled="disabled" /> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment