收藏
回答

如何引用crypto,const crypto = require("crypto");报错?

确认安装了nodejs -v v14.16.0

在微信开发工具 const crypto = require("crypto");

报 "crypto.js" is not defined.

外部命令 node v3.js没有报错,但是开发工具const crypto = require("crypto");老是报错,请问如何解决?麻烦了。

const crypto = require('crypto');

function sha256(message, secret = '', encoding) {

    const hmac = crypto.createHmac('sha256', secret)

    return hmac.update(message).digest(encoding)

}

function getHash(message, encoding = 'hex') {

    const hash = crypto.createHash('sha256')

    return hash.update(message).digest(encoding)

}

function getDate(timestamp) {

    const date = new Date(timestamp * 1000)

    const year = date.getUTCFullYear()

    const month = ('0' + (date.getUTCMonth() + 1)).slice(-2)

    const day = ('0' + date.getUTCDate()).slice(-2)

    return `${year}-${month}-${day}`

}

function main(){

    // 密钥参数

    const SECRET_ID = "Aetet*"

    const SECRET_KEY = "sdfasdf"

    const endpoint = "iai.tencentcloudapi.com"

    const service = "iai"

    const region = "ap-beijing"

    const action = "DetectFace"

    const version = "2020-03-03"

    //const timestamp = getTime()

    const timestamp = Math.round(new Date().getTime()/1000) + "";

    //时间处理, 获取世界时间日期

    const date = getDate(timestamp)

    // ************* 步骤 1:拼接规范请求串 *************

    const signedHeaders = "content-type;host"

    const payload = "{\"Image\": \"\"}"

    const hashedRequestPayload = getHash(payload);

    const httpRequestMethod = "POST"

    const canonicalUri = "/"

    const canonicalQueryString = ""

    const canonicalHeaders = "content-type:application/json; charset=utf-8\n" + "host:" + endpoint + "\n"

    const canonicalRequest = httpRequestMethod + "\n"

        + canonicalUri + "\n"

        + canonicalQueryString + "\n"

        + canonicalHeaders + "\n"

        + signedHeaders + "\n"

        + hashedRequestPayload

    console.log(canonicalRequest)

    console.log("----------------------------")

    // ************* 步骤 2:拼接待签名字符串 *************

    const algorithm = "TC3-HMAC-SHA256"

    const hashedCanonicalRequest = getHash(canonicalRequest);

    const credentialScope = date + "/" + service + "/" + "tc3_request"

    const stringToSign = algorithm + "\n" +

        timestamp + "\n" +

        credentialScope + "\n" +

        hashedCanonicalRequest

    console.log(stringToSign)

    console.log("----------------------------")

    // ************* 步骤 3:计算签名 *************

    const kDate = sha256(date, 'TC3' + SECRET_KEY)

    const kService = sha256(service, kDate)

    const kSigning = sha256('tc3_request', kService)

    const signature = sha256(stringToSign, kSigning, 'hex')

    console.log(signature)

    console.log("----------------------------")

    // ************* 步骤 4:拼接 Authorization *************

    const authorization = algorithm + " " +

        "Credential=" + SECRET_ID + "/" + credentialScope + ", " +

        "SignedHeaders=" + signedHeaders + ", " +

        "Signature=" + signature

    console.log(authorization)

    console.log("----------------------------")

    const Call_Information = 'curl -X POST ' + "https://" + endpoint

        + ' -H "Authorization: ' + authorization + '"'

        + ' -H "Content-Type: application/json; charset=utf-8"'

        + ' -H "Host: ' + endpoint + '"'

        + ' -H "X-TC-Action: ' + action + '"'

        + ' -H "X-TC-Timestamp: ' + timestamp.toString() + '"'

        + ' -H "X-TC-Version: ' + version + '"'

        + ' -H "X-TC-Region: ' + region + '"'

        + " -d '" + payload + "'"

    console.log(Call_Information)

}

main()

回答关注问题邀请回答
收藏

1 个回答

  • Jsnewbie
    Jsnewbie
    2022-06-15

    解决了吗?我也遇到了,死活引入不了

    2022-06-15
    有用
    回复
登录 后发表内容
问题标签