A much better commit of the raw rmagick access. Huge time improvement for calculating the salt.

This commit is contained in:
monqui 2026-07-20 19:16:30 -04:00
parent 0b26d83313
commit e10bedee0f
4 changed files with 51 additions and 93 deletions

View file

@ -7,11 +7,11 @@ class EncodeImage
@@utils = nil
def initialize(p, m, i)
def initialize(p, m, uri)
@@password = p
@@message = m
@@utils = ImageUtils.new(@@password, @@message, i)
@@utils = ImageUtils.new(@@password, @@message, uri)
@@table = @@utils.generateLookupTable
end
@ -32,46 +32,4 @@ class EncodeImage
@@utils.getImage.saveImage
end
# Need to properly sever this at some point...
def decode
puts "Hacked in decode..."
s = @@utils.getImage.getSize
msg_bits = 0
@@table.reverse.each do |loc|
pixel = @@utils.getImage.getPixel(loc[:x], loc[:y])
msg_bits = (msg_bits << 6) | pixel.decodeBits
end
#this trash needs to move somewhere...
puts "Try to convert the message..."
len = msg_bits & 0xffff
msg_bits = msg_bits >> 16
# check the padding bits....
if (msg_bits & 255) != 0 then
puts "Padding failed scrutiny! "
puts msg_bits.to_s(2)
return false
end
msg_bits = msg_bits >> 8
puts "Padding passed scrutiny..."
msg_chars = []
while msg_bits > 0 do
byte = msg_bits & 0xff
msg_chars.push(byte.chr)
msg_bits = msg_bits >> 8
end
msg = msg_chars.reverse.join
return msg
end
end

View file

@ -1,65 +1,65 @@
require "./pixel_image.rb"
require "rmagick"
require "digest"
class ImageUtils
@@magickImg = nil
@@pixelImg = nil
@@password = ""
@@message = ""
@@hash = nil
def initialize(pass, msg, i)
@@width = 0
@@height = 0
@@pixel_arr = []
def initialize(pass, msg, uri)
@@password = pass
@@message = msg
@@salt = 0
@@magickImg = Magick::Image.read(uri).first
@@width = @@magickImg.columns
@@height = @@magickImg.rows
@@pixel_arr = @@magickImg.get_pixels(0, 0, @@width, @@height)
if msg.length > 1024 then
puts "Maximum length exceeded. Exiting."
exit
end
@@magickImg = i
@@pixelImg = PixelImage.new(@@magickImg)
self.generateHash
end
def getImage
return @@pixelImg
def getPixel(x, y)
loc = (y*@@width) + x
return @@pixel_arr[loc]
end
def getSalt
for x in 0..@@width-1
for y in 0..@@height-1
pix = self.getPixel(x, y)
@@salt = @@salt + (pix.red/512)
@@salt = @@salt + (pix.green/512)
@@salt = @@salt + (pix.blue/512)
end
end
h = Digest::SHA512.hexdigest(@@salt.to_s(16))
return h
end
def generateHash
h = Digest::SHA512.hexdigest(@@password)
s = @@pixelImg.getSalt
pass_h = Digest::SHA512.hexdigest(@@password)
@@hash = Digest::SHA512.hexdigest(self.interlaceHash(h, s))
end
def interlaceHash(h1, h2)
puts h1
puts h2
h1 = self.getBits(h1)
h2 = self.getBits(h2)
interlaced = 0
while h1.to_i > 0 and h2.to_i > 0 do
interlaced = (interlaced << 1) | (h1 & 1)
h1 = h1 >> 1
interlaced = (interlaced << 1) | (h2 & 1)
h2 = h2 >> 1
# puts interlaced
# puts h1
# puts h2
end
puts interlaced.to_s.inspect
return interlaced.to_s
@@hash = Digest::SHA512.hexdigest(pass_h + @@salt.to_s)
end
def generateLookupTable
@ -113,4 +113,4 @@ class ImageUtils
return msg_bits
end
end
end

Binary file not shown.

Before

Width:  |  Height:  |  Size: 237 KiB

View file

@ -1,13 +1,10 @@
require "rmagick"
require "./image_utils.rb"
require "./encode_image.rb"
img = Magick::ImageList.new "./images/original/arty.png"
#srand(12345)
pass = "my_password"
message = "This is the message that I would like to save please " +
"and thank you also I love you? AND apostrophe's, and " +
"also some commas somewhere hiding around here, It's " +
@ -24,17 +21,21 @@ message = "This is the message that I would like to save please " +
"are going even further. further than anyone believed " +
"`~!@\#\{$%^&*()-_=+\][}{';:'}]} Wooow. 123456789!1234" +
"56789! so put 15 more??????400!"
#message = "tests"
puts message.length
puts message
puts
puts "Initializing EncodeImage..."
start = Time.new.to_f
em = EncodeImage.new(pass, message, img)
puts "Initializing EncodeImage..."
iu = ImageUtils.new(pass, message, "./images/original/arty.png")
puts "Took " + (Time.new.to_f - start).to_s + " seconds to encode... 🐱"
=begin
em = EncodeImage.new(pass, message, "./images/original/arty.png")
puts em.encode
puts "Took " + (Time.new.to_f - start).to_s + " seconds to encode... 🐱"
@ -46,13 +47,12 @@ result = em.decode
puts result
=end
puts "Took " + (Time.new.to_f - start).to_s + " seconds to decode... 🐱"
if message == result then
puts "WOW THEY MATCH! 🐱🐱🐱🐱🐱🐱🐱🐱"
end
exit
puts
puts "Done!"