Making the random seeded pixel values a bit protected from infinite loopes.

This commit is contained in:
monqui 2026-07-20 20:29:07 -04:00
parent e10bedee0f
commit 6cd76be94d

View file

@ -2,17 +2,22 @@ require "rmagick"
require "digest" require "digest"
class ImageUtils class ImageUtils
@@magickImg = nil @@magickImg = nil
@@password = "" @@password = ""
@@message = "" @@message = ""
@@salt = 0
@@hash = nil @@imagickImg = nil
@@width = 0 @@width = 0
@@height = 0 @@height = 0
@@pixel_arr = [] @@pixel_arr = nil
@@hash = nil
@@rng = nil
def initialize(pass, msg, uri) def initialize(pass, msg, uri)
@@password = pass @@password = pass
@ -26,12 +31,12 @@ class ImageUtils
@@pixel_arr = @@magickImg.get_pixels(0, 0, @@width, @@height) @@pixel_arr = @@magickImg.get_pixels(0, 0, @@width, @@height)
if msg.length > 1024 then @@hash = self.generateHash
puts "Maximum length exceeded. Exiting."
exit
end
self.generateHash # convert hex string into an int to store for making the lookup table
@@rng = Random.new(@@hash.to_i(16))
puts self.getLookupTable.inspect
end end
def getPixel(x, y) def getPixel(x, y)
@ -59,41 +64,53 @@ class ImageUtils
def generateHash def generateHash
pass_h = Digest::SHA512.hexdigest(@@password) pass_h = Digest::SHA512.hexdigest(@@password)
@@hash = Digest::SHA512.hexdigest(pass_h + @@salt.to_s) return Digest::SHA512.hexdigest(pass_h + @@salt.to_s)
end end
def generateLookupTable def getBits(o)
msg_bytes = o.unpack("c*")
msg_bits = 0
for b in msg_bytes
msg_bits = (msg_bits << 8) | b
end
return msg_bits
end
def getLookupTable
tbl = [] tbl = []
hash_bits = @@pixelImg.getBits(@@hash) # track hash classes. if too many occur during the interval, abaondon
msg_bits = @@pixelImg.getBits(@@message) # encoding.
clashes = 0
clash_limit = 100
# gross, but pad in the 24 bits that will become the msg length msg_bits = self.getBits(@@message)
# and padding bits...
msg_bits = msg_bits << 24
while msg_bits > 0 do while msg_bits > 0 do
loc = nil loc = nil
loop do loop do
x = (hash_bits.to_i % @@pixelImg.getSize[:width]) x = @@rng.rand(0..@@width)
y = (hash_bits.to_i % @@pixelImg.getSize[:height]) y = @@rng.rand(0..@@height)
loc = {"x": x, "y": y} loc = {"x": x, "y": y}
if !tbl.include?(loc) then
clashes = 0
else
clashes = clashes + 1
end
break if !tbl.include?(loc) if clashes < clash_limit then
hash_bits = hash_bits >> 1 break
else
if hash_bits <= 0 then puts "wow clashed so much sorry for breaking"
puts
puts "wow too much data"
puts "leftover bits: " + msg_bits.to_s(36)
exit exit
end end
end end
hash_bits = hash_bits >> 1
tbl.push(loc) tbl.push(loc)
msg_bits = msg_bits >> 6 msg_bits = msg_bits >> 6