Last active
September 15, 2017 19:21
-
-
Save o0101/094497fdda0dbd7ef7f62e088543bd40 to your computer and use it in GitHub Desktop.
5lines - useful utils in 5 lines or less
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
/* 5lines - function body must be not more than 5 SLOC */ | |
// encrypt a string, also decrypt a string | |
function encrypt(key, str, s = 1) { | |
// clean | |
s = Number.isNaN(parseInt(s)) ? 1 : parseInt(s); | |
// turn key into numbers | |
key = new Uint16Array([...key].map( k => k.codePointAt(0) )); | |
// write generator to produce RNG | |
const rng = (function*(){while(true){yield(key.reduce((K,k,i)=>(key[(i+s)%key.length]+=k, k=(k<<1||k>>15),K+k),s),key[0])}}()); | |
// encrypt string | |
str = new Uint16Array( [...str].map( s => s.codePointAt(0) ) ).map( s => s ^ rng.next().value ); | |
// convert to characters & return | |
return Array.from(str).map( s => String.fromCodePoint(s) ).join(''); | |
} | |
// create a random (meant-to-be-unique) code | |
function rcode(s = 1) { | |
s = Number.isNaN(parseInt(s)) ? 1 : parseInt(s); | |
const ran32s = (crypto.getRandomValues(new Uint32Array(3)).map(v=>v^(+new Date)).map((v,i)=>v+performance.now()+i*s)); | |
const ran8s = new Uint8Array(ran32s.buffer); | |
const ranbytes = Array.from(ran8s).map( b => String.fromCodePoint(b) ); | |
return btoa(ranbytes.join('')); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment