Skip to content

Commit 7981d38

Browse files
committed
docs: computeHash
1 parent 8aec58c commit 7981d38

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

README-summary.md

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ or view the documentation of previous major releases:
5050
- [`insertValues()`](https://github.com/Sv443-Network/UserUtils#insertvalues) - insert values into a string at specified placeholders
5151
- [`compress()`](https://github.com/Sv443-Network/UserUtils#compress) - compress a string with Gzip or Deflate
5252
- [`decompress()`](https://github.com/Sv443-Network/UserUtils#decompress) - decompress a previously compressed string
53+
- [`computeHash()`](https://github.com/Sv443-Network/UserUtils#computehash) - compute the hash / checksum of a string or ArrayBuffer
5354
- **Arrays:**
5455
- [`randomItem()`](https://github.com/Sv443-Network/UserUtils#randomitem) - returns a random item from an array
5556
- [`randomItemIndex()`](https://github.com/Sv443-Network/UserUtils#randomitemindex) - returns a tuple of a random item and its index from an array

README.md

+36-1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ View the documentation of previous major releases:
5252
- [`insertValues()`](#insertvalues) - insert values into a string at specified placeholders
5353
- [`compress()`](#compress) - compress a string with Gzip or Deflate
5454
- [`decompress()`](#decompress) - decompress a previously compressed string
55+
- [`computeHash()`](#computehash) - compute the hash / checksum of a string or ArrayBuffer
5556
- [**Arrays:**](#arrays)
5657
- [`randomItem()`](#randomitem) - returns a random item from an array
5758
- [`randomItemIndex()`](#randomitemindex) - returns a tuple of a random item and its index from an array
@@ -1129,7 +1130,7 @@ Also, by default a checksum is calculated and importing data with a mismatching
11291130

11301131
The class' internal methods are all declared as protected, so you can extend this class and override them if you need to add your own functionality.
11311132

1132-
⚠️ Needs to run in a secure context (HTTPS) due to the use of the Web Crypto API.
1133+
⚠️ Needs to run in a secure context (HTTPS) due to the use of the SubtleCrypto API.
11331134

11341135
The options object has the following properties:
11351136
| Property | Description |
@@ -1491,6 +1492,40 @@ console.log(decompressed); // "Hello, World!"
14911492

14921493
</details>
14931494

1495+
<br>
1496+
1497+
### computeHash()
1498+
Usage:
1499+
```ts
1500+
computeHash(input: string | ArrayBuffer, algorithm?: string): Promise<string>
1501+
```
1502+
1503+
Computes a hash / checksum of a string or ArrayBuffer using the specified algorithm ("SHA-256" by default).
1504+
The algorithm must be supported by the [SubtleCrypto API](https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/digest).
1505+
1506+
⚠️ This function needs to be called in a secure context (HTTPS) due to the use of the SubtleCrypto API.
1507+
⚠️ If you use this for cryptography, make sure to use a secure algorithm (under no circumstances use SHA-1) and to [salt](https://en.wikipedia.org/wiki/Salt_(cryptography)) your input data.
1508+
1509+
<details><summary><b>Example - click to view</b></summary>
1510+
1511+
```ts
1512+
import { computeHash } from "@sv443-network/userutils";
1513+
1514+
async function run() {
1515+
const hash1 = await computeHash("Hello, World!");
1516+
const hash2 = await computeHash("Hello, World!");
1517+
1518+
console.log(hash1); // dffd6021bb2bd5b0af676290809ec3a53191dd81c7f70a4b28688a362182986f
1519+
console.log(hash1 === hash2); // true (same input = same output)
1520+
1521+
const hash3 = await computeHash("Hello, world!"); // lowercase "w"
1522+
console.log(hash3); // 315f5bdb76d078c43b8ac0064e4a0164612b1fce77c869345bfc94c75894edd3
1523+
}
1524+
1525+
run();
1526+
```
1527+
</details>
1528+
14941529
<br><br>
14951530

14961531
<!-- #SECTION Arrays -->

lib/DataStoreSerializer.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export type SerializedDataStore = {
2727
* All methods are at least `protected`, so you can easily extend this class and overwrite them to use a different storage method or to add additional functionality.
2828
* Remember that you can call `super.methodName()` in the subclass to access the original method.
2929
*
30-
* ⚠️ Needs to run in a secure context (HTTPS) due to the use of the Web Crypto API.
30+
* ⚠️ Needs to run in a secure context (HTTPS) due to the use of the SubtleCrypto API if checksumming is enabled.
3131
*/
3232
export class DataStoreSerializer {
3333
protected stores: DataStore[];

0 commit comments

Comments
 (0)