Skip to content

Commit 83f0f45

Browse files
committed
Internal: drop unused util functions (close #147)
Many util helpers are unused as they have already been duplicated in the monorepo, and will be renamed or refactored there.
1 parent 79dfefd commit 83f0f45

File tree

5 files changed

+32
-68
lines changed

5 files changed

+32
-68
lines changed

lib/crypto/_md5.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,24 @@
1212
* warranty of any kind.
1313
*/
1414

15-
import { arrayToBinaryString, hexStringToArray } from '../utils';
15+
import { hexStringToArray } from '../utils';
16+
17+
/**
18+
* Encode an array of 8-bit integers as a string
19+
* @param bytes data to encode
20+
* @return string-encoded bytes
21+
*/
22+
const arrayToBinaryString = (bytes) => {
23+
const result = [];
24+
const bs = 1 << 14;
25+
const j = bytes.length;
26+
27+
for (let i = 0; i < j; i += bs) {
28+
// @ts-ignore Uint8Array treated as number[]
29+
result.push(String.fromCharCode.apply(String, bytes.subarray(i, i + bs < j ? i + bs : j)));
30+
}
31+
return result.join('');
32+
};
1633

1734
/* this function is much faster,
1835
so if possible we use it. Some IEs

lib/utils.ts

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,6 @@ import type { MaybeWebStream, WebStream } from './pmcrypto';
33

44
export type MaybeArray<T> = T | Array<T>;
55

6-
const ifDefined = <T, R>(cb: (input: T) => R) => <U extends T | undefined>(input: U) => {
7-
return (input !== undefined ? cb(input as T) : undefined) as U extends T ? R : undefined;
8-
};
9-
export const encodeUtf8 = ifDefined((input: string) => unescape(encodeURIComponent(input)));
10-
export const decodeUtf8 = ifDefined((input: string) => decodeURIComponent(escape(input)));
11-
export const encodeBase64 = ifDefined((input: string) => btoa(input).trim());
12-
export const decodeBase64 = ifDefined((input: string) => atob(input.trim()));
13-
export const encodeUtf8Base64 = ifDefined((input: string) => encodeBase64(encodeUtf8(input)));
14-
export const decodeUtf8Base64 = ifDefined((input: string) => decodeUtf8(decodeBase64(input)));
15-
166
/**
177
* Concatenate (flatten) Uint8Arrays
188
* @param arrays - Uint8Arrays to concatenate
@@ -44,40 +34,6 @@ const isString = (data: any): data is string | String => {
4434
return typeof data === 'string' || data instanceof String;
4535
};
4636

47-
/**
48-
* Convert a string to an array of 8-bit integers
49-
* @param str String to convert
50-
* @returns An array of 8-bit integers
51-
*/
52-
export const binaryStringToArray = (str: string) => {
53-
if (!isString(str)) {
54-
throw new Error('binaryStringToArray: Data must be in the form of a string');
55-
}
56-
57-
const result = new Uint8Array(str.length);
58-
for (let i = 0; i < str.length; i++) {
59-
result[i] = str.charCodeAt(i);
60-
}
61-
return result;
62-
};
63-
64-
/**
65-
* Encode an array of 8-bit integers as a string
66-
* @param bytes data to encode
67-
* @return string-encoded bytes
68-
*/
69-
export const arrayToBinaryString = (bytes: Uint8Array) => {
70-
const result = [];
71-
const bs = 1 << 14;
72-
const j = bytes.length;
73-
74-
for (let i = 0; i < j; i += bs) {
75-
// @ts-ignore Uint8Array treated as number[]
76-
result.push(String.fromCharCode.apply(String, bytes.subarray(i, i + bs < j ? i + bs : j)));
77-
}
78-
return result.join('');
79-
};
80-
8137
/**
8238
* Convert a hex string to an array of 8-bit integers
8339
* @param hex A hex string to convert

test/crypto/hash.spec.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
import { expect } from 'chai';
22
import { unsafeMD5, unsafeSHA1, SHA256, SHA512 } from '../../lib';
3-
import { arrayToHexString, binaryStringToArray } from '../../lib/utils';
3+
import { arrayToHexString, stringToUtf8Array } from '../../lib/utils';
44

55
describe('hash functions', () => {
66
it('md5 basic test', async () => {
77
const emptyHash = await unsafeMD5(new Uint8Array([])).then(arrayToHexString);
8-
const testHash = await unsafeMD5(binaryStringToArray('The quick brown fox jumps over the lazy dog')).then(arrayToHexString);
8+
const testHash = await unsafeMD5(stringToUtf8Array('The quick brown fox jumps over the lazy dog')).then(arrayToHexString);
99
expect(emptyHash).to.equal('d41d8cd98f00b204e9800998ecf8427e');
1010
expect(testHash).to.equal('9e107d9d372bb6826bd81d3542a419d6');
1111
});
1212

1313
it('sha1 basic test', async () => {
1414
const emptyHash = await unsafeSHA1(new Uint8Array([])).then(arrayToHexString);
15-
const testHash = await unsafeSHA1(binaryStringToArray('abc')).then(arrayToHexString);
15+
const testHash = await unsafeSHA1(stringToUtf8Array('abc')).then(arrayToHexString);
1616
expect(emptyHash).to.equal('da39a3ee5e6b4b0d3255bfef95601890afd80709');
1717
expect(testHash).to.equal('a9993e364706816aba3e25717850c26c9cd0d89d');
1818
});
@@ -26,7 +26,7 @@ describe('hash functions', () => {
2626
});
2727
const dataStreamTest = new ReadableStream<Uint8Array>({
2828
pull: (controller) => {
29-
const data = binaryStringToArray('abc');
29+
const data = stringToUtf8Array('abc');
3030
for (let i = 0; i < data.length; i++) {
3131
controller.enqueue(data.subarray(i, i + 1));
3232
}
@@ -40,21 +40,21 @@ describe('hash functions', () => {
4040
});
4141

4242
it('sha256 basic test', async () => {
43-
const emptyInput = binaryStringToArray('');
43+
const emptyInput = stringToUtf8Array('');
4444
const emptyDigest = await SHA256(emptyInput).then(arrayToHexString);
4545
expect(emptyDigest).to.equal('e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855');
4646

47-
const abcInput = binaryStringToArray('abc');
47+
const abcInput = stringToUtf8Array('abc');
4848
const abcDigest = await SHA256(abcInput).then(arrayToHexString);
4949
expect(abcDigest).to.equal('ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad');
5050
});
5151

5252
it('sha512 basic test', async () => {
53-
const emptyInput = binaryStringToArray('');
53+
const emptyInput = stringToUtf8Array('');
5454
const emptyDigest = await SHA512(emptyInput).then(arrayToHexString);
5555
expect(emptyDigest).to.equal('cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e');
5656

57-
const abcInput = binaryStringToArray('abc');
57+
const abcInput = stringToUtf8Array('abc');
5858
const abcDigest = await SHA512(abcInput).then(arrayToHexString);
5959
expect(abcDigest).to.equal('ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f');
6060
});

test/message/encryptMessage.spec.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { readToEnd, toStream, type WebStream } from '@openpgp/web-stream-tools';
33
import { config as globalConfig, type CompressedDataPacket, enums, SymEncryptedSessionKeyPacket, type PartialConfig, SymEncryptedIntegrityProtectedDataPacket } from '../../lib/openpgp';
44

55
import { decryptKey, readPrivateKey, verifyMessage, encryptMessage, decryptMessage, generateSessionKey, readSignature, readMessage, encryptSessionKey, decryptSessionKey } from '../../lib';
6-
import { hexStringToArray, arrayToBinaryString, stringToUtf8Array } from '../../lib/utils';
6+
import { hexStringToArray, stringToUtf8Array, utf8ArrayToString } from '../../lib/utils';
77
import { testPrivateKeyLegacy, v4PrivateKeySEIPDv2 } from './encryptMessage.data';
88
import { VERIFICATION_STATUS } from '../../lib/constants';
99

@@ -275,7 +275,7 @@ describe('message encryption and decryption', () => {
275275
verificationKeys: [decryptedPrivateKey.toPublic()],
276276
format: 'binary'
277277
});
278-
expect(arrayToBinaryString(await readToEnd(decrypted))).to.equal(inputData);
278+
expect(utf8ArrayToString(await readToEnd(decrypted))).to.equal(inputData);
279279
expect(verified).to.equal(VERIFICATION_STATUS.SIGNED_AND_VALID);
280280
});
281281

@@ -303,7 +303,7 @@ describe('message encryption and decryption', () => {
303303
verificationKeys: [decryptedPrivateKey.toPublic()],
304304
format: 'binary'
305305
});
306-
expect(arrayToBinaryString(await readToEnd(decrypted))).to.equal(inputData);
306+
expect(utf8ArrayToString(await readToEnd(decrypted))).to.equal(inputData);
307307
expect(verified).to.equal(VERIFICATION_STATUS.SIGNED_AND_VALID);
308308
});
309309

@@ -333,7 +333,7 @@ describe('message encryption and decryption', () => {
333333
verificationKeys: [decryptedPrivateKey.toPublic()],
334334
format: 'binary'
335335
});
336-
expect(arrayToBinaryString(await readToEnd(decrypted))).to.equal(inputData);
336+
expect(utf8ArrayToString(await readToEnd(decrypted))).to.equal(inputData);
337337
expect(await verified).to.equal(VERIFICATION_STATUS.SIGNED_AND_VALID);
338338
});
339339

@@ -359,7 +359,7 @@ describe('message encryption and decryption', () => {
359359
verificationKeys: [decryptedPrivateKey.toPublic()],
360360
format: 'binary'
361361
});
362-
expect(await readToEnd(decrypted).then(arrayToBinaryString)).to.equal(inputData);
362+
expect(utf8ArrayToString(await readToEnd(decrypted))).to.equal(inputData);
363363
expect(await verified).to.equal(VERIFICATION_STATUS.SIGNED_AND_VALID);
364364
});
365365

test/utils.spec.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { expect } from 'chai';
2-
// @ts-ignore missing isStream definitions
32
import { isStream, readToEnd } from '@openpgp/web-stream-tools';
4-
import { concatArrays, decodeBase64, encodeBase64, hexStringToArray, stringToUtf8Array, utf8ArrayToString } from '../lib/utils';
3+
import { concatArrays, hexStringToArray, stringToUtf8Array, utf8ArrayToString } from '../lib/utils';
54
import type { Data } from '../lib';
65

76
const streamFromChunks = <T extends Data>(chunks: T[]) => {
@@ -72,12 +71,4 @@ describe('utils', () => {
7271
expect(isStream(encoded)).to.not.be.false;
7372
expect(await readToEnd(encoded)).to.deep.equal(hexStringToArray('68656c6c6f20776f726c64'));
7473
});
75-
76-
it('encodeBase64 - it can correctly encode base 64', async () => {
77-
expect(encodeBase64('foo')).to.equal('Zm9v');
78-
});
79-
80-
it('decodeBase64 - it can correctly decode base 64', async () => {
81-
expect(decodeBase64('Zm9v')).to.equal('foo');
82-
});
8374
});

0 commit comments

Comments
 (0)