self
with depth
railsRail works by drawing a zig-zag pattern on depth
rails.
Say we have "fuckingbehemoth".railfence(4)
This happens in-memory:
f.....g.....o..
.u...n.b...m.t.
..c.i...e.e...h
...k.....h.....
Therefore, yielding the ciphertext : "fgounbmtcieehkh"
assert "fuckingbehemoth".railfence(4) == "fgounbmtcieehkh"
# Returns a rail-fence cipher from `self` with `depth` rails
#
# Rail works by drawing a zig-zag pattern on `depth` rails.
#
# Say we have "fuckingbehemoth".railfence(4)
#
# This happens in-memory:
#
# ~~~raw
# f.....g.....o..
# .u...n.b...m.t.
# ..c.i...e.e...h
# ...k.....h.....
# ~~~
#
# Therefore, yielding the ciphertext : "fgounbmtcieehkh"
#
# assert "fuckingbehemoth".railfence(4) == "fgounbmtcieehkh"
fun railfence(depth: Int): Text do
var lines = new Array[FlatBuffer].with_capacity(depth)
var up = false
for i in [0..depth[ do
lines[i] = new FlatBuffer.with_capacity(length)
end
var curr_depth = 0
for i in chars do
for j in [0..depth[ do
if j == curr_depth then
lines[j].add i
else
lines[j].add '.'
end
end
if up then
curr_depth -= 1
else
curr_depth += 1
end
if curr_depth == 0 then
up = false
end
if curr_depth == (depth - 1) then
up = true
end
end
var r = new FlatBuffer.with_capacity(length)
for i in lines do
r.append i.to_s.replace(".", "")
end
return r.to_s
end
lib/crypto/basic_ciphers.nit:78,2--128,4