Skip to content

Instantly share code, notes, and snippets.

View helloimalastair's full-sized avatar
💻
Understanding stuff I didn't build

Mikkel Ricafrente helloimalastair

💻
Understanding stuff I didn't build
View GitHub Profile
@helloimalastair
helloimalastair / phonetic.js
Last active May 7, 2022 21:24
Revision of Erisa's Phonetic Generator. Utilizes Alastair's BetterRand
// Revision of Erisa's Phonetic Generator(https://github.com/Erisa/starbin/blob/b8a333b781a893dcae60c4fb2638d34e63c6f568/index.js#L38). Utilizes Alastair's BetterRand(https://gist.github.com/helloimalastair/9143e9d43f4a641b23ebddfa11cefeff)
const BetterRand = async t=>{const n=new Int32Array(20),r=(await(await fetch("https://drand.cloudflare.com/public/latest")).json()).signature;crypto.getRandomValues(n);const a=function(t){for(var n=0,r=1779033703^t.length;n<t.length;n++)r=(r=Math.imul(r^t.charCodeAt(n),3432918353))<<13|r>>>19;return function(){return r=Math.imul(r^r>>>16,2246822507),r=Math.imul(r^r>>>13,3266489909),(r^=r>>>16)>>>0}}((void 0===t?null:t.toString())+crypto.toString()+r+Date.now());return o=a(),e=a(),u=a(),c=a(),function(){var t=e<<9,n=5*o;return c^=e,e^=u^=o,o^=c,u^=t,c=c<<11|c>>>21,((n=9*(n<<7|n>>>25))>>>0)/4294967296};var o,e,u,c};
const randOf = (collection, rand) => () => collection[Math.floor(rand() * collection.length)];
async function generateId() {
const rand = await BetterRand("T
/* These types were generated messily by hand, and thus will probably not be the most intuitive, but they should be correct according to current Cloudflare DNS resolution Output. Pulled from the current CF DNS Spec at https://developers.cloudflare.com/1.1.1.1/encrypted-dns/dns-over-https/make-api-requests/dns-json */
declare type DNSJSON = {
/* The Response Code of the DNS Query. These are defined here: https://www.iana.org/assignments/dns-parameters/dns-parameters.xhtml#dns-parameters-6 */
Status: DNSRequestStatus,
/* If true, it means the truncated bit was set. This happens when the DNS answer is larger than a single UDP or TCP packet. TC will almost always be false with Cloudflare DNS over HTTPS because Cloudflare supports the maximum response size. */
TC: Boolean,
/* If true, it means the Recursive Desired bit was set. This is always set to true for Cloudflare DNS over HTTPS. */
RD: Boolean,
/* If true, it means the Recursion Available bit was set. This is always set to true for Cloudflare
@helloimalastair
helloimalastair / BetterRand.mjs
Last active January 11, 2022 22:50
A random number generator for Cloudflare Workers, that is optimized for truly random number generation. Works similarly to Math.random, generating numbers between 0 and 1. Comes compressed and expanded. Can be seeded with a custom seed, in addition to the generated seed.
/*
Note for all implementations below, you must call await on the initial function, as that is used to seed the generator with values from drand. This generator uses a hash function based on MurmurHash3, and the xoshiro128 generation algorithm, made by Vigna & Blackman, and both were ported to javascript by StackOverflow User Bryc. This generator can be seeded using a custom seed, which is plugged into the initial call.
Example Code:
*/
const rand = await BetterRand("This Seed Is Very Secure, and not at all a sentence that is easy to guess.");
console.log(rand()); //0.20066208578646183
/*The Code:*/
// Compressed Version, to be used with a bundler, or in module mode. Use this for everything except debugging. Size: 617 Bytes
export default async t=>{const n=new Int32Array(20),r=(await(await fetch("https://drand.cloudflare.com/public/latest")).json()).signature;crypto.getRandomValues(n);const a=function(t){for(var n=0,r=1779033703^t.length;n<t.length;n++)r=(r=Math.imul(r^t.charCodeAt(n),3432918353))<<13|r>>>19;