[英]Crypto-JS hash functions return object
我正在嘗試在 react/typescript 項目中使用 crypto-js 庫對字符串進行哈希處理。 我正在使用 crypto-js 3.1.9 和 @types/crypto-js 3.1.33。
這是一些代碼:
import CryptoJS = require("crypto-js");
export const hashString= (str: string): string => {
const hash = CryptoJS.MD5(str);
return hash;
}
我希望hash
是 string 類型,如 crypto-js 實現的文檔中所指定。 但該函數返回一個對象,其中包含一個 wordarray。
我也試過打電話
hash.toString(CryptoJS.enc.Hex)
但這不起作用,因為打字稿還假定hash
將是一個字符串。 所以不允許使用參數化的toString
函數。
我做錯了什么?
我知道這是一個老問題,但我最近遇到了這個問題,如果有人正在尋找答案,我只是將結果轉換為字符串。 對我來說似乎工作正常。
console.log(typeof CryptoJS.MD5('hello')); console.log("String() => ", String(CryptoJS.MD5('hello')));
<script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.0.0/crypto-js.min.js"></script>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.