This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
thatsimplekid's profile picture

Hashing strings on the fly?

Started by thatsimplekid, 23 March 2014 - 02:51 PM
thatsimplekid #1
Posted 23 March 2014 - 03:51 PM
Hello, I'm not sure if this has already been asked, but I cannot seem to find what I am looking for…

Is there any way for me to hash a string for transmission over rednet, so that it is unreadable to those who don't have knowledge of how to de-hash it?
And then to be able to de-hash it on a "client" when it reaches its destination?

Any help is much appreciated :D/>
Goof #2
Posted 23 March 2014 - 03:59 PM
Well first of all.. "Hashes" are algorithm which CANNOT be "de-hashed" as you might think off..

What you're looking at is Encrypt/Decryption algorithms.

My forms for decrypt/encrypt is the following:

– It requires a string to be encrypted, and some password, so you only can reuse it…
Spoiler

local ValidChars = {
"!","\"","#","$","%","&","'","(",")","*","+",",","-",".","/","0","1","2","3",
"4","5","6","7","8","9",":",";","<","=",">","?","@","A","B","C","D","E","F","G",
"H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","[",
"\\","]","^","_","`","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o",
"p","q","r","s","t","u","v","w","x","y","z","{","|","}","~";
} -- well yeah... just a table with the valid chars which can be encrypted.

local CharToCode = {}
local CodeToChar = {}
for _, c in pairs(ValidChars) do
  local code = #CodeToChar+1
  CharToCode[c] = code
  CodeToChar[code] = c
end

string.encrypt = function(msg, key) -- encrypt
  math.randomseed(key)
  local out = ""
  for i = 1,#msg do
    local c = msg:sub(i,i)
    local code = CharToCode[c]
    code = code + math.random(0,#ValidChars)
    if code > #ValidChars then
      code = code - #ValidChars
    end
    out = out..CodeToChar[code]
  end
  return out
end

string.decrypt = function(msg, key) --decrypt
  math.randomseed(key)
  local out = ""
  for i = 1,#msg do
    local c = msg:sub(i,i)
    local code = CharToCode[c]
    code = code - math.random(0,#ValidChars)
    if code < 1 then
      code = #ValidChars + code
    end
    out = out..CodeToChar[code]
  end
  return out
end

Usage?:
– sender computer

os.run({},"myprogramname") -- if you used my code as another program
-- if not, then delete this^
local side = "right"
local yourkey = 1234
print("input a string to encrypt:")
local str=string.encrypt(read(),yourkey)
print("Encrypted string:\n"..str)
rednet.open(side)
rednet.broadcast(str)


–receiver computer

os.run({},"myprogramname")
local side = "right"
local yourkey = 1234
rednet.open(side)
while true do
local id, msg, dist=rednet.receive()
local decryptedString = string.decrypt(msg,yourkey) 
print("Received string: "..msg)
print("Decrypted string as: "..decryptedString)
end



If you want more information just ask… else you might be able to find tutorials in the "tutorials" section..(else: Use Google)
Edited on 23 March 2014 - 03:06 PM
thatsimplekid #3
Posted 23 March 2014 - 04:10 PM

Ahh, thanks! Yeah, I did mean encryption/decryption, not hashing (that was a weird thought process :o/> ) :P/>

Okay, thankyou :D/>