【问题标题】:TypeError: crypto.createHmac is not a function类型错误:crypto.createHmac 不是函数
【发布时间】:2022-10-04 21:07:48
【问题描述】:

我一直在尝试运行集成测试,但遇到以下错误:

2022-03-26T18:51:12.446Z cypress:network:agent got family { family: 4, href: 'https://wepapi.com/api/session-status' }
  1) "before all" hook for "should login"
  0 passing (345ms)
  1 failing
  1) Login
       "before all" hook for "should login":
     TypeError: crypto.createHmac is not a function
Because this error occurred during a `before all` hook we are skipping all of the remaining tests.

我们正在使用 Crypto 来生成 Auth Lambda 版本。

import Crypto from 'crypto';
import fs from 'fs';

export const getSha256FromFile = (filePath: string): string =>
Crypto
    .createHash('sha256')
    .update(fs.readFileSync(filePath))
    .digest('hex')

在授权 Lambda 文件中:

const codeSha256 = getSha256FromFile(AUTH_LAMBDA_CODE_FILE);

    this.version = authLambda.addVersion(
      `AuthLambdaVersion_${codeSha256}`,
      codeSha256

我什至没有使用 crypto.createHmac 函数。不知道发生了什么。

更新

我刚刚意识到我们正在使用AWS4 Sign functionality 来签署请求。有点像这样:

aws4.sign(requestOptions, {
  secretAccessKey: "<your-secret-access-key>",
  accessKeyId: "<your-access-key-id>",
  sessionToken: "<your-session-token>"
})

并且签名功能使用加密功能。这会导致此错误。但我仍然不确定如何解决它

【问题讨论】:

    标签: node.js typescript cryptography


    【解决方案1】:

    导入crypto 模块的正确方法如下:

    import * as crypto from "crypto"
    

    或者你可以只导入函数

    import { createHmac } from "crypto" 
    

    【讨论】:

      猜你喜欢
      • 2020-06-20
      • 2016-02-26
      • 2013-04-01
      • 2016-03-06
      • 2018-04-28
      • 2019-02-14
      • 2021-10-02
      • 2021-10-30
      相关资源
      最近更新 更多