Verifying stream with detached signature fails to validate #1271
-
I'm trying to pass in a I wrote 2 tests that can be added to // Based off `Detached sign small message` test (line:639)
it('Detached sign/verify small string stream message', async function() {
dataArrived(); // Do not wait until data arrived.
const dataSign = new ReadableStream({
async start(controller) {
controller.enqueue(util.strToUint8Array('hello '));
controller.enqueue(util.strToUint8Array('world'));
controller.close();
}
});
const dataVerify = openpgp.stream.clone(dataSign)
const signed = await openpgp.sign({
message: openpgp.Message.fromBinary(dataSign),
privateKeys: privKey,
detached: true,
streaming: expectedType
});
expect(openpgp.stream.isStream(signed)).to.equal(expectedType);
const armoredSignature = await openpgp.stream.readToEnd(signed);
const signature = await openpgp.readSignature({ armoredSignature });
const verified = await openpgp.verify({
signature,
publicKeys: pubKey,
message: openpgp.Message.fromBinary(dataVerify)
});
const dataString = await openpgp.stream.readToEnd(verified.data);
expect(dataString).to.equal('hello world');
expect(verified.signatures).to.exist.and.have.length(1);
expect(await verified.signatures[0].verified).to.be.true;
});
it('Detached sign/verify small binary stream message', async function() {
dataArrived(); // Do not wait until data arrived.
const dataSign = new ReadableStream({
async start(controller) {
controller.enqueue(new Uint8Array([0x01, 0x02, 0x03]));
controller.close();
}
});
const dataVerify = openpgp.stream.clone(dataSign)
const signed = await openpgp.sign({
message: openpgp.Message.fromBinary(dataSign),
privateKeys: privKey,
detached: true,
streaming: expectedType
});
expect(openpgp.stream.isStream(signed)).to.equal(expectedType);
const armoredSignature = await openpgp.stream.readToEnd(signed);
const signature = await openpgp.readSignature({ armoredSignature });
const verified = await openpgp.verify({
signature,
publicKeys: pubKey,
message: openpgp.Message.fromBinary(dataVerify),
format: 'binary'
});
console.log(verified.signatures)
const dataString = await openpgp.stream.readToEnd(verified.data);
console.log(verified.signatures)
expect(dataString).to.deep.equal(new Uint8Array([0x01, 0x02, 0x03]));
expect(verified.signatures).to.exist.and.have.length(1);
expect(await verified.signatures[0].verified).to.be.true;
}); Update 1: Add in use of |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 6 replies
-
Hi 👋 A stream's contents can only be read once. If you want it to be able to be read again, you can use |
Beta Was this translation helpful? Give feedback.
Hi 👋
A stream's contents can only be read once. If you want it to be able to be read again, you can use
openpgp.stream.clone
(which keeps an extra copy of the data in memory).