【发布时间】:2017-07-13 13:57:30
【问题描述】:
我正在尝试在 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 是字符串类型,如crypto-js 实现的文档中所指定。但是该函数返回一个包含 wordarray 的对象。
我也试过打电话
hash.toString(CryptoJS.enc.Hex)
但这不起作用,因为打字稿还假定hash 将是一个字符串。所以参数化的toString 函数是不允许的。
我做错了什么?
【问题讨论】:
-
hash.toString(CryptoJS.enc.Hex)应该是正确的,不过你也可以试试hash.toString() -
你是通过添加 toString() 还是其他方式解决的?
-
@NeriusJok 我一直没能解决它并通过自己的 MD5 实现
标签: javascript typescript cryptojs