Skip to content

Commit

Permalink
Added ROT13 ciphering algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
Yonaba committed Apr 21, 2014
1 parent a4aca2e commit ee844d8
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
32 changes: 32 additions & 0 deletions ROT13_Cipher/Lua/Yonaba/rot13.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,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,
}
36 changes: 36 additions & 0 deletions ROT13_Cipher/Lua/Yonaba/rot13_test.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
-- Tests for rot13.lua
local ROT13 = require 'rot13'

local total, pass = 0, 0

local function dec(str, len)
return #str < len
and str .. (('.'):rep(len-#str))
or str:sub(1,len)
end

local function run(message, f)
total = total + 1
local ok, err = pcall(f)
if ok then pass = pass + 1 end
local status = ok and 'PASSED' or 'FAILED'
print(('%02d. %68s: %s'):format(total, dec(message,68), status))
end

run('Ciphering test', function()
assert(ROT13.cipher('abcd') == 'nopq')
assert(ROT13.cipher('WXYZ') == 'JKLM')
assert(ROT13.cipher('abcdefghijklmnopqrstuvwxyz') == 'nopqrstuvwxyzabcdefghijklm')
assert(ROT13.cipher('ABCDEFGHIJKLMNOPQRSTUVWXYZ') == 'NOPQRSTUVWXYZABCDEFGHIJKLM')
end)

run('Deciphering test', function()
assert(ROT13.decipher('nopq') == 'abcd')
assert(ROT13.decipher('JKLM') == 'WXYZ')
assert(ROT13.decipher('nopqrstuvwxyzabcdefghijklm') == 'abcdefghijklmnopqrstuvwxyz')
assert(ROT13.decipher('NOPQRSTUVWXYZABCDEFGHIJKLM') == 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')
end)

print(('-'):rep(80))
print(('Total : %02d: Pass: %02d - Failed : %02d - Success: %.2f %%')
:format(total, pass, total-pass, (pass*100/total)))

0 comments on commit ee844d8

Please sign in to comment.