caesar cipher


See On Github

Data

Contributor

Generic placeholder thumbnail

by Yonaba

in lua

Tags

Source Code

-- Caesar ciphering algorithm implementation
-- See: http://en.wikipedia.org/wiki/Caesar_cipher

-- 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

-- str     : a string to be ciphered
-- key     : the ciphering key (a number)
-- returns : the ciphered string
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 deciphered
-- key     : the ciphering key (a number)
-- returns : the deciphered string
local function caesar_decipher(str, key) 
  return caesar_cipher(str, -key) 
end

return {
  cipher     = caesar_cipher,
  decipher = caesar_decipher,
}
-- Tests for caesar_cipher.lua
local caesar = require 'caesar_cipher'

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(caesar.cipher('abcd',1) == 'bcde')
  assert(caesar.cipher('WXYZ',2) == 'YZAB')
  assert(caesar.cipher('abcdefghijklmnopqrstuvwxyz',3) == 'defghijklmnopqrstuvwxyzabc')
  assert(caesar.cipher('ABCDEFGHIJKLMNOPQRSTUVWXYZ',4) == 'EFGHIJKLMNOPQRSTUVWXYZABCD')
end)

run('Deciphering test', function()
  assert(caesar.decipher('bcde',1) == 'abcd')
  assert(caesar.decipher('YZAB',2) == 'WXYZ')
  assert(caesar.decipher('defghijklmnopqrstuvwxyzabc',3) == 'abcdefghijklmnopqrstuvwxyz')
  assert(caesar.decipher('EFGHIJKLMNOPQRSTUVWXYZABCD',4) == 'ABCDEFGHIJKLMNOPQRSTUVWXYZ')
end)

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