1

What I'm trying to do:

  • get some Azure storage blobs from container DIDE and encrypt them with RSA 2048 and upload them in other container called encrypted-dide These blobs are downloaded through a stream(here Microsoft did a good job https://learn.microsoft.com/en-us/azure/storage/blobs/storage-quickstart-blobs-nodejs#upload-blobs-to-a-container) and recomposed by func. streamToString(readableStream) (I'm not using openpgp JS streams as I don't know if Microsoft streams are the same with NodeJs ones)
  • My code works as expected with unecrypted text and upload blobs in the supposedly encryped container encrypted-dide
  • I have followed the official documentation of openpgp js and some Internet resources.
  • The error I am getting is Error: Parameter [message] needs to be of type Message in openpgp JS

the publicKey is harcoded in the file keys.js and and is exported like this:

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/MeHppHFqV06Fm4JDAOpGyh8lgleLwP4P9Lrod3AVSOKSX48u+UM/Bo4LtG
foAntS+tC9RjWlpR6PZ0aJA8SqHKLCnkaUvz7wv/M55fgGxeeQbhOLutNxN4
L8rCNhPo3UbWwoB+ifgQ9S4bv4kgyJxXYinyGYG0CD67YxQKxiAt58qjsdmK
x4uKCoFbHd1Oa4wfr6ezXet+2hCQvsf8eJV88+qL7TmpSe3ypiTWHNgxymNx
v77SlOkkzayJVWxrWtFU8ZoatlsfOP3A5tToio2rEhCHcnqYl4KtF1a0WUR8
KG+pJc7ATQRg4It4AQgA0Q2uZL9TIqGWtNzeAygdG0C3o+D+MoEYI/Qx0A6X
aZv7/1v84V++lRD0iuIMUlBgFEJWMsHF7cN1EMlUV1lRxOzyKTv+0FqyoSTr
bWexA+jG/Nb3Q8vSX1+eVHvm1+2H7AGhBH2szVmXeH15bGNaOaw03EmG5pCh
CIaCoXYUXKoavsa+C8827lGSuqLs1uRniCmIjQvkQSZg7a0IH6dpMIpxdHPh
h9Zyt8e74WwfmXW/be6cjWRI9FgBzl9U5EQEEVO1JdLvfzEEXkNthyAAhl+P
Z1oTR2PSs4ZUlYdb3MQrt7XoKeEOqCHHxoHB3gsj+75Jnc/aAbM+hb13imAJ
iwARAQABwsB2BBgBCAAJBQJg4It4AhsMACEJEGHAYnRSOf5GFiEExGMJvxnV
v1dXecI0YcBidFI5/kZYSQgAop0OsPV11O/fzbZ+oEabC3Ye9hNGapJQNdmJ
MJkiJg7Hnl1FO4MDtHK5OJ4YePFAqtlKRDIBCALPiN0E2v9+3yAafs6TQDc9
Lg3iIPDOnrXv7J7pv2WPnnue4o8Gkggpa+wEjbQJcUBLX311xJGBG4pSNIVN
FJcsl1fGoaxXB5ANPy/+UNMv0l/7cQWDzSw8V9WH10SO2Q4dQF7Zxw+UgBdb
mRVXWNHkcTs81WA/hYtAuLw0O5Q1QWfbXzlTJGNPy/lMMsxLF6La8fBPHlE0
CjYd4ZH9HgOvpCACjRtbc1jywaZJEisO2aJcO2BaozSzYUmkr5sH2wjSKcMS
nLviCw==
=Wg0i
-----END PGP PUBLIC KEY BLOCK-----`
  • The code is:
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));

2 Answers 2

1

openpgp.createMessage returns a Promise. So you need to do .then or add await before it.

Same with penpgp.readKey. It is also a promise.

For example from the 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: Without using await.

.then(blobAsString => {
    return Promise.all([openpgp.createMessage({ text: blobAsString }), openpgp.readKey({ armoredKey: publicKey })])
    .then(([message, publicKeys ])=>{
        return openpgp.encrypt({
            message,
            publicKeys,
        });
    });    
})
Sign up to request clarification or add additional context in comments.

6 Comments

I am already in a then chain in order to control the flow. Please let me know how you would integrate your response as I need also to wait for 'blobAsString' result
edited. you can use then chains in there as well.
I have tried like this before and gives a syntax error :/, apparently await it cannot be used in a then block
@arita-chakraborty I have tried like this .then(blobAsString => { Promise.all([openpgp.createMessage({ text: blobAsString }), openpgp.readKey({ armoredKey: publicKey })]) }) .then(v => console.log(v)) but gives me 3??? times undefined
seems to be very close to what I just added as well. check this new edit out @AB I think in ur solution u were not returning the encrypt function
|
0

Used like this:

            .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)});;    
            })

1 Comment

can u accept the other answer given that you just used the code in ur solution. and add this code in the question itself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.