Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --theinsektAPIs/hsign
- --experiment, not tested much
- --will use a hash algorithm + shared salt to sign a message
- --will verify the message by checking that not to much time has passed
- --(to prevent attackers from sending a copy of the message)
- --will verify the message doing hash + salt to see that it's the same hash value
- os.loadAPI("theinsektAPIs/sha256")
- function send(receiverID,message,protocol, secret)
- --prepare the box that holds the data, and a time stamp
- local box={time0=os.time(),day0=os.day(), data=message,}
- --turn the box into a string
- local serializedBox=textutils.serialize(box)
- --put it in a table, and put hash of it+secret
- local message={
- ["box"]=serializedBox,
- ["hash"]=hash(serializedBox..secret),
- }
- --send the prepared message table, return true or false
- return rednet.send(receiverID,message,protocol)
- end
- --if timed out returns false,nil, you should nil check the first value
- --returns ok,senderID, message, protocol,hash
- --ok is true if the message was correctly signed
- --the user of this function could blacklist old hash values
- function receive(protocol, timeout,secret, timeLimit)
- --receive from rednet
- local senderID, message, protocol = rednet.receive(protocol,timeout)
- --get the time that the message was received
- local time2=os.time()
- local day2=os.day()
- --check that rednet receive didn't have a timeout
- if senderID==nil then
- return false, nil
- end
- --type check message
- if type(message)~="table" or type(message["box"])~="string" or type(message["hash"])~="string" then
- return false, senderID, "", protocol,nil
- end
- --get and unserialize box
- local serializedBox=message["box"]
- local box=textutils.unserialize(serializedBox)
- --type check box
- if type(box)~="table" or type(box["time0"])~="number" or type(box["day0"])~="number" then
- return false, senderID, "", protocol,nil
- end
- --check that time stamps aren't to old
- if timeLimit==nil then timeLimit=10 end
- if math.abs(timeDiff(time2,day2,box["time0"],box["day0"]))>timeLimit then
- return false, senderID, "", protocol,nil
- end
- --check that the hash is correct
- local hashValue=hash(serializedBox..secret)
- if hashValue~=message["hash"] then
- return false, senderID, "", protocol,nil
- end
- --passed all tests
- return true,senderID, box["data"], protocol,hashValue
- end
- --returns the time difference in seconds
- function timeDiff(time1,day1,time2,day2)
- --converts to minecraft ingame hours
- local minecraftDiff=(day1-day2)*24+(time1-time2)
- --converts to real seconds
- local secondsDiff=minecraftDiff*((20/24)*60)
- return secondsDiff
- end
- function hash(msg)
- return sha256.sha256(msg)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement