Skip to content

Commit

Permalink
Use enums for format in literal packet
Browse files Browse the repository at this point in the history
  • Loading branch information
larabr committed Nov 17, 2021
1 parent 9630a77 commit 5a600d7
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 18 deletions.
19 changes: 12 additions & 7 deletions openpgp.d.ts
Expand Up @@ -185,8 +185,8 @@ export function decryptSessionKeys<T extends MaybeStream<Data>>(options: { messa
export function readMessage<T extends MaybeStream<string>>(options: { armoredMessage: T, config?: PartialConfig }): Promise<Message<T>>;
export function readMessage<T extends MaybeStream<Uint8Array>>(options: { binaryMessage: T, config?: PartialConfig }): Promise<Message<T>>;

export function createMessage<T extends MaybeStream<string>>(options: { text: T, filename?: string, date?: Date, type?: DataPacketType }): Promise<Message<T>>;
export function createMessage<T extends MaybeStream<Uint8Array>>(options: { binary: T, filename?: string, date?: Date, type?: DataPacketType }): Promise<Message<T>>;
export function createMessage<T extends MaybeStream<string>>(options: { text: T, filename?: string, date?: Date, format?: enums.literalFormatNames }): Promise<Message<T>>;
export function createMessage<T extends MaybeStream<Uint8Array>>(options: { binary: T, filename?: string, date?: Date, format?: enums.literalFormatNames }): Promise<Message<T>>;

export function encrypt<T extends MaybeStream<Data>>(options: EncryptOptions & { message: Message<T>, format?: 'armored' }): Promise<
T extends WebStream<infer X> ? WebStream<string> :
Expand Down Expand Up @@ -438,8 +438,8 @@ export class LiteralDataPacket extends BasePacket {
static readonly tag: enums.packet.literalData;
private getText(clone?: boolean): MaybeStream<string>;
private getBytes(clone?: boolean): MaybeStream<Uint8Array>;
private setText(text: MaybeStream<string>, format?: DataPacketType);
private setBytes(bytes: MaybeStream<Uint8Array>, format?: DataPacketType);
private setText(text: MaybeStream<string>, format?: enums.literal);
private setBytes(bytes: MaybeStream<Uint8Array>, format: enums.literal);
private setFilename(filename: string);
private getFilename(): string;
private writeHeader(): Uint8Array;
Expand Down Expand Up @@ -534,8 +534,6 @@ export type AnyPacket = BasePacket;
export type AnySecretKeyPacket = SecretKeyPacket | SecretSubkeyPacket;
export type AnyKeyPacket = BasePublicKeyPacket;

type DataPacketType = 'utf8' | 'binary' | 'text' | 'mime';

type AllowedPackets = Map<enums.packet, object>; // mapping to Packet classes (i.e. typeof LiteralDataPacket etc.)
export class PacketList<T extends AnyPacket> extends Array<T> {
static fromBinary(bytes: MaybeStream<Uint8Array>, allowedPackets: AllowedPackets, config?: Config): PacketList<AnyPacket>; // the packet types depend on`allowedPackets`
Expand Down Expand Up @@ -639,7 +637,6 @@ interface SignOptions {
message: CleartextMessage | Message<MaybeStream<Data>>;
signingKeys?: MaybeArray<PrivateKey>;
format?: 'armored' | 'binary' | 'object';
dataType?: DataPacketType;
detached?: boolean;
signingKeyIDs?: MaybeArray<KeyID>;
date?: Date;
Expand Down Expand Up @@ -885,4 +882,12 @@ export namespace enums {
ocb = 2,
experimentalGCM = 100 // Private algorithm
}

export type literalFormatNames = 'utf8' | 'binary' | 'text' | 'mime'
enum literal {
binary = 98,
text = 116,
utf8 = 117,
mime = 109
}
}
4 changes: 2 additions & 2 deletions src/message.js
Expand Up @@ -862,9 +862,9 @@ export async function createMessage({ text, binary, filename, date = new Date(),
}
const literalDataPacket = new LiteralDataPacket(date);
if (text !== undefined) {
literalDataPacket.setText(input, format);
literalDataPacket.setText(input, enums.write(enums.literal, format));
} else {
literalDataPacket.setBytes(input, format);
literalDataPacket.setBytes(input, enums.write(enums.literal, format));
}
if (filename !== undefined) {
literalDataPacket.setFilename(filename);
Expand Down
12 changes: 6 additions & 6 deletions src/packet/literal_data.js
Expand Up @@ -35,7 +35,7 @@ class LiteralDataPacket {
* @param {Date} date - The creation date of the literal package
*/
constructor(date = new Date()) {
this.format = 'utf8'; // default format for literal data packets
this.format = enums.literal.utf8; // default format for literal data packets
this.date = util.normalizeDate(date);
this.text = null; // textual data representation
this.data = null; // literal data representation
Expand All @@ -46,9 +46,9 @@ class LiteralDataPacket {
* Set the packet data to a javascript native string, end of line
* will be normalized to \r\n and by default text is converted to UTF8
* @param {String | ReadableStream<String>} text - Any native javascript string
* @param {utf8|binary|text|mime} [format] - The format of the string of bytes
* @param {enums.literal} [format] - The format of the string of bytes
*/
setText(text, format = 'utf8') {
setText(text, format = enums.literal.utf8) {
this.format = format;
this.text = text;
this.data = null;
Expand All @@ -70,7 +70,7 @@ class LiteralDataPacket {
/**
* Set the packet data to value represented by the provided string of bytes.
* @param {Uint8Array | ReadableStream<Uint8Array>} bytes - The string of bytes
* @param {utf8|binary|text|mime} format - The format of the string of bytes
* @param {enums.literal} format - The format of the string of bytes
*/
setBytes(bytes, format) {
this.format = format;
Expand Down Expand Up @@ -123,7 +123,7 @@ class LiteralDataPacket {
async read(bytes) {
await stream.parse(bytes, async reader => {
// - A one-octet field that describes how the data is formatted.
const format = enums.read(enums.literal, await reader.readByte());
const format = await reader.readByte(); // enums.literal

const filename_len = await reader.readByte();
this.filename = util.decodeUTF8(await reader.readBytes(filename_len));
Expand All @@ -145,7 +145,7 @@ class LiteralDataPacket {
const filename = util.encodeUTF8(this.filename);
const filename_length = new Uint8Array([filename.length]);

const format = new Uint8Array([enums.write(enums.literal, this.format)]);
const format = new Uint8Array([this.format]);
const date = util.writeDate(this.date);

return util.concatUint8Array([format, filename_length, filename, date]);
Expand Down
4 changes: 2 additions & 2 deletions test/general/openpgp.js
Expand Up @@ -3481,7 +3481,7 @@ aOU=
}).then(async function (message) {
const literals = message.packets.filterByTag(openpgp.enums.packet.literalData);
expect(literals.length).to.equal(1);
expect(literals[0].format).to.equal('binary');
expect(literals[0].format).to.equal(openpgp.enums.literal.binary);
expect(+literals[0].date).to.equal(+future);
const signatures = await message.verify([publicKey_2038_2045], future, undefined, openpgp.config);
expect(await stream.readToEnd(message.getLiteralData())).to.deep.equal(data);
Expand Down Expand Up @@ -3510,7 +3510,7 @@ aOU=
}).then(async function (message) {
const literals = message.packets.filterByTag(openpgp.enums.packet.literalData);
expect(literals.length).to.equal(1);
expect(literals[0].format).to.equal('mime');
expect(literals[0].format).to.equal(openpgp.enums.literal.mime);
expect(+literals[0].date).to.equal(+future);
const signatures = await message.verify([publicKey_2038_2045], future, undefined, openpgp.config);
expect(await stream.readToEnd(message.getLiteralData())).to.deep.equal(data);
Expand Down
2 changes: 1 addition & 1 deletion test/typescript/definitions.ts
Expand Up @@ -68,7 +68,7 @@ import {

// Encrypt text message (armored)
const text = 'hello';
const textMessage = await createMessage({ text: 'hello' });
const textMessage = await createMessage({ text: 'hello', format: 'text' });
const encryptedArmor: string = await encrypt({ encryptionKeys: publicKeys, message: textMessage });
expect(encryptedArmor).to.include('-----BEGIN PGP MESSAGE-----');

Expand Down

0 comments on commit 5a600d7

Please sign in to comment.