Simple cryptography utilities in ruby
Add this line to your application's Gemfile:
gem 'ovaltine'And then execute:
$ bundle
Or install it yourself as:
$ gem install ovaltine
require 'ovaltine'###Playfair Cipher
pf = Ovaltine::Playfair.new("passcode")
puts pf
# [ p a s c o ]
# [ d e b f g ]
# [ h ij k l m ]
# [ n q r t u ]
# [ v w x y z ]
ciphertext = pf.encode "The last Metroid is in captivity. The galaxy is at peace..."
# => "nlfijsculfqushekahqoscnhwlqcyijdeoijcyzkacqadsobw"
passcode = pf.decode ciphertext
# => "thelastmetroidisincaptivitythegalaxyisatpeacex"###Frequency Analysis
Helpful methods mixed into String
# Sample text
text = "Jerry was a race car driver".downcase.gsub(/\s/,'')Find the letter frequency of a text:
text.letter_frequency
# => {"j"=>1, "e"=>3, "r"=>6, "y"=>1, "w"=>1, "a"=>4, "s"=>1, ...Find the three most frequent digrams:
text.digram_frequency.sort_by { |digram, frequency| frequency}.reverse.take(3)
# => [["er", 2], ["ar", 2], ["ra", 1]]Other available methods:
text.trigram_frequency
text.ngram_frequency(8)###Co-prime numbers Generate the co-primes (< 26) of 26
26.coprimes
# => [1, 3, 5, 7, 9, 11, 15, 17, 19, 21, 23, 25]###DES Cryptographically insecure Data Encryption Standard implementation ####Key Schedule Generate the 8th round subkey:
Ovaltine::DES::KeySchedule.new("133457799BBCDFF1").keys[7]
# => "111101111000101000111010110000010011101111111011"Simple toy hash implementation
Ovaltine::TTH.hash("I leave twenty million dollars to my friendly cousin Bill.")
# => "BFQG"
Ovaltine::TTH.hash("AYHGD")
# => "BFQG"Bug reports and pull requests are welcome on GitHub at https://github.com/Aesthetikx/ovaltine. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
The gem is available as open source under the terms of the MIT License.
