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

Method performTranslate

src/Encoder/Enigma.js:581–749  ·  view source on GitHub ↗

* Performs encode or decode on given content. * @protected * @param {Chain} content * @param {boolean} isEncode True for encoding, false for decoding * @return {Chain}

(content, isEncode)

Source from the content-addressed store, hash-verified

579 * @return {Chain}
580 */
581 performTranslate (content, isEncode) {
582 const includeForeignChars = this.getSettingValue('includeForeignChars')
583 const model = EnigmaEncoder.getModel(this.getSettingValue('model'))
584 let i = 0
585
586 // Collect slots, rotors, positions and rings for current translation
587 const slots = model.slots
588 const slotCount = slots.length
589 const slotRotating = slots.map(slot => slot.rotating !== false)
590 const rotors = []
591 const positions = []
592 const rings = []
593 const stepRotors = new Array(slotCount)
594
595 for (i = 0; i < slotCount; i++) {
596 const name = this.getSettingValue(`rotor${i + 1}`)
597 rotors.push(EnigmaEncoder.getRotor(name))
598 positions.push(this.getSettingValue(`position${i + 1}`) - 1)
599 rings.push(this.getSettingValue(`ring${i + 1}`) - 1)
600 }
601
602 // Retrieve entry config
603 const entryRotor = EnigmaEncoder.getRotor(model.entryRotor)
604 const entryPosition = 0
605 const entryRing = 0
606
607 // Retrieve reflector config
608 const reflectorRotating = model.reflectorRotating === true
609 const reflectorRotor =
610 EnigmaEncoder.getRotor(this.getSettingValue('reflector'))
611 let reflectorPosition = model.reflectorThumbwheel
612 ? this.getSettingValue('positionReflector')
613 : 0
614 const reflectorRing = model.reflectorThumbwheel
615 ? this.getSettingValue('ringReflector')
616 : 0
617 let stepReflector
618
619 // Compose plugboard wiring, if it is available for this model
620 let plugboard = null
621 if (model.plugboard) {
622 const plugboardSetting = this.getSettingValue('plugboard')
623 plugboard = this.wiringFromPlugboardValue(plugboardSetting.toString())
624 }
625
626 // Use a short 'map' alias to call the static rotorMapChar method
627 const map = EnigmaEncoder.rotorMapChar
628
629 // Go through each content code point
630 let encodedCodePoints = content.getCodePoints().map((codePoint, index) => {
631 let char = null
632
633 if (codePoint >= 65 && codePoint <= 90) {
634 // Read uppercase character
635 char = codePoint - 65
636 } else if (codePoint >= 97 && codePoint <= 122) {
637 // Read lowercase character
638 char = codePoint - 97

Callers

nothing calls this directly

Calls 7

getSettingValueMethod · 0.80
getRotorMethod · 0.80
toStringMethod · 0.80
getCodePointsMethod · 0.80
rotorAtTurnoverMethod · 0.80
getModelMethod · 0.45

Tested by

no test coverage detected