mirror of
https://github.com/Nystik-gh/ignis.git
synced 2026-06-17 04:35:53 +00:00
more shims, path, crypto, url
This commit is contained in:
20
shims/crypto/random-bytes.js
Normal file
20
shims/crypto/random-bytes.js
Normal file
@@ -0,0 +1,20 @@
|
||||
// Shim for crypto.randomBytes
|
||||
// Uses Web Crypto API under the hood
|
||||
|
||||
export function randomBytes(size) {
|
||||
const buf = new Uint8Array(size);
|
||||
crypto.getRandomValues(buf);
|
||||
|
||||
// Add Buffer-like convenience methods
|
||||
buf.toString = function(encoding) {
|
||||
if (encoding === 'hex') {
|
||||
return Array.from(this).map(b => b.toString(16).padStart(2, '0')).join('');
|
||||
}
|
||||
if (encoding === 'base64') {
|
||||
return btoa(String.fromCharCode(...this));
|
||||
}
|
||||
return new TextDecoder().decode(this);
|
||||
};
|
||||
|
||||
return buf;
|
||||
}
|
||||
Reference in New Issue
Block a user