Add openpgp.cleartext.fromText

For symmetry with message.fromText
This commit is contained in:
Daniel Huigens 2018-07-17 16:22:13 +02:00
parent 52c4fa9639
commit b35b167e63
7 changed files with 30 additions and 21 deletions

View File

@ -209,3 +209,12 @@ function verifyHeaders(headers, packetlist) {
throw new Error('Hash algorithm mismatch in armor header and signature'); 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);
}

View File

@ -173,7 +173,7 @@ describe('Brainpool Cryptography', function () {
}); });
it('Sign message', async function () { it('Sign message', async function () {
const romeoPrivate = await load_priv_key('romeo'); 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 romeoPublic = await load_pub_key('romeo');
const msg = await openpgp.cleartext.readArmored(signed.data); const msg = await openpgp.cleartext.readArmored(signed.data);
const result = await openpgp.verify({publicKeys: [romeoPublic], message: msg}); const result = await openpgp.verify({publicKeys: [romeoPublic], message: msg});
@ -227,7 +227,7 @@ describe('Brainpool Cryptography', function () {
return Promise.all([ return Promise.all([
// Signing message // Signing message
openpgp.sign( openpgp.sign(
{ message: new openpgp.cleartext.CleartextMessage(testData), privateKeys: hi } { message: openpgp.cleartext.fromText(testData), privateKeys: hi }
).then(async signed => { ).then(async signed => {
const msg = await openpgp.cleartext.readArmored(signed.data); const msg = await openpgp.cleartext.readArmored(signed.data);
// Verifying signed message // Verifying signed message
@ -237,7 +237,7 @@ describe('Brainpool Cryptography', function () {
).then(output => expect(output.signatures[0].valid).to.be.true), ).then(output => expect(output.signatures[0].valid).to.be.true),
// Verifying detached signature // Verifying detached signature
openpgp.verify( openpgp.verify(
{ message: new openpgp.cleartext.CleartextMessage(testData), { message: openpgp.cleartext.fromText(testData),
publicKeys: pubHi, publicKeys: pubHi,
signature: await openpgp.signature.readArmored(signed.data) } signature: await openpgp.signature.readArmored(signed.data) }
).then(output => expect(output.signatures[0].valid).to.be.true) ).then(output => expect(output.signatures[0].valid).to.be.true)

View File

@ -185,7 +185,7 @@ describe('Elliptic Curve Cryptography', function () {
}); });
it('Sign message', async function () { it('Sign message', async function () {
const romeoPrivate = await load_priv_key('romeo'); 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 romeoPublic = await load_pub_key('romeo');
const msg = await openpgp.cleartext.readArmored(signed.data); const msg = await openpgp.cleartext.readArmored(signed.data);
const result = await openpgp.verify({publicKeys: [romeoPublic], message: msg}); const result = await openpgp.verify({publicKeys: [romeoPublic], message: msg});
@ -254,7 +254,7 @@ describe('Elliptic Curve Cryptography', function () {
// Signing message // Signing message
openpgp.sign( openpgp.sign(
{ message: new openpgp.cleartext.CleartextMessage(testData), privateKeys: hi } { message: openpgp.cleartext.fromText(testData), privateKeys: hi }
).then(async signed => { ).then(async signed => {
const msg = await openpgp.cleartext.readArmored(signed.data); const msg = await openpgp.cleartext.readArmored(signed.data);
// Verifying signed message // Verifying signed message
@ -264,7 +264,7 @@ describe('Elliptic Curve Cryptography', function () {
).then(output => expect(output.signatures[0].valid).to.be.true), ).then(output => expect(output.signatures[0].valid).to.be.true),
// Verifying detached signature // Verifying detached signature
openpgp.verify( openpgp.verify(
{ message: new openpgp.cleartext.CleartextMessage(testData), { message: openpgp.cleartext.fromText(testData),
publicKeys: pubHi, publicKeys: pubHi,
signature: await openpgp.signature.readArmored(signed.data) } signature: await openpgp.signature.readArmored(signed.data) }
).then(output => expect(output.signatures[0].valid).to.be.true) ).then(output => expect(output.signatures[0].valid).to.be.true)

View File

@ -1993,7 +1993,7 @@ VYGdb3eNlV8CfoEC
privateKey.users[0].userId.parse('Test User <b@c.com>'); privateKey.users[0].userId.parse('Test User <b@c.com>');
// Set second user to prefer aes128. We will select this user. // Set second user to prefer aes128. We will select this user.
privateKey.users[1].selfCertifications[0].preferredHashAlgorithms = [openpgp.enums.hash.sha512]; 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); 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}); 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); 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.length).to.equal(1);
expect(newKey.users[0].userId.userid).to.equal(userId); expect(newKey.users[0].userId.userid).to.equal(userId);
expect(newKey.isDecrypted()).to.be.true; 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( return openpgp.verify(
{message: await openpgp.cleartext.readArmored(signed.data), publicKeys: newKey.toPublic()} {message: await openpgp.cleartext.readArmored(signed.data), publicKeys: newKey.toPublic()}
).then(async function(verified) { ).then(async function(verified) {

View File

@ -1538,7 +1538,7 @@ describe('OpenPGP.js public api tests', function() {
}); });
it('should sign and verify cleartext data', function () { it('should sign and verify cleartext data', function () {
const message = new openpgp.cleartext.CleartextMessage(plaintext); const message = openpgp.cleartext.fromText(plaintext);
const signOpt = { const signOpt = {
message, message,
privateKeys: privateKey.keys 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]; const privKeyDE = (await openpgp.key.readArmored(priv_key_de)).keys[0];
await privKeyDE.decrypt(passphrase); await privKeyDE.decrypt(passphrase);
const message = new openpgp.cleartext.CleartextMessage(plaintext); const message = openpgp.cleartext.fromText(plaintext);
const signOpt = { const signOpt = {
message, message,
privateKeys: [privateKey.keys[0], privKeyDE] 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 () { 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 = { const signOpt = {
message, message,
privateKeys: privateKey.keys, 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 () { 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 = { const signOpt = {
message, message,
privateKeys: privateKey.keys 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 () { 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 = { const signOpt = {
message, message,
privateKeys: privateKey.keys, 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 () { 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 = { const signOpt = {
message, message,
privateKeys: privateKey.keys, 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 () { it('should sign and verify cleartext data and not armor with detached signatures', function () {
const start = openpgp.util.normalizeDate(); const start = openpgp.util.normalizeDate();
const message = new openpgp.cleartext.CleartextMessage(plaintext); const message = openpgp.cleartext.fromText(plaintext);
const signOpt = { const signOpt = {
message, message,
privateKeys: privateKey.keys, 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 () { 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 past = new Date(2000);
const signOpt = { const signOpt = {
message, message,

View File

@ -606,7 +606,7 @@ yYDnCgA=
const privKey = (await openpgp.key.readArmored(priv_key_arm2)).keys[0]; const privKey = (await openpgp.key.readArmored(priv_key_arm2)).keys[0];
await privKey.decrypt('hello world'); 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); const csMsg = await openpgp.cleartext.readArmored(signed.data);
return openpgp.verify({ publicKeys:[pubKey], message:csMsg }); return openpgp.verify({ publicKeys:[pubKey], message:csMsg });
@ -626,7 +626,7 @@ yYDnCgA=
const privKey = (await openpgp.key.readArmored(priv_key_arm2)).keys[0]; const privKey = (await openpgp.key.readArmored(priv_key_arm2)).keys[0];
await privKey.decrypt('hello world'); 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); const csMsg = await openpgp.cleartext.readArmored(signed.data);
return openpgp.verify({ publicKeys:[pubKey], message:csMsg }); return openpgp.verify({ publicKeys:[pubKey], message:csMsg });
@ -646,7 +646,7 @@ yYDnCgA=
const privKey = (await openpgp.key.readArmored(priv_key_arm2)).keys[0]; const privKey = (await openpgp.key.readArmored(priv_key_arm2)).keys[0];
await privKey.decrypt('hello world'); 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); const csMsg = await openpgp.cleartext.readArmored(signed.data);
return openpgp.verify({ publicKeys:[pubKey], message:csMsg }); return openpgp.verify({ publicKeys:[pubKey], message:csMsg });

View File

@ -172,7 +172,7 @@ describe('X25519 Cryptography', function () {
const name = 'light'; const name = 'light';
const randomData = input.createSomeMessage(); const randomData = input.createSomeMessage();
const priv = await load_priv_key(name); 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 pub = await load_pub_key(name);
const msg = await openpgp.cleartext.readArmored(signed.data); const msg = await openpgp.cleartext.readArmored(signed.data);
const result = await openpgp.verify({ publicKeys: [pub], message: msg}); const result = await openpgp.verify({ publicKeys: [pub], message: msg});
@ -274,7 +274,7 @@ describe('X25519 Cryptography', function () {
}), }),
// Signing message // Signing message
openpgp.sign( 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 => { ).then(async signed => {
const msg = await openpgp.cleartext.readArmored(signed.data); const msg = await openpgp.cleartext.readArmored(signed.data);
// Verifying signed message // Verifying signed message