Class: Cyphera::FF1

Inherits:
Object
  • Object
show all
Defined in:
lib/cyphera/ff1.rb

Instance Method Summary collapse

Constructor Details

#initialize(key, tweak, alphabet = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ') ⇒ FF1

Returns a new instance of FF1.

Raises:

  • (ArgumentError)


5
6
7
8
9
10
11
12
13
14
15
# File 'lib/cyphera/ff1.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, "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



23
24
25
26
27
# File 'lib/cyphera/ff1.rb', line 23

def decrypt(ciphertext)
  digits = to_digits(ciphertext)
  result = ff1_decrypt(digits, @tweak)
  from_digits(result)
end

#encrypt(plaintext) ⇒ Object



17
18
19
20
21
# File 'lib/cyphera/ff1.rb', line 17

def encrypt(plaintext)
  digits = to_digits(plaintext)
  result = ff1_encrypt(digits, @tweak)
  from_digits(result)
end