\ xor.fs \ Not standalone! Runs from fybb.fs via 'included'. \ Simple byte-chain encryption \ Version 2025-08-18 Copyright Gan Uesli Starling \ En/de-encrypt sub-array like for PostScript Type 1 fonts $AA VALUE xor_seed \ Initial XOR key $00 VALUE xor_byte \ Migratory XOR key : xor.init xor_seed TO xor_byte ; \ Encrypts. \ Works also with hop.up or hop.dn from hop.fs... \ ...after first DO ing xor.int : en.xor.byte ( addr -- ) DUP >R C@ DUP >R xor_byte XOR R> xor_seed XOR TO xor_byte R> C! ; \ Unencrypts the above. \ If used with words from hop.fs, traverse... \ ...array same direction for both. So either... \ ...hop.up for both, or hop.dn for both. Do not... \ ...forget TO xor.init beforehand. : de.xor.byte ( addr -- ) DUP >R C@ xor_byte XOR DUP xor_seed XOR TO xor_byte R> C! ; \ Encrypt sub-array in upward traversal. : en.xor.up ( -- ) xor.init Nth+1 0th DO I en.xor.byte LOOP ; \ Decrypt sub-array in upward traversal. : de.xor.up ( -- ) xor.init Nth+1 0th DO I de.xor.byte LOOP ; \ Encrypt sub-array in DO wnward traversal. : en.xor.dn ( -- ) xor.init 0th Nth DO I en.xor.byte -1 +LOOP ; \ Decrypt sub-array in DO wnward traversal. : de.xor.dn ( -- ) xor.init 0th Nth DO I de.xor.byte -1 +LOOP ; \ TESTING WORDS BELOW FALSE [IF] \ Make true TO run tests. Uncomment specific test. : test.xor.up PAGE x.init.bytes CR CR ." Test of en.xor.up & de.xor.up..." CR CR ." Sub-array before (empty): " x.show.bytes $AA TO xor_seed en.xor.up CR CR ." Sub-array after XOR-ing upwards: " x.show.bytes $AA TO xor_seed de.xor.up CR CR ." Sub-array after XOR-ing upwards again TO undo: " x.show.bytes CR ." Done." CR ; \ test.xor.up : test.xor.dn PAGE x.init.bytes CR CR ." Test of en.xor.dn & de.xor.dn ..." CR CR ." Sub-array before (empty): " x.show.bytes $AA TO xor_seed en.xor.dn CR CR ." Sub-array after XOR-ing DO wnwards: " x.show.bytes $AA TO xor_seed de.xor.dn CR CR ." Sub-array after XOR-ing DO wnwards again TO undo: " x.show.bytes CR ." Done." CR ; \ test.xor.dn [THEN] \ End of file