如何在openpgp JS中将参数消息作为文本传递
Posted
技术标签:
【中文标题】如何在openpgp JS中将参数消息作为文本传递【英文标题】:How to pass parameter message as text in openpgp JS 【发布时间】:2021-09-16 08:45:16 【问题描述】:我正在尝试做的事情:
从容器 DIDE 中获取一些 Azure 存储 blob,并使用 RSA 2048 对其进行加密,然后将它们上传到另一个名为 encrypted-dide 的容器中 这些 blob 通过流下载(这里微软做得很好https://docs.microsoft.com/en-us/azure/storage/blobs/storage-quickstart-blobs-nodejs#upload-blobs-to-a-container)并由 func 重组。 streamToString(可读流) (我没有使用 openpgp JS 流,因为我不知道 Microsoft 流是否与 NodeJs 相同) 我的代码使用未加密的文本按预期工作,并在所谓的加密容器中上传 blob encrypted-dide 我已经关注了openpgp js的官方文档和一些互联网资源。 我得到的错误是 错误:参数 [message] 需要是 openpgp JS 中的 Message 类型publicKey 被硬编码在文件 keys.js 中,并像这样导出:
const publicKey = `-----BEGIN PGP PUBLIC KEY BLOCK-----
xsBNBGDgi3gBCADcZqIcczPDAx3+os5cCFVgaoT62Y+5rRvwPGPaPKKA1ajw
7NvoxS0RsJbqYAwNk0IEneoZvgSPpqkehGQCOBdsEhjcEgxVxaSlbbgPJkPh
avjTBDUhr6pIUc+MkSX7eh5LdkgWRSfzZdLXX2es5ycM5R1ZryzPTAenZh7D
l1g1x9TsyX+scI7gAtuyfbzAybYVqYMIvcHYZdIi8m6pGmxIqb0QW6sgO6nG
GyjVgxLDyMnHzYMInFRmKUV8XUUw9ECLZ6ioW4rthmpjoswh9vmP6vWI9OL/
Y7Zb3xY5XnIT6UFSpAHS5V/TNbEUD/EpoNtEI30bUl2X35UM277fUxetABEB
AAHNG0pvbiBTbWl0aCA8am9uQGV4YW1wbGUuY29tPsLAigQQAQgAHQUCYOCL
eAQLCQcIAxUICgQWAAIBAhkBAhsDAh4BACEJEGHAYnRSOf5GFiEExGMJvxnV
v1dXecI0YcBidFI5/kY5PAgAxL10QcUZIrxRXQIrqk04ZDhO4ehMirPqH/KT
L/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jywaZJEisO2aJcO2BaozSzYUmkr5sH2wjSKcMS
nLviCw==
=Wg0i
-----END PGP PUBLIC KEY BLOCK-----`
代码是:
const BlobServiceClient = require('@azure/storage-blob');
// const v1: uuidv1 = require('uuid');
// const stream = require('stream').promises
const openpgp = require('openpgp');
// import * as openpgp from 'openpgp'
const publicKey = require('./keys')
async function main()
const AZURE_STORAGE_CONNECTION_STRING = process.env.AZURE_STORAGE_CONNECTION_STRING;
const blobServiceClient = BlobServiceClient.fromConnectionString(AZURE_STORAGE_CONNECTION_STRING);
const containerClient = blobServiceClient.getContainerClient("uploadebs");
const containerEncryptedFiles = blobServiceClient.getContainerClient("encrypted-dide");
await containerEncryptedFiles.createIfNotExists("encrypted-dide")
// console.log(await openpgp.readKey( armoredKey: publicKey )) <- THIS WORKS!
for await (const blob of containerClient.listBlobsFlat())
if (blob.name.match('^DIDE*'))
const blockBlobClient = containerClient.getBlockBlobClient(blob.name);
const encryptedblockBlobClient = containerEncryptedFiles.getBlockBlobClient(blob.name)
blockBlobClient.download(0)
.then(downloadBlockBlobResponse => streamToString(downloadBlockBlobResponse.readableStreamBody))
.then(blobAsString => openpgp.encrypt(
message: openpgp.createMessage( text: blobAsString ), // input as Message object
publicKeys: openpgp.readKey( armoredKey: publicKey ),
))
// BELOW LINE, SENDS TEXT IN BLOBS, ENCRYPTED OR NOT THROUGH FUNC UPLOAD
.then(encrypted => encryptedblockBlobClient.upload(encrypted, encrypted.length))
async function streamToString(readableStream)
return new Promise((resolve, reject) =>
const chunks = [];
readableStream.on("data", (data) =>
chunks.push(data.toString());
);
readableStream.on("end", () =>
resolve(chunks.join(""));
);
readableStream.on("error", reject);
);
main().then(() => console.log('Done')).catch((ex) => console.log(ex.message));
【问题讨论】:
【参考方案1】:openpgp.createMessage
返回一个 Promise。所以你需要做.then
或者在它前面加上await
。
与penpgp.readKey
相同。这也是一个承诺。
例如来自Doc:
const publicKey = await openpgp.readKey( armoredKey: publicKeyArmored );
const encrypted = await openpgp.encrypt(
message: await openpgp.createMessage( text: 'Hello, World!' ), // input as Message object
publicKeys: publicKey, // for encryption
privateKeys: privateKey // for signing (optional)
);
EDIT2: 不使用等待。
.then(blobAsString =>
return Promise.all([openpgp.createMessage( text: blobAsString ), openpgp.readKey( armoredKey: publicKey )])
.then(([message, publicKeys ])=>
return openpgp.encrypt(
message,
publicKeys,
);
);
)
【讨论】:
为了控制流程,我已经在 then 链中。请让我知道您将如何整合您的回复,因为我还需要等待“blobAsString”结果 已编辑。你也可以在那里使用then
链。
我以前试过这样,并给出了语法错误:/,显然等待它不能在 then 块中使用
@arita-chakraborty 我试过这样 .then(blobAsString => Promise.all([openpgp.createMessage( text: blobAsString ), openpgp.readKey( ArmoredKey: publicKey ) ]) ) .then(v => console.log(v)) 但给了我 3???次未定义
似乎也非常接近我刚刚添加的内容。检查这个新的编辑@AB 我认为在你的解决方案中你没有返回加密函数【参考方案2】:
这样使用:
.then(blobAsString =>
return Promise.all([openpgp.createMessage( text: blobAsString ), openpgp.readKey( armoredKey: publicKey )])
.then(([message, publicKeys ])=>
return openpgp.encrypt(
message,
publicKeys,
)
)
.then(encrypted => encryptedblockBlobClient.upload(encrypted, encrypted.length));;
)
【讨论】:
鉴于您刚刚在您的解决方案中使用了代码,您能否接受其他答案。并在问题本身中添加此代码。以上是关于如何在openpgp JS中将参数消息作为文本传递的主要内容,如果未能解决你的问题,请参考以下文章
如何在 node.js 环境中将查询语句传递给 bigquery
如何在 s-s-rS 报告中将多个参数作为单独的多个页面传递?
如何在Visual Basic 2005中将文本框值作为参数传递给crystal报表