forked from irisyuan/Algorithm-Implementations
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))) |