Skip to content

Commit 8ffd7aa

Browse files
committed
Remove import cycles
1 parent 94868e6 commit 8ffd7aa

6 files changed

Lines changed: 41 additions & 37 deletions

File tree

src/crypto/cipher/getCipher.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import * as cipher from '.';
2+
import enums from '../../enums';
3+
4+
/**
5+
* Get implementation of the given cipher
6+
* @param {enums.symmetric} algo
7+
* @returns {Object}
8+
* @throws {Error} on invalid algo
9+
*/
10+
export default function getCipher(algo) {
11+
const algoName = enums.read(enums.symmetric, algo);
12+
return cipher[algoName];
13+
}

src/crypto/crypto.js

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@
2525
*/
2626

2727
import publicKey from './public_key';
28-
import * as cipher from './cipher';
2928
import mode from './mode';
3029
import { getRandomBytes } from './random';
30+
import getCipher from './cipher/getCipher';
3131
import ECDHSymkey from '../type/ecdh_symkey';
3232
import KDFParams from '../type/kdf_params';
3333
import enums from '../enums';
@@ -385,16 +385,7 @@ export function getAEADMode(algo) {
385385
return mode[algoName];
386386
}
387387

388-
/**
389-
* Get implementation of the given cipher
390-
* @param {enums.symmetric} algo
391-
* @returns {Object}
392-
* @throws {Error} on invalid algo
393-
*/
394-
export function getCipher(algo) {
395-
const algoName = enums.read(enums.symmetric, algo);
396-
return cipher[algoName];
397-
}
388+
export { getCipher };
398389

399390
/**
400391
* Check whether the given curve OID is supported

src/crypto/mode/cfb.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@
2424

2525
import { AES_CFB } from '@openpgp/asmcrypto.js/dist_es8/aes/cfb';
2626
import * as stream from '@openpgp/web-stream-tools';
27-
import { getCipher } from '../crypto';
28-
import * as cipher from '../cipher';
27+
import getCipher from '../cipher/getCipher';
2928
import util from '../../util';
3029
import enums from '../../enums';
3130

@@ -62,7 +61,8 @@ export async function encrypt(algo, key, plaintext, iv, config) {
6261
return aesEncrypt(algo, key, plaintext, iv, config);
6362
}
6463

65-
const cipherfn = new cipher[algoName](key);
64+
const Cipher = getCipher(algo);
65+
const cipherfn = new Cipher(key);
6666
const block_size = cipherfn.blockSize;
6767

6868
const blockc = iv.slice();
@@ -104,7 +104,8 @@ export async function decrypt(algo, key, ciphertext, iv) {
104104
return aesDecrypt(algo, key, ciphertext, iv);
105105
}
106106

107-
const cipherfn = new cipher[algoName](key);
107+
const Cipher = getCipher(algo);
108+
const cipherfn = new Cipher(key);
108109
const block_size = cipherfn.blockSize;
109110

110111
let blockp = iv;

src/crypto/public_key/elliptic/ecdh.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import util from '../../../util';
3131
import { b64ToUint8Array } from '../../../encoding/base64';
3232
import * as pkcs5 from '../../pkcs5';
3333
import { keyFromPublic, keyFromPrivate, getIndutnyCurve } from './indutnyKey';
34-
import { getCipher } from '../../crypto';
34+
import getCipher from '../../cipher/getCipher';
3535

3636
const webCrypto = util.getWebCrypto();
3737
const nodeCrypto = util.getNodeCrypto();

src/key/factory.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import {
2626
UserAttributePacket
2727
} from '../packet';
2828
import PrivateKey from './private_key';
29-
import { createKey } from './key';
29+
import PublicKey from './public_key';
3030
import * as helper from './helper';
3131
import enums from '../enums';
3232
import util from '../util';
@@ -44,6 +44,25 @@ const allowedKeyPackets = /*#__PURE__*/ util.constructAllowedPackets([
4444
SignaturePacket
4545
]);
4646

47+
/**
48+
* Creates a PublicKey or PrivateKey depending on the packetlist in input
49+
* @param {PacketList} - packets to parse
50+
* @return {Key} parsed key
51+
* @throws if no key packet was found
52+
*/
53+
function createKey(packetlist) {
54+
for (const packet of packetlist) {
55+
switch (packet.constructor.tag) {
56+
case enums.packet.secretKey:
57+
return new PrivateKey(packetlist);
58+
case enums.packet.publicKey:
59+
return new PublicKey(packetlist);
60+
}
61+
}
62+
throw new Error('No key packet found');
63+
}
64+
65+
4766
/**
4867
* Generates a new OpenPGP key. Supports RSA and ECC keys.
4968
* By default, primary and subkeys will be of same type.

src/key/key.js

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ import util from '../util';
2626
import User from './user';
2727
import Subkey from './subkey';
2828
import * as helper from './helper';
29-
import PrivateKey from './private_key';
30-
import PublicKey from './public_key';
3129
import { UnparseablePacket } from '../packet/packet';
3230

3331
// A key revocation certificate can contain the following packets
@@ -710,21 +708,3 @@ class Key {
710708
});
711709

712710
export default Key;
713-
714-
/**
715-
* Creates a PublicKey or PrivateKey depending on the packetlist in input
716-
* @param {PacketList} - packets to parse
717-
* @return {Key} parsed key
718-
* @throws if no key packet was found
719-
*/
720-
export function createKey(packetlist) {
721-
for (const packet of packetlist) {
722-
switch (packet.constructor.tag) {
723-
case enums.packet.secretKey:
724-
return new PrivateKey(packetlist);
725-
case enums.packet.publicKey:
726-
return new PublicKey(packetlist);
727-
}
728-
}
729-
throw new Error('No key packet found');
730-
}

0 commit comments

Comments
 (0)