瀏覽代碼

docs: computeHash

Sven 9 月之前
父節點
當前提交
7981d38f1a
共有 3 個文件被更改,包括 38 次插入2 次删除
  1. 1 0
      README-summary.md
  2. 36 1
      README.md
  3. 1 1
      lib/DataStoreSerializer.ts

+ 1 - 0
README-summary.md

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

+ 36 - 1
README.md

@@ -52,6 +52,7 @@ View the documentation of previous major releases:
     - [`insertValues()`](#insertvalues) - insert values into a string at specified placeholders
     - [`insertValues()`](#insertvalues) - insert values into a string at specified placeholders
     - [`compress()`](#compress) - compress a string with Gzip or Deflate
     - [`compress()`](#compress) - compress a string with Gzip or Deflate
     - [`decompress()`](#decompress) - decompress a previously compressed string
     - [`decompress()`](#decompress) - decompress a previously compressed string
+    - [`computeHash()`](#computehash) - compute the hash / checksum of a string or ArrayBuffer
   - [**Arrays:**](#arrays)
   - [**Arrays:**](#arrays)
     - [`randomItem()`](#randomitem) - returns a random item from an array
     - [`randomItem()`](#randomitem) - returns a random item from an array
     - [`randomItemIndex()`](#randomitemindex) - returns a tuple of a random item and its index from an array
     - [`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
   
   
 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.  
 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.  
   
   
-⚠️ Needs to run in a secure context (HTTPS) due to the use of the Web Crypto API.  
+⚠️ Needs to run in a secure context (HTTPS) due to the use of the SubtleCrypto API.  
   
   
 The options object has the following properties:  
 The options object has the following properties:  
 | Property | Description |
 | Property | Description |
@@ -1491,6 +1492,40 @@ console.log(decompressed); // "Hello, World!"
 
 
 </details>
 </details>
 
 
+<br>
+
+### computeHash()
+Usage:  
+```ts
+computeHash(input: string | ArrayBuffer, algorithm?: string): Promise<string>
+```
+  
+Computes a hash / checksum of a string or ArrayBuffer using the specified algorithm ("SHA-256" by default).  
+The algorithm must be supported by the [SubtleCrypto API](https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/digest).  
+  
+⚠️ This function needs to be called in a secure context (HTTPS) due to the use of the SubtleCrypto API.  
+⚠️ 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.  
+  
+<details><summary><b>Example - click to view</b></summary>
+
+```ts
+import { computeHash } from "@sv443-network/userutils";
+
+async function run() {
+  const hash1 = await computeHash("Hello, World!");
+  const hash2 = await computeHash("Hello, World!");
+
+  console.log(hash1);           // dffd6021bb2bd5b0af676290809ec3a53191dd81c7f70a4b28688a362182986f
+  console.log(hash1 === hash2); // true (same input = same output)
+
+  const hash3 = await computeHash("Hello, world!"); // lowercase "w"
+  console.log(hash3); // 315f5bdb76d078c43b8ac0064e4a0164612b1fce77c869345bfc94c75894edd3
+}
+
+run();
+```
+</details>
+
 <br><br>
 <br><br>
 
 
 <!-- #SECTION Arrays -->
 <!-- #SECTION Arrays -->

+ 1 - 1
lib/DataStoreSerializer.ts

@@ -27,7 +27,7 @@ export type SerializedDataStore = {
  * 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.  
  * 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.  
  * Remember that you can call `super.methodName()` in the subclass to access the original method.  
  * Remember that you can call `super.methodName()` in the subclass to access the original method.  
  *   
  *   
- * ⚠️ Needs to run in a secure context (HTTPS) due to the use of the Web Crypto API.  
+ * ⚠️ Needs to run in a secure context (HTTPS) due to the use of the SubtleCrypto API if checksumming is enabled.  
  */
  */
 export class DataStoreSerializer {
 export class DataStoreSerializer {
   protected stores: DataStore[];
   protected stores: DataStore[];