Class: Cyphera::FF3
- Inherits:
-
Object
- Object
- Cyphera::FF3
- Defined in:
- lib/cyphera/ff3.rb
Instance Method Summary collapse
- #decrypt(ciphertext) ⇒ Object
- #encrypt(plaintext) ⇒ Object
-
#initialize(key, tweak, alphabet = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ') ⇒ FF3
constructor
A new instance of FF3.
Constructor Details
#initialize(key, tweak, alphabet = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ') ⇒ FF3
Returns a new instance of FF3.
5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/cyphera/ff3.rb', line 5 def initialize(key, tweak, alphabet = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ') raise ArgumentError, "Key must be 16, 24, or 32 bytes" unless [16, 24, 32].include?(key.bytesize) raise ArgumentError, "Tweak must be exactly 8 bytes" unless tweak.bytesize == 8 raise ArgumentError, "Alphabet must have >= 2 characters" if alphabet.length < 2 @key = key.reverse @tweak = tweak @alphabet = alphabet @radix = alphabet.length @char_map = {} alphabet.each_char.with_index { |c, i| @char_map[c] = i } end |
Instance Method Details
#decrypt(ciphertext) ⇒ Object
24 25 26 27 28 |
# File 'lib/cyphera/ff3.rb', line 24 def decrypt(ciphertext) digits = to_digits(ciphertext) result = ff3_decrypt(digits) from_digits(result) end |
#encrypt(plaintext) ⇒ Object
18 19 20 21 22 |
# File 'lib/cyphera/ff3.rb', line 18 def encrypt(plaintext) digits = to_digits(plaintext) result = ff3_encrypt(digits) from_digits(result) end |