refactor: add Config type instead of defaultConfig

This commit is contained in:
Julia Krüger 2024-01-09 12:54:32 +01:00
parent 2983d225cc
commit 888f149f12
No known key found for this signature in database
GPG Key ID: B8738B09D5E94DEB
3 changed files with 17 additions and 14 deletions

View File

@ -2,6 +2,8 @@ import defaultConfig from '../../config';
import enums from '../../enums'; import enums from '../../enums';
import util from '../../util'; import util from '../../util';
import crypto from '../../crypto'; import crypto from '../../crypto';
import type { default as loadArgonWasmModuleType } from 'argon2id';
import { Config } from '../../../openpgp';
const ARGON2_TYPE = 0x02; // id const ARGON2_TYPE = 0x02; // id
const ARGON2_VERSION = 0x13; const ARGON2_VERSION = 0x13;
@ -20,21 +22,21 @@ export class Argon2OutOfMemoryError extends Error {
} }
// cache argon wasm module // cache argon wasm module
let loadArgonWasmModule: Function; let loadArgonWasmModule: typeof loadArgonWasmModuleType;
let argon2Promise: Promise<Function>; let argon2Promise: ReturnType<typeof loadArgonWasmModuleType>;
// reload wasm module above this treshold, to deallocated used memory // reload wasm module above this treshold, to deallocated used memory
const ARGON2_WASM_MEMORY_THRESHOLD_RELOAD = 2 << 19; const ARGON2_WASM_MEMORY_THRESHOLD_RELOAD = 2 << 19;
class Argon2S2K { class Argon2S2K {
type: string; type: 'argon2';
salt: Uint8Array | null; private salt: Uint8Array | null;
t: number; private t: number;
p: number; private p: number;
encodedM: number; private encodedM: number;
/** /**
* @param {Object} [config] - Full configuration, defaults to openpgp.config * @param {Object} [config] - Full configuration, defaults to openpgp.config
*/ */
constructor(config = defaultConfig) { constructor(config: Config) {
const { passes, parallelism, memoryExponent } = config.s2kArgon2Params; const { passes, parallelism, memoryExponent } = config.s2kArgon2Params;

View File

@ -27,9 +27,10 @@
* @module type/s2k * @module type/s2k
*/ */
import defaultConfig from '../../config'; import type { Config, enums as enumsType } from '../../../openpgp';
import crypto from '../../crypto';
import enums from '../../enums'; import enums from '../../enums';
import crypto from '../../crypto';
import { UnsupportedError } from '../../packet/packet'; import { UnsupportedError } from '../../packet/packet';
import util from '../../util'; import util from '../../util';
@ -41,7 +42,7 @@ class GenericS2K {
/** /**
* @param {Object} [config] - Full configuration, defaults to openpgp.config * @param {Object} [config] - Full configuration, defaults to openpgp.config
*/ */
constructor(s2kType: number, config = defaultConfig) { constructor(s2kType: enumsType.s2k.simple | enumsType.s2k.salted | enumsType.s2k.iterated, config: Config) {
/** /**
* Hash function identifier, or 0 for gnu-dummy keys * Hash function identifier, or 0 for gnu-dummy keys
* @type {module:enums.hash | 0} * @type {module:enums.hash | 0}

View File

@ -1,9 +1,9 @@
import defaultConfig from '../../config';
import Argon2S2K, { Argon2OutOfMemoryError } from './argon2'; import Argon2S2K, { Argon2OutOfMemoryError } from './argon2';
import GenericS2K from './generic'; import GenericS2K from './generic';
import enums from '../../enums'; import enums from '../../enums';
import { UnsupportedError } from '../../packet/packet'; import { UnsupportedError } from '../../packet/packet';
import GnuS2K from './gnu'; import GnuS2K from './gnu';
import { Config } from '../../../openpgp';
const allowedS2KTypesForEncryption = new Set([enums.s2k.argon2, enums.s2k.iterated]); const allowedS2KTypesForEncryption = new Set([enums.s2k.argon2, enums.s2k.iterated]);
@ -14,7 +14,7 @@ const allowedS2KTypesForEncryption = new Set([enums.s2k.argon2, enums.s2k.iterat
* @returns {Object} New s2k object * @returns {Object} New s2k object
* @throws {Error} for unknown or unsupported types * @throws {Error} for unknown or unsupported types
*/ */
export function newS2KFromType (type: number, config = defaultConfig): Argon2S2K | GenericS2K | GnuS2K { export function newS2KFromType (type: number, config: Config): Argon2S2K | GenericS2K | GnuS2K {
switch (type) { switch (type) {
case enums.s2k.gnu: case enums.s2k.gnu:
return new GnuS2K(); return new GnuS2K();
@ -35,7 +35,7 @@ export function newS2KFromType (type: number, config = defaultConfig): Argon2S2K
* @returns {Object} New s2k object * @returns {Object} New s2k object
* @throws {Error} for unknown or unsupported types * @throws {Error} for unknown or unsupported types
*/ */
export function newS2KFromConfig(config = defaultConfig) { export function newS2KFromConfig(config: Config) {
const { s2kType } = config; const { s2kType } = config;
if (!allowedS2KTypesForEncryption.has(s2kType)) { if (!allowedS2KTypesForEncryption.has(s2kType)) {