Skip to content

Instantly share code, notes, and snippets.

View rndme's full-sized avatar

dandavis rndme

View GitHub Profile
function useState(value){ // designed after https://reactjs.org/docs/hooks-intro.html demo
var o = useState.state || (useState.state = new Map());
if(o.has(arguments.callee.caller)){ // not first time?
return o.get(arguments.callee.caller);
}else{
// build array of returns, set it, return it
var ret = [];
[...arguments].map((x, i)=>{
ret.push(x)
ret.push(function _updater(val){ ret[(i*2)] = val; });
function ip(cb) {
with(new RTCPeerConnection) {
createDataChannel(0);
createOffer(e=> setLocalDescription(e), Date);
onicecandidate =e=>{if(e=e.candidate) cb(e.candidate.split(/\s+/)[4])};
}
}
®‼⁉™ℹ↔↕↖↗↘↙↩↪⌚⌛⌨⏏⏩⏪⏫⏬⏭⏮⏯⏰⏱⏲⏳▪▫▶◀◻◼◽◾☀☁☂☃☄☎☑☔☕☘☝☠☢☣☦☪☮☯☸☹☺♀♂♈♉♊♋♌♍♎♏♐♑♒♓♟♠♣♥♦♨♻♾♿⚒⚓⚔⚕⚖⚗⚙⚛⚜⚠⚡⚪⚫⚰⚱⚽⚾⛄⛅⛈⛎⛏⛑⛓⛔⛩⛪⛰⛱⛲⛳⛴⛵⛷⛸⛹⛹️‍♀️⛹️‍♂️⛺⛽✂✅✈✉✊✋✌✍✏✒✔✖✝✡✨✳✴❄❇❌❎❓❔❕❗❣❤➕➖➗➡➰➿⤴⤵⬅⬆⬇⬛⬜⭐⭕〰〽🌀🌁🌂🌃🌄🌅🌆🌇🌈🌉🌊🌋🌌🌍🌎🌏🌐🌑🌒🌓🌔🌕🌖🌗🌘🌙🌚🌛🌜🌝🌞🌟🌠🌰🌱🌲🌳🌴🌵🌶🌷🌸🌹🌺🌻🌼🌽🌾🌿🍀🍁🍂🍃🍄🍅🍆🍇🍈🍉🍊🍋🍌🍍🍎🍏🍐🍑🍒🍓🍔🍕🍖🍗🍘🍙🍚🍛🍜🍝🍞🍟🍠🍡🍢🍣🍤🍥🍦🍧🍨🍩🍪🍫🍬🍭🍮🍯🍰🍱🍲🍳🍴🍵🍶🍷🍸🍹🍺🍻🍼🎀🎁🎂🎃🎄🎅🎆🎇🎈🎉🎊🎋🎌🎍🎎🎏🎐🎑🎒🎓🎠🎡🎢🎣🎤🎥🎦🎧🎨🎩🎪🎫🎬🎭🎮🎯🎰🎱🎲🎳🎴🎵🎶🎷🎸🎹🎺🎻🎼🎽🎾🎿🏀🏁🏂🏃🏃‍♀️🏃‍♂️🏄🏄‍♀️🏄‍♂️🏅🏆🏇🏈🏉🏊🏊‍♀️🏊🏠🏡🏢🏣🏤🏥🏦🏧🏨🏩🏪🏫🏬🏭🏮🏯🏰‍🌈‍☠️🐀🐁🐂🐃🐄🐅🐆🐇🐈🐉🐊🐋🐌🐍🐎🐏🐐🐑🐒🐓🐔🐕🐖🐗🐘🐙🐚🐛🐜🐝🐞🐟🐠🐡🐢🐣🐤🐥🐦🐧🐨🐩🐪🐫🐬🐭🐮🐯🐰🐱🐲🐳🐴🐵🐶🐷🐸🐹🐺🐻🐼🐽🐾👀👂👃👄👅👆👇👈👉👊👋👌👍👎👏👐👑👒👓👔👕👖👗👘👙👚👛👜👝👞👟👠👡👢👣👤👥👦👧👨👨‍⚕️👨‍⚖️👨‍✈️👨‍❤️‍👨👨‍❤️‍💋‍👨👨‍🌾👨‍🍳👨‍🎓👨‍🎤👨‍🎨👨‍🏫👨‍🏭👫👬👭👮👮‍♀️👮‍♂️👯👯‍♀️👯‍♂️👰👱👱‍♀️👱‍♂️👲👳👳‍♀️👳‍♂️👴👵👶👷👷‍♀️👷‍♂️👸👹👺👻👼👽👾👿💀💁💁‍♀️💁‍♂️💂💂‍♀️💂‍♂️💃💄💅💆💆‍♀️💆‍♂️💇💇‍♀️💇‍♂️💈💉💊💋💌💍💎💏💐💑💒💓💔💕💖💗💘💙💚💛💜💝💞💟💠💡💢💣💤💥💦💧💨💩💪💫💬💭💮💯💰💱💲💳💴💵💶💷💸💹💺💻💼💽💾💿📀📁📂📃📄📅📆📇📈📉📊📋📌📍📎📏📐📑📒📓📔📕📖📗📘📙📚📛📜📝📞📟📠📡📢📣📤📥📦📧📨📩📪📫📬📭📮📯📰📱📲📳📴📵📶📷📹📺📻📼🔀🔁🔂🔃🔄🔅🔆🔇🔈🔉🔊🔋🔌🔍🔎🔏🔐🔑🔒🔓🔔🔕🔖🔗🔘🔙🔚🔛🔜🔝🔞🔟🔠🔡🔢🔣🔤🔥🔦🔧🔨🔩🔪🔫🔬🔭🔮🔯🔰🔱🔲🔳🔴🔵🔶🔷🔸🔹🔺🔻🔼🔽🕐🕑🕒🕓🕔🕕🕖🕗🕘🕙🕚🕛🕜🕝🕞🕟🕠🕡🕢🕣🕤🕥🕦🕧♀️♂️🗻🗼🗽🗾🗿😀😁😂😃😄😅😆😇😈😉😊😋😌😍😎😏😐😑😒😓😔😕😖😗😘😙😚😛😜😝😞😟😠😡😢😣😤😥😦😧😨😩😪😫😬😭😮😯😰😱😲😳😴😵😶😷😸😹😺😻😼😽😾😿🙀🙅🙅‍♀️🙅‍♂️🙆🙆‍♀️🙆‍♂️🙇🙇‍♀️🙇‍♂️🙈🙉🙊🙋🙋‍♀️🙋‍♂️🙌🙍🙍‍♀️🙍‍♂️🙎🙎‍♀️🙎‍♂️🙏🚀🚁🚂🚃🚄🚅🚆🚇🚈🚉🚊🚋🚌🚍🚎🚏🚐🚑🚒🚓🚔🚕🚖🚗🚘🚙🚚🚛🚜🚝🚞🚟🚠🚡🚢🚣🚣‍♀️🚣‍♂️🚤🚥🚦🚧🚨🚩
function copy(str){
var inp = document.createElement("textarea");
inp.style.position="fixed";
document.body.appendChild(inp);
inp.value=str;
inp.select();
document.execCommand("Copy");
inp.remove();
}
@rndme
rndme / hashcrypt.js
Last active November 1, 2018 18:11
Symmetric encryption using hash function and key derivation
function hashCrypt(key, plain, hasher, workFactor=1) { // hasher is any sync string-output hash function
var len=plain.length, keyLen = key.length, keyOrig = key,
// calculate derivation count based on deterministic factors from the inputs:
mx = hasher(key).split("").map((a, b)=> a.charCodeAt(0) + b).reduce((a, b)=> a + b, 0) * workFactor;
// derive firstly for some hardness using cpu and ram hungry calculations:
for(let i = 0; i < mx; i++) key = hasher(key.repeat( ((i+len) % ((workFactor || 1) * 4))+1 ));
// expand key to needed length by appending re-hashing (with counters):
@rndme
rndme / spritzstream.js
Last active July 26, 2016 15:24
given a seed value "iv" (optional), returns a deterministic string of pseudo-random hex values of length "chars"
// public domain, based on https://www.schneier.com/blog/archives/2014/10/spritz_a_new_rc.html
function spritzStream(chars, iv) {
var s = [], key=iv || (Date.now() / (performance.now()*1000)).toString(36) + (chars/Math.random()),
j = 0,
w = 17,
k = 0,
res = [],
l = Math.ceil(key.length/2),
m = chars,
@rndme
rndme / rc4stream.js
Created July 26, 2016 07:36
given a seed value "iv" (optional), returns a deterministic string of pseudo-random hex values of length "chars"
function rc4stream(chars, iv) {
var s = [], key=iv || (Date.now() / (performance.now()*1000)).toString(36) + (chars/Math.random()),
j = 0,
x, res = [],
i, j, y, l = key.length,
m = Math.ceil(chars/2);
for(i = 0; i < 256; i++) s[i] = i;
for(i = 0; i < 256; i++) {
j = (j + s[i] + key.charCodeAt(i % l)) % 256;
@rndme
rndme / sha3stream.js
Last active July 26, 2016 07:55
given a seed value "iv" (optional), returns a deterministic string of pseudo-random hex values of length "chars"
function stream(chars, iv){ // requires https://github.com/emn178/js-sha3/
var buff="", i=0;
iv= sha3_512(iv || (Date.now() / (performance.now()*1000)).toString(36) + (chars/Math.random()));
while(buff.length < chars) buff+= iv = sha3_256(++i + iv);
return buff.slice(0, chars);
}
//ex:
// stream(500, "this is not a great iv");
@rndme
rndme / nonce maker.js
Last active July 26, 2016 01:22
a fast clock times a slow clock and uses a factor of the difference to build a un-predictable 36 alphabet string
function nonce(chars){
var b="", round, last;
while(true){
var round= (Date.now() / (performance.now()*1000)).toString(36).split(".").pop().slice(2, -2);
if(round!=last) b+= round;
if(b.length > chars) break;
last=round;
}
return b.slice(-chars);
}
@rndme
rndme / derive.js
Last active July 6, 2016 02:18
key derivation using sha3 and variable loop count
/* password derivation that forces a re-creation of this routine by making slight mods to a normal hash-based kdf:
requires https://github.com/emn178/js-sha3
Example timings on a i7 in chrome:
cost ms
1 - 108
2 - 408
3 - 1067
4 - 2351
5 - 3927