-
Notifications
You must be signed in to change notification settings - Fork 0
/
protocol.exs
32 lines (27 loc) · 811 Bytes
/
protocol.exs
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
defprotocol Caesar do
def encrypt(string, shift)
def rot13(string)
end
defimpl Caesar, for: BitString do
def encrypt("", _), do: ""
def encrypt(<< head::utf8, tail::binary >>, shift)
when (head + shift > 122) do
<< (96 + rem(head + shift, 122))::utf8, encrypt(tail, shift)::binary >>
end
def encrypt(<< head::utf8, tail::binary >>, shift) do
<< (head + shift)::utf8, encrypt(tail, shift)::binary >>
end
def rot13(string) do
encrypt(string, 13)
end
end
defimpl Caesar, for: List do
def encrypt([], _), do: []
def encrypt([head | tail], shift) when (head + shift > 122) do
[96 + rem(head + shift, 122) | encrypt(tail, shift)]
end
def encrypt([head | tail], shift), do: [head + shift | encrypt(tail, shift)]
def rot13(string) do
encrypt(string, 13)
end
end