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"
class ImageUtils
@@magickImg = nil
@@magickImg = nil
@@password = ""
@@message = ""
@@password = ""
@@message = ""
@@salt = 0
@@hash = nil
@@imagickImg = nil
@@width = 0
@@height = 0
@@width = 0
@@height = 0
@@pixel_arr = []
@@pixel_arr = nil
@@hash = nil
@@rng = nil
def initialize(pass, msg, uri)
@@password = pass
@ -26,12 +31,12 @@ class ImageUtils
@@pixel_arr = @@magickImg.get_pixels(0, 0, @@width, @@height)
if msg.length > 1024 then
puts "Maximum length exceeded. Exiting."
exit
end
@@hash = self.generateHash
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
def getPixel(x, y)
@ -59,41 +64,53 @@ class ImageUtils
def generateHash
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
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 = []
hash_bits = @@pixelImg.getBits(@@hash)
msg_bits = @@pixelImg.getBits(@@message)
# gross, but pad in the 24 bits that will become the msg length
# and padding bits...
msg_bits = msg_bits << 24
# track hash classes. if too many occur during the interval, abaondon
# encoding.
clashes = 0
clash_limit = 100
msg_bits = self.getBits(@@message)
while msg_bits > 0 do
loc = nil
loop do
x = (hash_bits.to_i % @@pixelImg.getSize[:width])
y = (hash_bits.to_i % @@pixelImg.getSize[:height])
x = @@rng.rand(0..@@width)
y = @@rng.rand(0..@@height)
loc = {"x": x, "y": y}
if !tbl.include?(loc) then
clashes = 0
else
clashes = clashes + 1
end
break if !tbl.include?(loc)
hash_bits = hash_bits >> 1
if hash_bits <= 0 then
puts
puts "wow too much data"
puts "leftover bits: " + msg_bits.to_s(36)
if clashes < clash_limit then
break
else
puts "wow clashed so much sorry for breaking"
exit
end
end
hash_bits = hash_bits >> 1
tbl.push(loc)
msg_bits = msg_bits >> 6