Skip to content

Commit

Permalink
Use ES6 classes
Browse files Browse the repository at this point in the history
  • Loading branch information
twiss committed Feb 9, 2021
1 parent fb3d234 commit aea7aa9
Show file tree
Hide file tree
Showing 39 changed files with 4,878 additions and 4,868 deletions.
202 changes: 101 additions & 101 deletions src/cleartext.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,117 +32,117 @@ import { Signature } from './signature';
import { createVerificationObjects, createSignaturePackets } from './message';

/**
* @class
* @classdesc Class that represents an OpenPGP cleartext signed message.
* Class that represents an OpenPGP cleartext signed message.
* See {@link https://tools.ietf.org/html/rfc4880#section-7}
* @param {String} text The cleartext of the signed message
* @param {module:signature.Signature} signature The detached signature or an empty signature for unsigned messages
*/
export function CleartextMessage(text, signature) {
if (!(this instanceof CleartextMessage)) {
return new CleartextMessage(text, signature);
}
// normalize EOL to canonical form <CR><LF>
this.text = util.removeTrailingSpaces(text).replace(/\r?\n/g, '\r\n');
if (signature && !(signature instanceof Signature)) {
throw new Error('Invalid signature input');
export class CleartextMessage {
/**
* @param {String} text The cleartext of the signed message
* @param {module:signature.Signature} signature The detached signature or an empty signature for unsigned messages
*/
constructor(text, signature) {
// normalize EOL to canonical form <CR><LF>
this.text = util.removeTrailingSpaces(text).replace(/\r?\n/g, '\r\n');
if (signature && !(signature instanceof Signature)) {
throw new Error('Invalid signature input');
}
this.signature = signature || new Signature(new PacketList());
}
this.signature = signature || new Signature(new PacketList());
}

/**
* Returns the key IDs of the keys that signed the cleartext message
* @returns {Array<module:type/keyid>} array of keyid objects
*/
CleartextMessage.prototype.getSigningKeyIds = function() {
const keyIds = [];
const signatureList = this.signature.packets;
signatureList.forEach(function(packet) {
keyIds.push(packet.issuerKeyId);
});
return keyIds;
};

/**
* Sign the cleartext message
* @param {Array<module:key.Key>} privateKeys private keys with decrypted secret key data for signing
* @param {Signature} signature (optional) any existing detached signature
* @param {Date} date (optional) The creation time of the signature that should be created
* @param {Array} userIds (optional) user IDs to sign with, e.g. [{ name:'Steve Sender', email:'steve@openpgp.org' }]
* @returns {Promise<module:cleartext.CleartextMessage>} new cleartext message with signed content
* @async
*/
CleartextMessage.prototype.sign = async function(privateKeys, signature = null, date = new Date(), userIds = []) {
return new CleartextMessage(this.text, await this.signDetached(privateKeys, signature, date, userIds));
};
/**
* Returns the key IDs of the keys that signed the cleartext message
* @returns {Array<module:type/keyid>} array of keyid objects
*/
getSigningKeyIds() {
const keyIds = [];
const signatureList = this.signature.packets;
signatureList.forEach(function(packet) {
keyIds.push(packet.issuerKeyId);
});
return keyIds;
}

/**
* Sign the cleartext message
* @param {Array<module:key.Key>} privateKeys private keys with decrypted secret key data for signing
* @param {Signature} signature (optional) any existing detached signature
* @param {Date} date (optional) The creation time of the signature that should be created
* @param {Array} userIds (optional) user IDs to sign with, e.g. [{ name:'Steve Sender', email:'steve@openpgp.org' }]
* @returns {Promise<module:signature.Signature>} new detached signature of message content
* @async
*/
CleartextMessage.prototype.signDetached = async function(privateKeys, signature = null, date = new Date(), userIds = []) {
const literalDataPacket = new LiteralDataPacket();
literalDataPacket.setText(this.text);
/**
* Sign the cleartext message
* @param {Array<module:key.Key>} privateKeys private keys with decrypted secret key data for signing
* @param {Signature} signature (optional) any existing detached signature
* @param {Date} date (optional) The creation time of the signature that should be created
* @param {Array} userIds (optional) user IDs to sign with, e.g. [{ name:'Steve Sender', email:'steve@openpgp.org' }]
* @returns {Promise<module:cleartext.CleartextMessage>} new cleartext message with signed content
* @async
*/
async sign(privateKeys, signature = null, date = new Date(), userIds = []) {
return new CleartextMessage(this.text, await this.signDetached(privateKeys, signature, date, userIds));
}

return new Signature(await createSignaturePackets(literalDataPacket, privateKeys, signature, date, userIds, true));
};
/**
* Sign the cleartext message
* @param {Array<module:key.Key>} privateKeys private keys with decrypted secret key data for signing
* @param {Signature} signature (optional) any existing detached signature
* @param {Date} date (optional) The creation time of the signature that should be created
* @param {Array} userIds (optional) user IDs to sign with, e.g. [{ name:'Steve Sender', email:'steve@openpgp.org' }]
* @returns {Promise<module:signature.Signature>} new detached signature of message content
* @async
*/
async signDetached(privateKeys, signature = null, date = new Date(), userIds = []) {
const literalDataPacket = new LiteralDataPacket();
literalDataPacket.setText(this.text);

return new Signature(await createSignaturePackets(literalDataPacket, privateKeys, signature, date, userIds, true));
}

/**
* Verify signatures of cleartext signed message
* @param {Array<module:key.Key>} keys array of keys to verify signatures
* @param {Date} date (optional) Verify the signature against the given date, i.e. check signature creation time < date < expiration time
* @returns {Promise<Array<{keyid: module:type/keyid, valid: Boolean}>>} list of signer's keyid and validity of signature
* @async
*/
CleartextMessage.prototype.verify = function(keys, date = new Date()) {
return this.verifyDetached(this.signature, keys, date);
};
/**
* Verify signatures of cleartext signed message
* @param {Array<module:key.Key>} keys array of keys to verify signatures
* @param {Date} date (optional) Verify the signature against the given date, i.e. check signature creation time < date < expiration time
* @returns {Promise<Array<{keyid: module:type/keyid, valid: Boolean}>>} list of signer's keyid and validity of signature
* @async
*/
verify(keys, date = new Date()) {
return this.verifyDetached(this.signature, keys, date);
}

/**
* Verify signatures of cleartext signed message
* @param {Array<module:key.Key>} keys array of keys to verify signatures
* @param {Date} date (optional) Verify the signature against the given date, i.e. check signature creation time < date < expiration time
* @returns {Promise<Array<{keyid: module:type/keyid, valid: Boolean}>>} list of signer's keyid and validity of signature
* @async
*/
CleartextMessage.prototype.verifyDetached = function(signature, keys, date = new Date()) {
const signatureList = signature.packets;
const literalDataPacket = new LiteralDataPacket();
// we assume that cleartext signature is generated based on UTF8 cleartext
literalDataPacket.setText(this.text);
return createVerificationObjects(signatureList, [literalDataPacket], keys, date, true);
};
/**
* Verify signatures of cleartext signed message
* @param {Array<module:key.Key>} keys array of keys to verify signatures
* @param {Date} date (optional) Verify the signature against the given date, i.e. check signature creation time < date < expiration time
* @returns {Promise<Array<{keyid: module:type/keyid, valid: Boolean}>>} list of signer's keyid and validity of signature
* @async
*/
verifyDetached(signature, keys, date = new Date()) {
const signatureList = signature.packets;
const literalDataPacket = new LiteralDataPacket();
// we assume that cleartext signature is generated based on UTF8 cleartext
literalDataPacket.setText(this.text);
return createVerificationObjects(signatureList, [literalDataPacket], keys, date, true);
}

/**
* Get cleartext
* @returns {String} cleartext of message
*/
CleartextMessage.prototype.getText = function() {
// normalize end of line to \n
return this.text.replace(/\r\n/g, '\n');
};
/**
* Get cleartext
* @returns {String} cleartext of message
*/
getText() {
// normalize end of line to \n
return this.text.replace(/\r\n/g, '\n');
}

/**
* Returns ASCII armored text of cleartext signed message
* @returns {String | ReadableStream<String>} ASCII armor
*/
CleartextMessage.prototype.armor = function() {
let hashes = this.signature.packets.map(function(packet) {
return enums.read(enums.hash, packet.hashAlgorithm).toUpperCase();
});
hashes = hashes.filter(function(item, i, ar) { return ar.indexOf(item) === i; });
const body = {
hash: hashes.join(),
text: this.text,
data: this.signature.packets.write()
};
return armor.encode(enums.armor.signed, body);
};
/**
* Returns ASCII armored text of cleartext signed message
* @returns {String | ReadableStream<String>} ASCII armor
*/
armor() {
let hashes = this.signature.packets.map(function(packet) {
return enums.read(enums.hash, packet.hashAlgorithm).toUpperCase();
});
hashes = hashes.filter(function(item, i, ar) { return ar.indexOf(item) === i; });
const body = {
hash: hashes.join(),
text: this.text,
data: this.signature.packets.write()
};
return armor.encode(enums.armor.signed, body);
}
}


/**
Expand Down
45 changes: 22 additions & 23 deletions src/config/localStorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,30 @@

/**
* This object is used for storing and retrieving configuration from HTML5 local storage.
* @constructor
*/
function LocalStorage() {}

/**
* Reads the config out of the HTML5 local storage
* and initializes the object config.
* if config is null the default config will be used
*/
LocalStorage.prototype.read = function () {
const raw = globalThis.localStorage.getItem("config");
const cf = (raw === null ? null : JSON.parse(raw));
if (cf === null) {
this.config = this.default_config;
this.write();
} else {
this.config = cf;
class LocalStorage {
/**
* Reads the config out of the HTML5 local storage
* and initializes the object config.
* if config is null the default config will be used
*/
read() {
const raw = globalThis.localStorage.getItem("config");
const cf = (raw === null ? null : JSON.parse(raw));
if (cf === null) {
this.config = this.default_config;
this.write();
} else {
this.config = cf;
}
}
};

/**
* Writes the config to HTML5 local storage
*/
LocalStorage.prototype.write = function () {
globalThis.localStorage.setItem("config", JSON.stringify(this.config));
};
/**
* Writes the config to HTML5 local storage
*/
write() {
globalThis.localStorage.setItem("config", JSON.stringify(this.config));
}
}

export default LocalStorage;
Loading

0 comments on commit aea7aa9

Please sign in to comment.