Class: Cyphera::FF1
- Inherits:
-
Object
- Object
- Cyphera::FF1
- Defined in:
- lib/cyphera/ff1.rb
Instance Method Summary collapse
- #decrypt(ciphertext) ⇒ Object
- #encrypt(plaintext) ⇒ Object
-
#initialize(key, tweak, alphabet = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ') ⇒ FF1
constructor
A new instance of FF1.
Constructor Details
#initialize(key, tweak, alphabet = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ') ⇒ FF1
Returns a new instance of FF1.
5 6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/cyphera/ff1.rb', line 5 def initialize(key, tweak, alphabet = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ') unless [16, 24, 32].include?(key.bytesize) raise ArgumentError, "invalid key length: #{key.bytesize} (expected 16, 24, or 32)" end raise ArgumentError, "Alphabet must have >= 2 characters" if alphabet.length < 2 @key = key @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
25 26 27 28 29 |
# File 'lib/cyphera/ff1.rb', line 25 def decrypt(ciphertext) digits = to_digits(ciphertext) result = ff1_decrypt(digits, @tweak) from_digits(result) end |
#encrypt(plaintext) ⇒ Object
19 20 21 22 23 |
# File 'lib/cyphera/ff1.rb', line 19 def encrypt(plaintext) digits = to_digits(plaintext) result = ff1_encrypt(digits, @tweak) from_digits(result) end |