-
Notifications
You must be signed in to change notification settings - Fork 790
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
48 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,45 @@ | ||
import defaultConfig from '../../config'; | ||
import Argon2S2k, { Argon2OutOfMemoryError } from './argon2'; | ||
import OtherS2K from './generic'; | ||
import Argon2S2K, { Argon2OutOfMemoryError } from './argon2'; | ||
import GenericS2K from './generic'; | ||
import enums from '../../enums'; | ||
|
||
const allowedDefaultS2KTypes = new Set([enums.s2k.argon2, enums.s2k.iterated]); | ||
const allowedS2KTypesForEncryption = new Set([enums.s2k.argon2, enums.s2k.iterated]); | ||
|
||
/** | ||
* Instantiate a new S2K instance of the given type | ||
* @param {module:enums.s2k} [type] - If not specified, `config.s2kTypes` is used | ||
* @param {module:enums.s2k} type | ||
* @oaram {Object} [config] | ||
* @returns {Object} New s2k object | ||
* @throws {Error} for unknown or unsupported types | ||
*/ | ||
export function newS2KFromType(maybeType, config = defaultConfig) { | ||
if (!maybeType && !allowedDefaultS2KTypes.has(config.s2kType)) { | ||
throw new Error('The provided `config.s2kType` value is not allowed'); | ||
} | ||
|
||
const type = maybeType || config.s2kType; | ||
|
||
export function newS2KFromType(type, config = defaultConfig) { | ||
switch (type) { | ||
case enums.s2k.argon2: | ||
return new Argon2S2k(config); | ||
return new Argon2S2K(config); | ||
case enums.s2k.iterated: | ||
case enums.s2k.gnu: | ||
case enums.s2k.salted: | ||
case enums.s2k.simple: | ||
return new OtherS2K(type, config); | ||
return new GenericS2K(type, config); | ||
default: | ||
throw new Error(`Unsupported S2K type ${type}`); | ||
} | ||
} | ||
|
||
/** | ||
* Instantiate a new S2K instance based on the config settings | ||
* @oaram {Object} config | ||
* @returns {Object} New s2k object | ||
* @throws {Error} for unknown or unsupported types | ||
*/ | ||
export function newS2KFromConfig(config) { | ||
const { s2kType } = config; | ||
|
||
if (!allowedS2KTypesForEncryption.has(s2kType)) { | ||
throw new Error('The provided `config.s2kType` value is not allowed'); | ||
} | ||
|
||
return newS2KFromType(s2kType, config); | ||
} | ||
|
||
export { Argon2OutOfMemoryError }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters