<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                ## 加密和散列 `加密`是一個信息編碼的過程。這個過程將原始信息,即明文,轉換為密文。理想情況下,只有授權方可以將密文解密為明文。加密本身并不能防止干擾,但是會將可理解內容拒絕給一個可能的攔截器。加密是個雙向的函數,包含加密以及使用正確的`key`解密。 `哈希`是一個將給定值轉換成另一個值的過程。哈希函數使用數學算法來創建一個新值。一旦哈希完成,是無法從輸出值計算回輸入值的。 ### 加密 `Node.js`提供了一個內置的[crypto模塊](https://nodejs.org/api/crypto.html)可用于加密和解密字符串,數字,Buffer,流等等。Nest未在此基礎上提供額外的包以減少不必要的干擾。 一個使用`AES(高級加密系統) aes-256-ctr`算法,CTR加密模式。 ```TypeScript import { createCipheriv, randomBytes } from 'crypto'; import { promisify } from 'util'; const iv = randomBytes(16); const password = 'Password used to generate key'; // The key length is dependent on the algorithm. // In this case for aes256, it is 32 bytes. const key = (await promisify(scrypt)(password, 'salt', 32)) as Buffer; const cipher = createCipheriv('aes-256-ctr', key, iv); const textToEncrypt = 'Nest'; const encryptedText = Buffer.concat([ cipher.update(textToEncrypt), cipher.final(), ]); ``` 接下來,解密`encryptedText`值。 ```TypeScript import { createDecipheriv } from 'crypto'; const decipher = createDecipheriv('aes-256-ctr', key, iv); const decryptedText = Buffer.concat([ decipher.update(encryptedText), decipher.final(), ]); ``` ### 散列 散列方面推薦使用 [bcrypt](https://www.npmjs.com/package/bcrypt) 或 [argon2](https://www.npmjs.com/package/argon2)包. Nest自身并未提供任何這些模塊的包裝器以減少不必要的抽象(讓學習曲線更短)。 例如,使用`bcrypt`來哈希一個隨機密碼。 首先安裝依賴。 ```bash $ npm i bcrypt $ npm i -D @types/bcrypt ``` 依賴安裝后,可以使用哈希函數。 ```TypeScript import * as bcrypt from 'bcrypt'; const saltOrRounds = 10; const password = 'random_password'; const hash = await bcrypt.hash(password, saltOrRounds); ``` 使用`genSalt`函數來生成哈希需要的鹽。 ```TypeScript const salt = await bcrypt.genSalt(); ``` 使用`compare`函數來比較/檢查密碼。 ```TypeScript const isMatch = await bcrypt.compare(password, hash); ``` 更多函數參見[這里](https://www.npmjs.com/package/bcrypt)。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看