Class: Obfuskey
- Inherits:
-
Object
- Object
- Obfuskey
- Defined in:
- lib/obfuskey/math.rb,
lib/obfuskey/utils.rb,
lib/obfuskey/errors.rb,
lib/obfuskey/version.rb,
lib/obfuskey/obfuskey.rb,
lib/obfuskey/alphabets.rb,
lib/obfuskey/obfusbit_schema.rb
Defined Under Namespace
Modules: Alphabets, Math, Utils Classes: BitOverflowError, DuplicateError, Error, KeyLengthError, MaximumValueError, MultiplierError, NegativeValueError, ObfusbitSchema, SchemaValidationError, UnknownKeyError
Constant Summary collapse
- PRIME_MULTIPLIER =
1.618033988749894848- VERSION =
"0.1.0".freeze
- KEY_LENGTH =
6
Instance Attribute Summary collapse
-
#alphabet ⇒ Object
readonly
Returns the value of attribute alphabet.
-
#key_length ⇒ Object
readonly
Returns the value of attribute key_length.
-
#maximum_value ⇒ Object
readonly
Returns the value of attribute maximum_value.
Instance Method Summary collapse
- #get_key(value) ⇒ Object
- #get_value(key) ⇒ Object
-
#initialize(alphabet, key_length: KEY_LENGTH, multiplier: nil) ⇒ Obfuskey
constructor
A new instance of Obfuskey.
- #multiplier ⇒ Object
- #set_prime_multiplier(prime_multiplier) ⇒ Object
- #to_s ⇒ Object (also: #inspect)
Constructor Details
#initialize(alphabet, key_length: KEY_LENGTH, multiplier: nil) ⇒ Obfuskey
Returns a new instance of Obfuskey.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/obfuskey/obfuskey.rb', line 6 def initialize(alphabet, key_length: KEY_LENGTH, multiplier: nil) if alphabet.chars.uniq.length != alphabet.length raise DuplicateError, "The alphabet contains duplicate characters." end if !multiplier.nil? && (!multiplier.is_a?(Integer) || multiplier.even?) raise MultiplierError, "The multiplier must be an odd integer." end @alphabet = alphabet @key_length = key_length @maximum_value = alphabet.length**key_length - 1 @multiplier = multiplier @prime_multiplier = PRIME_MULTIPLIER end |
Instance Attribute Details
#alphabet ⇒ Object (readonly)
Returns the value of attribute alphabet.
4 5 6 |
# File 'lib/obfuskey/obfuskey.rb', line 4 def alphabet @alphabet end |
#key_length ⇒ Object (readonly)
Returns the value of attribute key_length.
4 5 6 |
# File 'lib/obfuskey/obfuskey.rb', line 4 def key_length @key_length end |
#maximum_value ⇒ Object (readonly)
Returns the value of attribute maximum_value.
4 5 6 |
# File 'lib/obfuskey/obfuskey.rb', line 4 def maximum_value @maximum_value end |
Instance Method Details
#get_key(value) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/obfuskey/obfuskey.rb', line 31 def get_key(value) raise NegativeValueError, "The value must be greater than or equal to zero." if value < 0 if value > @maximum_value raise MaximumValueError, "The maximum value possible is #{@maximum_value}" end return @alphabet[0] * @key_length if value.zero? encoded = Utils.encode((value * multiplier) % (@maximum_value + 1), @alphabet) encoded.rjust(@key_length, @alphabet[0]) end |
#get_value(key) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/obfuskey/obfuskey.rb', line 44 def get_value(key) unless key.chars.all? { |c| @alphabet.include?(c) } raise UnknownKeyError, "The key contains characters not found in the current alphabet." end if key.length != @key_length raise KeyLengthError, "The key length does not match the set length." end return 0 if key == @alphabet[0] * @key_length decoded = Utils.decode(key, @alphabet) max_p1 = @maximum_value + 1 decoded * Math.mod_inv(multiplier, max_p1) % max_p1 end |
#multiplier ⇒ Object
22 23 24 |
# File 'lib/obfuskey/obfuskey.rb', line 22 def multiplier @multiplier ||= generate_multiplier end |
#set_prime_multiplier(prime_multiplier) ⇒ Object
26 27 28 29 |
# File 'lib/obfuskey/obfuskey.rb', line 26 def set_prime_multiplier(prime_multiplier) @prime_multiplier = prime_multiplier @multiplier = nil end |
#to_s ⇒ Object Also known as: inspect
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/obfuskey/obfuskey.rb', line 61 def to_s multiplier_info = if @multiplier.nil? ", multiplier=auto (prime_mult=#{@prime_multiplier})" else ", multiplier=#{@multiplier}" end display_alphabet = if @alphabet.length > 20 "'#{@alphabet[0, 10]}...#{@alphabet[-5, 5]}'" else "'#{@alphabet}'" end "Obfuskey(alphabet=#{display_alphabet}, key_length=#{@key_length}#{multiplier_info})" end |