Sha256: c5f7dba86518e9ba73a69f56cd0aa9e154bf46b471db4cce6db7db5d3247053c
Contents?: true
Size: 1.56 KB
Versions: 1
Compression:
Stored size: 1.56 KB
Contents
# frozen_string_literal: true class String # "Encrypt / Decrypt" the string with Caesar cipher. This will shift the # alphabet letters by +n+, where +n+ is the integer key. The same function # is used for encryption / decryption. # @param opts [Hash] optional parameters # @option opts [Integer] :shift The shift key. Default value: 13. # @see https://en.wikipedia.org/wiki/Caesar_cipher # @return [String] the (de)ciphered string # @example # 'Hello world!'.rot # => "Uryyb jbeyq!" # 'Hello world!'.rot(shift: 11) # => "Spwwz hzcwo!" # 'Uryyb jbeyq!'.rot # => "Hello world!" # 'Spwwz hzcwo!'.rot(shift: 26-11) # => "Hello world!" def rot(opts = {}) opts[:shift] ||= 13 alphabet = Array('a'..'z') lowercase = alphabet.zip(alphabet.rotate(opts[:shift])).to_h alphabet = Array('A'..'Z') uppercasecase = alphabet.zip(alphabet.rotate(opts[:shift])).to_h encrypter = lowercase.merge(uppercasecase) chars.map { |c| encrypter.fetch(c, c) }.join end # "Encrypt / Decrypt" the string with Caesar cipher in place as described for # {String#rot}. # @return [String] the (de)ciphered string as well as changing changing the # object in place. # @example # a = 'Bonjour le monde !' # => "Bonjour le monde !" # a.rot! # => "Obawbhe yr zbaqr !" # a # => "Obawbhe yr zbaqr !" def rot!(opts = {}) replace(rot(opts)) end # Alias for {String#rot} with default value ( +rot(shift: 13)+ ). def rot13 rot end # Alias for {String#rot!} with default value ( +rot!(shift: 13)+ ). def rot13! rot! end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
ctf-party-1.4.1 | lib/ctf_party/rot.rb |