GET 请求成功 returns 第一次解密 gist,但重启服务器后对象为空

GET request successfully returns decrypted gist first time, but empty object after restarting the server

使用 JavaScript 和 nacl 库获取 GitHub 要点和 return 解密的内容。所有 nacl 方法都接受和 return UINT8 数组,因此值得注意的是,密钥也是一个 32 个随机字节的 UINT8 数组。

   server.get('/fetchmessagefromself:id', (req, res) => {
      // TODO:  Retrieve and decrypt the secret gist corresponding to the given ID
      const id = req.query.id;
      github.gists.get({ id })
        .then((response) => {
          const gist = response.data;
          const file = Object.keys(gist.files);
          const box = gist.files[file].content;
          const nonce = nacl.util.decodeBase64(box.slice(-32));
          const ciphertext = nacl.util.decodeBase64(box.slice(0, -32));
          const text = nacl.secretbox.open(ciphertext, nonce, key);

          res.send(nacl.util.encodeUTF8(text));
        })
        .catch((err) => {
          res.json(err);
        });
    });

在我的 GitHub 帐户上使用单独的方法创建加密的要点后,上述方法第一次工作并成功检索解密的要点,但在重新启动服务器后该方法仅 return是一个空对象。我不明白为什么。

我明白了;重新启动服务器后它不起作用,因为我没有保留密钥——它只是一个 32 个随机无符号整数的数组——所以每次重新启动服务器时都会创建一个新密钥。

我将密钥保存在一个单独的 .env 文件中,该功能现在可以正常工作了!