MCPcopy Create free account
hub / github.com/cryptii/cryptii / validatePlugboardValue

Method validatePlugboardValue

src/Encoder/Enigma.js:812–839  ·  view source on GitHub ↗

* Validates plugboard setting value. * @protected * @param {Chain} rawValue * @param {Setting} setting * @return {boolean} Returns true, if value is valid.

(rawValue, setting)

Source from the content-addressed store, hash-verified

810 * @return {boolean} Returns true, if value is valid.
811 */
812 validatePlugboardValue (rawValue, setting) {
813 const plugboard = rawValue.getString()
814
815 // Empty plugboard is valid
816 if (plugboard === '') {
817 return true
818 }
819
820 // Check format (ab cd ef)
821 if (plugboard.match(/^([a-z]{2}\s)*([a-z]{2})$/) === null) {
822 return {
823 key: 'enigmaPlugboardInvalidFormat',
824 message:
825 'Invalid plugboard format: pairs of letters to be swapped ' +
826 'expected (e.g. \'ab cd ef\')'
827 }
828 }
829
830 // Check if character pairs are unique
831 if (!ArrayUtil.isUnique(plugboard.replace(/\s/g, '').split(''))) {
832 return {
833 key: 'enigmaPlugboardPairsNotUnique',
834 message: 'Pairs of letters to be swapped need to be unique'
835 }
836 }
837
838 return true
839 }
840
841 /**
842 * Converts plugboard value to rotor wiring string.

Callers

nothing calls this directly

Calls 4

getStringMethod · 0.80
matchMethod · 0.80
isUniqueMethod · 0.80
splitMethod · 0.80

Tested by

no test coverage detected