catchat/image_utils.rb
2025-07-16 01:48:49 +00:00

81 lines
No EOL
1.4 KiB
Ruby

require "./pixel_image.rb"
require "digest"
class ImageUtils
@@magickImg = nil
@@pixelImg = nil
@@password = ""
@@message = ""
@@hash = nil
def initialize(pass, msg, i)
@@password = pass
@@message = msg
if msg.length > 400 then
puts "Maximum length exceeded. Exiting."
exit
end
@@magickImg = i
@@pixelImg = PixelImage.new(@@magickImg)
self.generateHash
end
def getImage
return @@pixelImg
end
def generateHash
h = Digest::SHA512.hexdigest(@@password)
s = @@pixelImg.getSalt
merge_to_i = h + s
@@hash = Digest::SHA512.hexdigest(h+s)
end
def generateLookupTable
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
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])
loc = {"x": x, "y": y}
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)
exit
end
end
hash_bits = hash_bits >> 1
tbl.push(loc)
msg_bits = msg_bits >> 6
end
return tbl
end
end