【问题标题】:How to import forge sha256 hash function on my Typescript Create React App project?如何在我的 Typescript Create React App 项目中导入 forge sha256 哈希函数?
【发布时间】:2020-01-18 16:05:45
【问题描述】:

作为参考,我使用的是 React 16.9.0 和 Typescript 3.5.3

关于Are there any SHA-256 javascript implementations that are generally considered trustworthy?这个问题,我了解了forge节点应用程序从字符串创建256个哈希。

这里可以找到sha256函数的实现和使用:

  1. https://github.com/digitalbazaar/forge/blob/master/lib/sha256.js
  2. https://github.com/digitalbazaar/forge#sha256

但是,我无法使用 Typescript 在我的 create-react-app 项目中导入它。

我使用这些命令安装了forge 库:

npm install node-forge
npm install @types/node-forge

当我尝试导入它时,Typescript 一直告诉我:

TypeScript error in /src/App.tsx(2,10):
Module '"node-forge"' has no exported member 'forge'.  TS2305

    1 | import React from 'react';
  > 2 | import { forge } from 'node-forge';
      |          ^
    3 | import './App.css';
    4 | 

如何在我的 create-react-app 项目中正确导入 forge 哈希库,这样我就可以用这个创建一个哈希:

var md = forge.md.sha256.create();
md.update('The quick brown fox jumps over the lazy dog');
console.log(md.digest().toHex());
// output: d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592

【问题讨论】:

  • 你试过import * as forge from 'node-forge'吗?
  • 您所说的“值得信赖”是什么意思?如果您的意思是“产生正确的结果”,那么您可以通过运行它并根据已知值检查结果来测试一个包。如果你的意思是别的,你应该进一步解释。
  • @Kryten,我没有写trustworthy,写trustworthy的人是我要链接的问题。
  • 对不起,我看错了问题!

标签: node.js reactjs typescript import


【解决方案1】:

import forge from 'node-forge'import * as forge from "node-forge"; 怎么样。或者你只是使用不同的库。例如:bcrypt

【讨论】:

    猜你喜欢
    • 2019-02-18
    • 2020-07-03
    • 2018-11-27
    • 2019-10-29
    • 2020-06-26
    • 2021-09-03
    • 1970-01-01
    • 2020-10-30
    • 2018-09-16
    相关资源
    最近更新 更多