UNPKG

@sanity/visual-editing

Version:

[![npm stat](https://img.shields.io/npm/dm/@sanity/visual-editing.svg?style=flat-square)](https://npm-stat.com/charts.html?package=@sanity/visual-editing) [![npm version](https://img.shields.io/npm/v/@sanity/visual-editing.svg?style=flat-square)](https://

28 lines (25 loc) 623 B
// WHATWG crypto RNG - https://w3c.github.io/webcrypto/Overview.html function whatwgRNG(length = 16) { const rnds8 = new Uint8Array(length) crypto.getRandomValues(rnds8) return rnds8 } const getByteHexTable = (() => { let table: string[] return () => { if (table) { return table } table = [] for (let i = 0; i < 256; ++i) { table[i] = (i + 0x100).toString(16).slice(1) } return table } })() export function randomKey(length?: number): string { const table = getByteHexTable() return whatwgRNG(length) .reduce((str, n) => str + table[n], '') .slice(0, length) }