Advertisement
theinsekt

hsign

Sep 11th, 2014
336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.67 KB | None | 0 0
  1. --theinsektAPIs/hsign
  2. --experiment, not tested much
  3. --will use a hash algorithm + shared salt to sign a message
  4. --will verify the message by checking that not to much time has passed
  5. --(to prevent attackers from sending a copy of the message)
  6. --will verify the message doing hash + salt to see that it's the same hash value
  7.  
  8. os.loadAPI("theinsektAPIs/sha256")
  9.  
  10.  
  11. function send(receiverID,message,protocol, secret)
  12.  --prepare the box that holds the data, and a time stamp
  13.  local box={time0=os.time(),day0=os.day(), data=message,}
  14.  --turn the box into a string
  15.  local serializedBox=textutils.serialize(box)
  16.  --put it in a table, and put hash of it+secret
  17.  local message={
  18.    ["box"]=serializedBox,
  19.    ["hash"]=hash(serializedBox..secret),
  20.  }
  21.  --send the prepared message table, return true or false
  22.  return rednet.send(receiverID,message,protocol)
  23. end
  24.  
  25.  
  26.  
  27.  
  28. --if timed out returns false,nil, you should nil check the first value
  29. --returns ok,senderID, message, protocol,hash
  30. --ok is true if the message was correctly signed
  31. --the user of this function could blacklist old hash values
  32. function receive(protocol, timeout,secret, timeLimit)
  33.  --receive from rednet
  34.  local senderID, message, protocol = rednet.receive(protocol,timeout)
  35.  
  36.  --get the time that the message was received
  37.  local time2=os.time()
  38.  local day2=os.day()
  39.  
  40.  --check that rednet receive didn't have a timeout
  41.  if senderID==nil then
  42.   return false, nil
  43.  end
  44.  
  45.  --type check message
  46.  if type(message)~="table" or type(message["box"])~="string" or type(message["hash"])~="string" then
  47.   return false, senderID, "", protocol,nil
  48.  end
  49.  
  50.  --get and unserialize box
  51.  local serializedBox=message["box"]
  52.  local box=textutils.unserialize(serializedBox)
  53.  
  54.  --type check box
  55.  if type(box)~="table" or type(box["time0"])~="number" or type(box["day0"])~="number" then
  56.   return false, senderID, "", protocol,nil
  57.  end
  58.  
  59.   --check that time stamps aren't to old
  60.   if timeLimit==nil then timeLimit=10 end
  61.   if math.abs(timeDiff(time2,day2,box["time0"],box["day0"]))>timeLimit then
  62.    return false, senderID, "", protocol,nil
  63.   end
  64.  
  65.   --check that the hash is correct
  66.   local hashValue=hash(serializedBox..secret)
  67.   if hashValue~=message["hash"] then
  68.    return false, senderID, "", protocol,nil
  69.   end
  70.  
  71.   --passed all tests
  72.   return true,senderID, box["data"], protocol,hashValue
  73. end
  74.  
  75.  
  76.  
  77.  
  78. --returns the time difference in seconds
  79. function timeDiff(time1,day1,time2,day2)
  80.   --converts to minecraft ingame hours
  81.   local minecraftDiff=(day1-day2)*24+(time1-time2)
  82.   --converts to real seconds
  83.   local secondsDiff=minecraftDiff*((20/24)*60)
  84.   return secondsDiff
  85. end
  86.  
  87. function hash(msg)
  88.  return sha256.sha256(msg)
  89. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement