Sha256: 0496c7521c92397ce006fcd1ff7d6680108a5d058b6c07fd1074241d70ea2469

Contents?: true

Size: 1.33 KB

Versions: 24

Compression:

Stored size: 1.33 KB

Contents

(define-module (atbash-cipher)
  #:autoload (srfi srfi-26) (cut)
  #:autoload (srfi srfi-1) (iota)
  #:autoload (ice-9 regex) (regexp-substitute/global
                            list-matches)
  #:export (encode decode))

;; "zip" a...z and z...a together to create an alist of mappings.
(define alphamap
  (let* ((alpha (map integer->char (iota 26 (char->integer #\a)))))
    (map cons alpha (reverse alpha))))

;; Look up the given character in the alphamap and return its complement.
(define (convert-char chr)
  (or (assoc-ref alphamap chr) chr))

;; Encode the given "val" string using the Atbash cipher.
(define (encode val)
  (format-atbash (string-map convert-char (prepare-input val))))

;; Decode the given Atbash cipher.
(define (decode val)
  (string-map convert-char (prepare-input val)))

;; Filter any unprocessable characters out of the plain text.
;; Only lowercase a-z and 0-9 are allowed.
(define (prepare-input val)
  (regexp-substitute/global #f "[^a-z0-9]" (string-downcase val) 'pre "" 'post))

;; Format atbash ciphertext for output.
;; Add a space every 5 characters
;; Currently generates a preceeding space, which must be trimmed :(
(define (format-atbash val)
  (string-trim
   (string-concatenate
    (map (compose (cut string-append " " <>)
                  match:substring)
         (list-matches "[a-z0-9]{1,5}" val)))))


Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/scheme/exercises/atbash-cipher/example.scm
trackler-2.2.1.179 tracks/scheme/exercises/atbash-cipher/example.scm
trackler-2.2.1.178 tracks/scheme/exercises/atbash-cipher/example.scm
trackler-2.2.1.177 tracks/scheme/exercises/atbash-cipher/example.scm
trackler-2.2.1.176 tracks/scheme/exercises/atbash-cipher/example.scm
trackler-2.2.1.175 tracks/scheme/exercises/atbash-cipher/example.scm
trackler-2.2.1.174 tracks/scheme/exercises/atbash-cipher/example.scm
trackler-2.2.1.173 tracks/scheme/exercises/atbash-cipher/example.scm
trackler-2.2.1.172 tracks/scheme/exercises/atbash-cipher/example.scm
trackler-2.2.1.171 tracks/scheme/exercises/atbash-cipher/example.scm
trackler-2.2.1.170 tracks/scheme/exercises/atbash-cipher/example.scm
trackler-2.2.1.169 tracks/scheme/exercises/atbash-cipher/example.scm
trackler-2.2.1.167 tracks/scheme/exercises/atbash-cipher/example.scm
trackler-2.2.1.166 tracks/scheme/exercises/atbash-cipher/example.scm
trackler-2.2.1.165 tracks/scheme/exercises/atbash-cipher/example.scm
trackler-2.2.1.164 tracks/scheme/exercises/atbash-cipher/example.scm
trackler-2.2.1.163 tracks/scheme/exercises/atbash-cipher/example.scm
trackler-2.2.1.162 tracks/scheme/exercises/atbash-cipher/example.scm
trackler-2.2.1.161 tracks/scheme/exercises/atbash-cipher/example.scm
trackler-2.2.1.160 tracks/scheme/exercises/atbash-cipher/example.scm