-
Notifications
You must be signed in to change notification settings - Fork 530
/
Copy pathrot13.lua
32 lines (27 loc) · 834 Bytes
/
rot13.lua
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
-- ROT13 ciphering algorithm implementation
-- See: http://en.wikipedia.org/wiki/ROT13
-- Returns the ASCII bytecode of either 'a' or 'A'
local function ascii_base(s)
return s:lower() == s and ('a'):byte() or ('A'):byte()
end
-- ROT13 is based on Caesar ciphering algorithm, using 13 as a key
local function caesar_cipher(str, key)
return (str:gsub('%a', function(s)
local base = ascii_base(s)
return string.char(((s:byte() - base + key) % 26) + base)
end))
end
-- str : a string to be ciphered
-- returns : the ciphered string
local function rot13_cipher(str)
return caesar_cipher(str, 13)
end
-- str : a string to be deciphered
-- returns : the deciphered string
local function rot13_decipher(str)
return caesar_cipher(str, -13)
end
return {
cipher = rot13_cipher,
decipher = rot13_decipher,
}