@sanity/visual-editing
Version:
[](https://npm-stat.com/charts.html?package=@sanity/visual-editing) [](https://
28 lines (25 loc) • 623 B
text/typescript
// 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)
}