diff --git a/src/cleartext.js b/src/cleartext.js index 40a290f4..dc50601d 100644 --- a/src/cleartext.js +++ b/src/cleartext.js @@ -209,3 +209,12 @@ function verifyHeaders(headers, packetlist) { throw new Error('Hash algorithm mismatch in armor header and signature'); } } + +/** + * Creates a new CleartextMessage object from text + * @param {String} text + * @static + */ +export function fromText(text) { + return new CleartextMessage(text); +} diff --git a/test/general/brainpool.js b/test/general/brainpool.js index 7120b96d..65d0ccec 100644 --- a/test/general/brainpool.js +++ b/test/general/brainpool.js @@ -173,7 +173,7 @@ describe('Brainpool Cryptography', function () { }); it('Sign message', async function () { const romeoPrivate = await load_priv_key('romeo'); - const signed = await openpgp.sign({privateKeys: [romeoPrivate], message: new openpgp.cleartext.CleartextMessage(data.romeo.message)}); + const signed = await openpgp.sign({privateKeys: [romeoPrivate], message: openpgp.cleartext.fromText(data.romeo.message)}); const romeoPublic = await load_pub_key('romeo'); const msg = await openpgp.cleartext.readArmored(signed.data); const result = await openpgp.verify({publicKeys: [romeoPublic], message: msg}); @@ -227,7 +227,7 @@ describe('Brainpool Cryptography', function () { return Promise.all([ // Signing message openpgp.sign( - { message: new openpgp.cleartext.CleartextMessage(testData), privateKeys: hi } + { message: openpgp.cleartext.fromText(testData), privateKeys: hi } ).then(async signed => { const msg = await openpgp.cleartext.readArmored(signed.data); // Verifying signed message @@ -237,7 +237,7 @@ describe('Brainpool Cryptography', function () { ).then(output => expect(output.signatures[0].valid).to.be.true), // Verifying detached signature openpgp.verify( - { message: new openpgp.cleartext.CleartextMessage(testData), + { message: openpgp.cleartext.fromText(testData), publicKeys: pubHi, signature: await openpgp.signature.readArmored(signed.data) } ).then(output => expect(output.signatures[0].valid).to.be.true) diff --git a/test/general/ecc_nist.js b/test/general/ecc_nist.js index 94cd9d60..19680625 100644 --- a/test/general/ecc_nist.js +++ b/test/general/ecc_nist.js @@ -185,7 +185,7 @@ describe('Elliptic Curve Cryptography', function () { }); it('Sign message', async function () { const romeoPrivate = await load_priv_key('romeo'); - const signed = await openpgp.sign({privateKeys: [romeoPrivate], message: new openpgp.cleartext.CleartextMessage(data.romeo.message)}); + const signed = await openpgp.sign({privateKeys: [romeoPrivate], message: openpgp.cleartext.fromText(data.romeo.message)}); const romeoPublic = await load_pub_key('romeo'); const msg = await openpgp.cleartext.readArmored(signed.data); const result = await openpgp.verify({publicKeys: [romeoPublic], message: msg}); @@ -254,7 +254,7 @@ describe('Elliptic Curve Cryptography', function () { // Signing message openpgp.sign( - { message: new openpgp.cleartext.CleartextMessage(testData), privateKeys: hi } + { message: openpgp.cleartext.fromText(testData), privateKeys: hi } ).then(async signed => { const msg = await openpgp.cleartext.readArmored(signed.data); // Verifying signed message @@ -264,7 +264,7 @@ describe('Elliptic Curve Cryptography', function () { ).then(output => expect(output.signatures[0].valid).to.be.true), // Verifying detached signature openpgp.verify( - { message: new openpgp.cleartext.CleartextMessage(testData), + { message: openpgp.cleartext.fromText(testData), publicKeys: pubHi, signature: await openpgp.signature.readArmored(signed.data) } ).then(output => expect(output.signatures[0].valid).to.be.true) diff --git a/test/general/key.js b/test/general/key.js index 5cd45f5d..c79e6c1e 100644 --- a/test/general/key.js +++ b/test/general/key.js @@ -1993,7 +1993,7 @@ VYGdb3eNlV8CfoEC privateKey.users[0].userId.parse('Test User '); // Set second user to prefer aes128. We will select this user. privateKey.users[1].selfCertifications[0].preferredHashAlgorithms = [openpgp.enums.hash.sha512]; - const signed = await openpgp.sign({message: new openpgp.cleartext.CleartextMessage('hello'), privateKeys: privateKey, fromUserId: {name: 'Test McTestington', email: 'test@example.com'}, armor: false}); + const signed = await openpgp.sign({message: openpgp.cleartext.fromText('hello'), privateKeys: privateKey, fromUserId: {name: 'Test McTestington', email: 'test@example.com'}, armor: false}); expect(signed.message.signature.packets[0].hashAlgorithm).to.equal(openpgp.enums.hash.sha512); const encrypted = await openpgp.encrypt({message: openpgp.message.fromText('hello'), publicKeys: publicKey, privateKeys: privateKey, fromUserId: {name: 'Test McTestington', email: 'test@example.com'}, detached: true, armor: false}); expect(encrypted.signature.packets[0].hashAlgorithm).to.equal(openpgp.enums.hash.sha512); @@ -2064,7 +2064,7 @@ VYGdb3eNlV8CfoEC expect(newKey.users.length).to.equal(1); expect(newKey.users[0].userId.userid).to.equal(userId); expect(newKey.isDecrypted()).to.be.true; - return openpgp.sign({message: new openpgp.cleartext.CleartextMessage('hello'), privateKeys: newKey, armor: true}).then(async function(signed) { + return openpgp.sign({message: openpgp.cleartext.fromText('hello'), privateKeys: newKey, armor: true}).then(async function(signed) { return openpgp.verify( {message: await openpgp.cleartext.readArmored(signed.data), publicKeys: newKey.toPublic()} ).then(async function(verified) { diff --git a/test/general/openpgp.js b/test/general/openpgp.js index 7b1c6174..0d26c7fa 100644 --- a/test/general/openpgp.js +++ b/test/general/openpgp.js @@ -1538,7 +1538,7 @@ describe('OpenPGP.js public api tests', function() { }); it('should sign and verify cleartext data', function () { - const message = new openpgp.cleartext.CleartextMessage(plaintext); + const message = openpgp.cleartext.fromText(plaintext); const signOpt = { message, privateKeys: privateKey.keys @@ -1563,7 +1563,7 @@ describe('OpenPGP.js public api tests', function() { const privKeyDE = (await openpgp.key.readArmored(priv_key_de)).keys[0]; await privKeyDE.decrypt(passphrase); - const message = new openpgp.cleartext.CleartextMessage(plaintext); + const message = openpgp.cleartext.fromText(plaintext); const signOpt = { message, privateKeys: [privateKey.keys[0], privKeyDE] @@ -1590,7 +1590,7 @@ describe('OpenPGP.js public api tests', function() { }); it('should sign and verify cleartext data with detached signatures', function () { - const message = new openpgp.cleartext.CleartextMessage(plaintext); + const message = openpgp.cleartext.fromText(plaintext); const signOpt = { message, privateKeys: privateKey.keys, @@ -1613,7 +1613,7 @@ describe('OpenPGP.js public api tests', function() { }); it('should sign and fail to verify cleartext data with wrong public pgp key', async function () { - const message = new openpgp.cleartext.CleartextMessage(plaintext); + const message = openpgp.cleartext.fromText(plaintext); const signOpt = { message, privateKeys: privateKey.keys @@ -1634,7 +1634,7 @@ describe('OpenPGP.js public api tests', function() { }); it('should sign and fail to verify cleartext data with wrong public pgp key with detached signature', async function () { - const message = new openpgp.cleartext.CleartextMessage(plaintext); + const message = openpgp.cleartext.fromText(plaintext); const signOpt = { message, privateKeys: privateKey.keys, @@ -1657,7 +1657,7 @@ describe('OpenPGP.js public api tests', function() { }); it('should sign and verify cleartext data and not armor', function () { - const message = new openpgp.cleartext.CleartextMessage(plaintext); + const message = openpgp.cleartext.fromText(plaintext); const signOpt = { message, privateKeys: privateKey.keys, @@ -1680,7 +1680,7 @@ describe('OpenPGP.js public api tests', function() { it('should sign and verify cleartext data and not armor with detached signatures', function () { const start = openpgp.util.normalizeDate(); - const message = new openpgp.cleartext.CleartextMessage(plaintext); + const message = openpgp.cleartext.fromText(plaintext); const signOpt = { message, privateKeys: privateKey.keys, @@ -1706,7 +1706,7 @@ describe('OpenPGP.js public api tests', function() { }); it('should sign and verify cleartext data with a date in the past', function () { - const message = new openpgp.cleartext.CleartextMessage(plaintext); + const message = openpgp.cleartext.fromText(plaintext); const past = new Date(2000); const signOpt = { message, diff --git a/test/general/signature.js b/test/general/signature.js index 137c759d..8686274b 100644 --- a/test/general/signature.js +++ b/test/general/signature.js @@ -606,7 +606,7 @@ yYDnCgA= const privKey = (await openpgp.key.readArmored(priv_key_arm2)).keys[0]; await privKey.decrypt('hello world'); - return openpgp.sign({ privateKeys:[privKey], message: new openpgp.cleartext.CleartextMessage(plaintext) }).then(async function(signed) { + return openpgp.sign({ privateKeys:[privKey], message: openpgp.cleartext.fromText(plaintext) }).then(async function(signed) { const csMsg = await openpgp.cleartext.readArmored(signed.data); return openpgp.verify({ publicKeys:[pubKey], message:csMsg }); @@ -626,7 +626,7 @@ yYDnCgA= const privKey = (await openpgp.key.readArmored(priv_key_arm2)).keys[0]; await privKey.decrypt('hello world'); - return openpgp.sign({ privateKeys:[privKey], message: new openpgp.cleartext.CleartextMessage(plaintext) }).then(async function(signed) { + return openpgp.sign({ privateKeys:[privKey], message: openpgp.cleartext.fromText(plaintext) }).then(async function(signed) { const csMsg = await openpgp.cleartext.readArmored(signed.data); return openpgp.verify({ publicKeys:[pubKey], message:csMsg }); @@ -646,7 +646,7 @@ yYDnCgA= const privKey = (await openpgp.key.readArmored(priv_key_arm2)).keys[0]; await privKey.decrypt('hello world'); - return openpgp.sign({ privateKeys:[privKey], message: new openpgp.cleartext.CleartextMessage(plaintext) }).then(async function(signed) { + return openpgp.sign({ privateKeys:[privKey], message: openpgp.cleartext.fromText(plaintext) }).then(async function(signed) { const csMsg = await openpgp.cleartext.readArmored(signed.data); return openpgp.verify({ publicKeys:[pubKey], message:csMsg }); diff --git a/test/general/x25519.js b/test/general/x25519.js index 6f0c1e8f..201085fa 100644 --- a/test/general/x25519.js +++ b/test/general/x25519.js @@ -172,7 +172,7 @@ describe('X25519 Cryptography', function () { const name = 'light'; const randomData = input.createSomeMessage(); const priv = await load_priv_key(name); - const signed = await openpgp.sign({ privateKeys: [priv], message: new openpgp.cleartext.CleartextMessage(randomData)}); + const signed = await openpgp.sign({ privateKeys: [priv], message: openpgp.cleartext.fromText(randomData)}); const pub = await load_pub_key(name); const msg = await openpgp.cleartext.readArmored(signed.data); const result = await openpgp.verify({ publicKeys: [pub], message: msg}); @@ -274,7 +274,7 @@ describe('X25519 Cryptography', function () { }), // Signing message openpgp.sign( - { message: new openpgp.cleartext.CleartextMessage('Hi, this is me, Hi!'), privateKeys: hi } + { message: openpgp.cleartext.fromText('Hi, this is me, Hi!'), privateKeys: hi } ).then(async signed => { const msg = await openpgp.cleartext.readArmored(signed.data); // Verifying signed message