You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Made `randomId()` default to using Math.random() and added the parameter `enhancedEntropy` to revert back to the much slower but also much more entropic implementation
Copy file name to clipboardexpand all lines: README-summary.md
+1-1
Original file line number
Diff line number
Diff line change
@@ -39,7 +39,6 @@ or view the documentation of previous major releases:
39
39
-[`clamp()`](https://github.com/Sv443-Network/UserUtils#clamp) - constrain a number between a min and max value
40
40
-[`mapRange()`](https://github.com/Sv443-Network/UserUtils#maprange) - map a number from one range to the same spot in another range
41
41
-[`randRange()`](https://github.com/Sv443-Network/UserUtils#randrange) - generate a random number between a min and max boundary
42
-
-[`randomId()`](https://github.com/Sv443-Network/UserUtils#randomid) - generate a random ID of a given length and radix
43
42
-**Misc:**
44
43
-[`DataStore`](https://github.com/Sv443-Network/UserUtils#datastore) - class that manages a hybrid sync & async persistent JSON database, including data migration
45
44
-[`DataStoreSerializer`](https://github.com/Sv443-Network/UserUtils#datastoreserializer) - class for importing & exporting data of multiple DataStore instances, including compression, checksumming and running migrations
@@ -51,6 +50,7 @@ or view the documentation of previous major releases:
51
50
-[`compress()`](https://github.com/Sv443-Network/UserUtils#compress) - compress a string with Gzip or Deflate
52
51
-[`decompress()`](https://github.com/Sv443-Network/UserUtils#decompress) - decompress a previously compressed string
53
52
-[`computeHash()`](https://github.com/Sv443-Network/UserUtils#computehash) - compute the hash / checksum of a string or ArrayBuffer
53
+
-[`randomId()`](https://github.com/Sv443-Network/UserUtils#randomid) - generate a random ID of a given length and radix
54
54
-**Arrays:**
55
55
-[`randomItem()`](https://github.com/Sv443-Network/UserUtils#randomitem) - returns a random item from an array
56
56
-[`randomItemIndex()`](https://github.com/Sv443-Network/UserUtils#randomitemindex) - returns a tuple of a random item and its index from an array
Copy file name to clipboardexpand all lines: README.md
+34-31
Original file line number
Diff line number
Diff line change
@@ -41,7 +41,6 @@ View the documentation of previous major releases:
41
41
-[`clamp()`](#clamp) - constrain a number between a min and max value
42
42
-[`mapRange()`](#maprange) - map a number from one range to the same spot in another range
43
43
-[`randRange()`](#randrange) - generate a random number between a min and max boundary
44
-
-[`randomId()`](#randomid) - generate a random ID of a given length and radix
45
44
-[**Misc:**](#misc)
46
45
-[`DataStore`](#datastore) - class that manages a hybrid sync & async persistent JSON database, including data migration
47
46
-[`DataStoreSerializer`](#datastoreserializer) - class for importing & exporting data of multiple DataStore instances, including compression, checksumming and running migrations
@@ -53,6 +52,7 @@ View the documentation of previous major releases:
53
52
-[`compress()`](#compress) - compress a string with Gzip or Deflate
54
53
-[`decompress()`](#decompress) - decompress a previously compressed string
55
54
-[`computeHash()`](#computehash) - compute the hash / checksum of a string or ArrayBuffer
55
+
-[`randomId()`](#randomid) - generate a random ID of a given length and radix
56
56
-[**Arrays:**](#arrays)
57
57
-[`randomItem()`](#randomitem) - returns a random item from an array
58
58
-[`randomItemIndex()`](#randomitemindex) - returns a tuple of a random item and its index from an array
@@ -928,36 +928,6 @@ randRange(10); // 7
928
928
929
929
</details>
930
930
931
-
<br>
932
-
933
-
### randomId()
934
-
Usage:
935
-
```ts
936
-
randomId(length?:number, radix?:number): string
937
-
```
938
-
939
-
Generates a cryptographically strong random ID of a given length and [radix (base).](https://en.wikipedia.org/wiki/Radix)
940
-
Uses the [Web Crypto API](https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues) for generating the random numbers.
941
-
⚠️ This is not intended for generating encryption keys, only for generating IDs with a decent amount of entropy!
942
-
943
-
The default length is 16 and the default radix is 16 (hexadecimal).
944
-
You may change the radix to get digits from different numerical systems.
945
-
Use 2 for binary, 8 for octal, 10 for decimal, 16 for hexadecimal and 36 for alphanumeric.
946
-
947
-
<details><summary><b>Example - click to view</b></summary>
Generates a random ID of a given length and [radix (base).](https://en.wikipedia.org/wiki/Radix)
1508
+
1509
+
The default length is 16 and the default radix is 16 (hexadecimal).
1510
+
You may change the radix to get digits from different numerical systems.
1511
+
Use 2 for binary, 8 for octal, 10 for decimal, 16 for hexadecimal and 36 for alphanumeric.
1512
+
1513
+
If `enhancedEntropy` is set to true (false by default), the [Web Crypto API](https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues) is used for generating the random numbers.
1514
+
Note that this takes MUCH longer, but the generated IDs will have a higher entropy.
1515
+
1516
+
⚠️ Not suitable for generating anything related to cryptography! Use [SubtleCrypto's `generateKey()`](https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/generateKey) for that instead.
1517
+
1518
+
<details><summary><b>Example - click to view</b></summary>
Copy file name to clipboardexpand all lines: lib/math.ts
-16
Original file line number
Diff line number
Diff line change
@@ -44,19 +44,3 @@ export function randRange(...args: number[]): number {
44
44
45
45
returnMath.floor(Math.random()*(max-min+1))+min;
46
46
}
47
-
48
-
/**
49
-
* Generates a random ID with the specified length and radix (16 characters and hexadecimal by default)
50
-
* Uses [`crypto.getRandomValues()`](https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues) for better cryptographic randomness
51
-
* ⚠️ Not suitable for generating encryption keys! Use [`crypto.subtle.generateKey()`](https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/generateKey) for that.
52
-
* @param length The length of the ID to generate (defaults to 16)
53
-
* @param radix The [radix](https://en.wikipedia.org/wiki/Radix) of each digit (defaults to 16 which is hexadecimal. Use 2 for binary, 10 for decimal, 36 for alphanumeric, etc.)
* Generates a random ID with the specified length and radix (16 characters and hexadecimal by default)
165
+
*
166
+
* ⚠️ Not suitable for generating anything related to cryptography! Use [SubtleCrypto's `generateKey()`](https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/generateKey) for that instead.
167
+
* @param length The length of the ID to generate (defaults to 16)
168
+
* @param radix The [radix](https://en.wikipedia.org/wiki/Radix) of each digit (defaults to 16 which is hexadecimal. Use 2 for binary, 10 for decimal, 36 for alphanumeric, etc.)
169
+
* @param enhancedEntropy If set to true, uses [`crypto.getRandomValues()`](https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues) for better cryptographic randomness (this also makes it take MUCH longer to generate)
0 commit comments