From ec732cb74e5fee1520aacd3fb5d0b4e2f4a14d08 Mon Sep 17 00:00:00 2001 From: Martin Gratzer Date: Tue, 20 Feb 2024 00:19:23 +0100 Subject: [PATCH] Fix obfuscator salt rendered as byte array in template (#11) --- lib/obfuskit/obfuscator.rb | 10 +++++----- sig/obfuskit/obfuscator.rbs | 9 ++++++--- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/lib/obfuskit/obfuscator.rb b/lib/obfuskit/obfuscator.rb index 0417726..da59499 100644 --- a/lib/obfuskit/obfuscator.rb +++ b/lib/obfuskit/obfuscator.rb @@ -11,23 +11,23 @@ class Obfuscator # The string is converted to an array of bytes and stored in @c. # The size of the array is stored in @l. def initialize(salt) - salt = salt.bytes if salt.is_a? String - @salt = salt - @length = @salt.size + @salt = salt || "" + @salt_bytes = (salt.bytes || []) if salt.is_a? String + @salt_length = @salt_bytes.size end # Obfuscates a string. # The string is converted to an array of bytes and each element is XORed with an element from @c. # The index of the element from @c is the index of the element from the string modulo @l. def obfuscate(value) - value.bytes.map.with_index { |b, i| b ^ @salt[i % @length] } + value.bytes.map.with_index { |b, i| b ^ @salt_bytes[i % @salt_length] } end # Reverses the obfuscation of an array of bytes. # Each element is XORed with an element from @c and the result is converted back to a string. # The index of the element from @c is the index of the element from the array modulo @l. def reveal(value) - value.map.with_index { |b, i| b ^ @salt[i % @length] }.pack('C*').force_encoding('utf-8') + value.map.with_index { |b, i| b ^ @salt_bytes[i % @salt_length] }.pack('C*').force_encoding('utf-8') end end end \ No newline at end of file diff --git a/sig/obfuskit/obfuscator.rbs b/sig/obfuskit/obfuscator.rbs index df98ff0..e76826b 100644 --- a/sig/obfuskit/obfuscator.rbs +++ b/sig/obfuskit/obfuscator.rbs @@ -1,10 +1,13 @@ module Obfuskit class Obfuscator - @length: Integer - @salt: String + attr_reader salt: String - def salt: -> String + def initialize: (salt: String) -> void def obfuscate: (String) -> [Integer] def reveal: ([Integer]) -> String + + private + attr_reader salt_length: Integer + attr_reader salt_bytes: [Integer] end end