首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ECMAScript 8,异步等待,语法错误javascript

ECMAScript 8,异步等待,语法错误javascript
EN

Stack Overflow用户
提问于 2021-11-26 09:23:05
回答 2查看 115关注 0票数 0

在函数的链中使用多个async()会破坏我的函数。有什么方法可以将Key2pkcs8()包含在generateKey()中吗?

代码语言:javascript
复制
async function generateKey() {
  let getKeyPair = await crypto.subtle.generateKey(
    {
      name: "ECDH",
      namedCurve: "P-384"
    },
    false,
    ["deriveKey"]
  );

  let PriKey = async() => {
    let PriKey = await getKeyPair.privateKey;
    console.log("pri = " + PriKey);
    return PriKey;
  };
  let PubKey = async() => {
    let PubKey = await getKeyPair.publicKey;
    console.log("pub = " + PubKey);
  };

  let Key2pkcs8 = async(PriKey, PubKey) => {
    let Key2pkcs8Pub = await crypto.subtle.exportKey("pkcs8", PubKey);
    let Key2pkcs8Pri = await crypto.subtle.exportKey("pkcs8", PriKey);
    return pkcs8keyarray = [Key2pkcs8Pub, Key2pkcs8Pri];
  
  return Keyarray = [PriKey(), PubKey()];  // i want to put <return pkcs8keyarray()> here  
};

generateKey().then(Key2pkcs8 => console.log(Key2pkcs8[0], Key2pkcs8[1]));按预期工作,并返回pri = [object CryptoKey] Promise { <state>: "fulfilled", <value>: undefined } Promise { <state>: "fulfilled", <value>: CryptoKey }

但是当使用return pkcs8keyarray()而不是return Keyarray = [PriKey(), PubKey()];时,它会中断并返回未定义的

我本打算让key2pkcs2将一个键作为变量(公钥或私钥),然后在类似示例的数组中返回这两个变量。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-11-26 19:11:43

您的程序演示了对承诺、异步/等待、密码模块和整个javascript的误解。

  • 仅在要将值重新分配到绑定时才使用let
  • 不要将函数重新分配到值,特别是在很容易避免的情况下
  • return Keyarray = ...这样的语句正在泄漏全局变量,并且不像您所期望的那样运行。
  • 不需要每次想要创建另一个异步值时都创建一个新的async函数。
  • 您不能简单地console.log私钥或公钥。根据exportKey文档,它返回一个解析为ArrayBuffer的承诺,它是原始字节数据,没有字符串表示。
代码语言:javascript
复制
async function generateKey() {
  const {privateKey, publicKey} =        // <- get privateKey, publicKey
    await crypto.subtle.generateKey(
      {
        name: "ECDH",
        namedCurve: "P-384"
      },
      true,
      ["deriveKey"]
    )

  return [
    await crypto.subtle.exportKey("pkcs8", privateKey), // <- export private
    await crypto.subtle.exportKey("pkcs8", publicKey),  // <- export public
  ]
}

由于generateKey正在返回数组对[private, public],所以我们可以很容易地在您编写的其他异步函数中使用-

代码语言:javascript
复制
async function myfunction() {
  const [private, public] = await generateKey() // <- resolves pair
  // do something
}

.then处理程序中的所有副作用移到下游。调用者对catch的错误负责。总是处理错误-

代码语言:javascript
复制
myfunction().then(console.log).catch(console.error)

如果您不了解这些简单的事情,请不要,而不是,尝试实现加密解决方案。你会100%地犯错误,你会引入一个漏洞,你和你的用户将承受后果。

这个答案中的代码是未经测试的,只是为了突出我很容易看到的错误。不要逐字使用代码,也不要期望它直接复制/粘贴到项目中。我不明白你的要求或意图是什么,因此不可能提出建议或提供其他建议。

有关滥用asyncawait的更多信息,请参见与此相关的问答

票数 2
EN

Stack Overflow用户

发布于 2021-11-26 10:31:28

代码语言:javascript
复制
async function generateKey() {
  let keyPair = await crypto.subtle.generateKey(
    {
      name: "ECDH",
      namedCurve: "P-384"
    },
    false,
    ["deriveKey"]
  );

  let PriKey = (keyPair) => {
    let PriKey = keyPair.privateKey;
    console.log("pri = " + PriKey);
    return keyPair;
  };
  let PubKey = (keyPair) => {
    let PubKey = keyPair.publicKey;
    console.log("pub = " + PubKey);
    return keyPair;
  };

  let Key2pkcs8 = async(keyPair) => {
    console.log("key = " + keyPair);
    let Key2pkcs8 = await crypto.subtle.exportKey("pkcs8", keyPair);
    return Key2pkcs8;
  };

  let printme = async() => {
    let printme = await Key2pkcs8(PubKey());
    console.log(printme);
    return printme;
  };

  return printme();
}

用法:

代码语言:javascript
复制
generateKey().then(Key2pkcs8 => console.log(Key2pkcs8 ));

如果您的外部生平是异步的,await将已经将异步流转换为同步,这意味着在调用其他函数时,getKeyPair (或我的代码段中的keyPair )已经可用。

PubKeyPriKey不需要是异步的,在这两种情况下,您都不会返回任何内容。Key2pkcs8也不会返回任何内容,那么您到底希望await Key2pkcs8(PubKey());返回什么呢?理想情况下,如果希望得到的Promise解析为undefined以外的东西,那么所有这些函数都应该返回一些内容。让上面的片段试一试,我没有测试它。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70122081

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档