Skip to content

Commit

Permalink
Fix obfuscator salt rendered as byte array in template (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
mgratzer authored Feb 19, 2024
1 parent 047ab2a commit ec732cb
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
10 changes: 5 additions & 5 deletions lib/obfuskit/obfuscator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
9 changes: 6 additions & 3 deletions sig/obfuskit/obfuscator.rbs
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit ec732cb

Please sign in to comment.