We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 47227f7 commit 82e50cdCopy full SHA for 82e50cd
1 file changed
src/generate-random-bytes.ts
@@ -1,8 +1,15 @@
1
import { BYTE_LENGTH } from './utils';
2
3
export default function generateRandomBytes(): string {
4
- const buffer = window.crypto.getRandomValues(new Uint8Array(BYTE_LENGTH));
5
- const bytes = btoa(String.fromCharCode(...new Uint8Array(buffer)));
+ if (typeof window !== 'undefined') {
+ const buffer = window.crypto.getRandomValues(new Uint8Array(BYTE_LENGTH));
6
+ const bytes = btoa(String.fromCharCode(...new Uint8Array(buffer)));
7
+
8
+ return bytes;
9
+ }
10
11
+ const buffer = require('crypto').randomBytes(BYTE_LENGTH);
12
+ const bytes = buffer.toString('base64');
13
14
return bytes;
15
}
0 commit comments