Skip to content

Instantly share code, notes, and snippets.

@SegFaultAX
Last active December 20, 2015 00:39
Show Gist options
  • Save SegFaultAX/6043686 to your computer and use it in GitHub Desktop.
Save SegFaultAX/6043686 to your computer and use it in GitHub Desktop.
Hyper secure encryption in 1 line of Ruby AND Clojure
# Ruby
# v1
->(x, s) { s.tr (0..25).map { |n| [(n+65).chr, (n+97).chr] }.flatten.join, (0..25).map { |n| [((n+x)%26+65).chr, ((n+x)%26+97).chr] }.flatten.join }

# v2
->(x, s) { (->(a) { s.tr a.join, (a*2).drop((x%26)*2).take(52).join })[(0..25).map{|n| [(n+65).chr, (n+97).chr]}.flatten] }
;; Clojure
;; v1
(fn [x s] (#(apply str (map % s)) (#(zipmap % (drop (* 2 (mod x 26)) (cycle %))) (mapcat (juxt #(char (+ 65 %)) #(char (+ 97 %))) (range 26)))))

;; v2
;; Given: (defn tr [s a b] (let [tran (zipmap a b)] (apply str (map #(tran % %) s))))
(fn [x s] (#(tr s % (drop (* 2 (mod x 26)) (cycle %))) (mapcat (juxt #(char (+ 65 %)) #(char (+ 97 %))) (range 26))))
@serdary
Copy link

serdary commented Jul 20, 2013

Wow probably more secure than aes. Liked the first version better.:)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment