github
Advanced Search
  • Home
  • Pricing and Signup
  • Explore GitHub
  • Blog
  • Login

Raynes / Cipher

  • Admin
  • Watch Unwatch
  • Fork
  • Your Fork
  • Pull Request
  • Download Source
    • 1
    • 1
  • Source
  • Commits
  • Network (1)
  • Issues (0)
  • Downloads (0)
  • Wiki (1)
  • Graphs
  • Branch: master

click here to add a description

click here to add a homepage

  • Branches (1)
    • master ✓
  • Tags (0)
Sending Request…
Enable Donations

Pledgie Donations

Once activated, we'll place the following badge in your repository's detail box:
Pledgie_example
This service is courtesy of Pledgie.

An implementation of Caesers Cipher in Clojure. — Read more

  cancel

  cancel
  • Private
  • Read-Only
  • HTTP Read-Only

This URL has Read+Write access

Removed unnecessary function. 
Raynes (author)
Sat Nov 07 15:04:05 -0800 2009
commit  461c25cadc933c0886cf31758f4d840abe7b7b17
tree    3ad1a5f3b5d9f7a488360d818c0648a5267a1583
parent  e1c02f28ef368fb8bec9885d74ad3575361afa63
Cipher / src / net / acidrayne / cipher / cipher.clj src/net/acidrayne/cipher/cipher.clj
100644 45 lines (39 sloc) 1.382 kb
edit raw blame history
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
(ns net.acidrayne.cipher.cipher
  (:gen-class)
  (:use [clojure.contrib.def :only (defnk)]))
 
(def alphabet-nums (apply hash-map (interleave (range 1 27) "abcdefghijklmnopqrstuvwxyz")))
(def alphabet-chars (apply hash-map (interleave "abcdefghijklmnopqrstuvwxyz" (range 1 27))))
 
(defn is-letter? [s]
  (if (re-find #"(?i)[a-z]" (.toString s)) true false))
 
(defnk shift-char
  "Shifts the given Character down the alphabet by three.
Has optional arg :d which takes :fw or :bw, so it can
be used for deciphering as well as ciphering. If it
isn't provided, will default to :fw for ciphering."
  [char :d :fw]
  (let [num (if (= d :fw) (+ char 3) (- char 3))
pred (if (= d :fw) (> num 25) (< num 2))
truec (if (= d :fw) (- num 26) (+ num 26))
index
(if (and pred (not= num (if (= d :fw) 26 1))) truec num)]
    (alphabet-nums index)))
 
(defn trans-string
  "Transforms a string into a vector of numbers
that correspond with their characters."
  [s]
  (for [x s] (alphabet-chars x)))
 
(defnk cipher
  "Encrypts a string with Caesar's cipher."
  [s :d :to]
  (apply str
(let [args
(if (= d :from) :bw :fw)]
(for [x (trans-string (.toLowerCase (apply str (filter is-letter? s))))]
(shift-char x :d args)))))
 
(comment
  cipher> (cipher "abd")
  "deg"
  cipher> (cipher "abd" :direction :from)
  "xya"
  cipher> (cipher "abd" :direction :to)
  "deg")
Blog | Support | Training | Contact | API | Status | Twitter | Help | Security
© 2010 GitHub Inc. All rights reserved. | Terms of Service | Privacy Policy
Powered by the Dedicated Servers and
Cloud Computing of Rackspace Hosting®
Dedicated Server