Advertisement
theinsekt

hsign2

Sep 12th, 2014
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.78 KB | None | 0 0
  1. --theinsektAPIs/hsign2
  2. --experiment2, not tested!
  3. --have changed how the code is structured
  4. --so that any send or receive function can be used
  5. --also any hashFunction can be used
  6.  
  7. --a signer: signs a message and turns it into a sendable string
  8. --a verifier: verifies the sendable string and returns ok,message2, where ok is true or false, and message2 is the
  9. --message that was signed
  10. --so if you do: local ok, message2=verify(sign(message))
  11. --then message2 will be equal to message, and ok will true
  12. --if someone fails at forging a message then ok will be false and message2 will be nil
  13.  
  14. --example usage (not tested!):
  15. --os.loadAPI("theinsektAPIs/sha256")
  16. --local protocol="myprotocolname"
  17. --local secret="mysupersecret"
  18. ----use this for sending
  19. --local sign=hsign2.getPack(secret,sha256.sha256)--do once
  20. --rednet.send(id,sign("my message, can be any type of data"),protocol)
  21. --rednet.send(id,sign("message2"),protocol)
  22. ----use this for receiving
  23. --local maxDelay=7 --7 seconds old messages will be considered invalid
  24. --local verify=hsign2.getUnpack(secret,sha256.sha256,maxDelay)--do once
  25. --local id, signedMessage,protocol=rednet.receive(protocol,timeout)
  26. --local ok,message=verify(signedMessage)--call directly after receive, because calculates time received inside
  27. -- if not ok then print("message was invalid") end
  28. --id, signedMessage,protocol=rednet.receive(protocol,timeout)
  29. --ok,message=verify(signedMessage)
  30. -- if not ok then print("message was invalid") end
  31.  
  32.  
  33.  
  34. --returns a pack function, that can be used to turn a message into a packet ready for sending
  35. --over rednet
  36. function getSigner(secret, hashFunction)
  37.   return function(message)
  38.     --prepare the box that holds the data, and a time stamp
  39.     local box={time0=os.time(),day0=os.day(), data=message,}
  40.     --turn the box into a string
  41.     local serializedBox=textutils.serialize(box)
  42.     --put it in a table, and put hash of it+secret
  43.     local packet={
  44.       ["box"]=serializedBox,
  45.       ["hash"]=hashFunction(serializedBox..secret),
  46.     }
  47.     --return the prepared packet
  48.     return textutils.serialize(packet)
  49.   end
  50. end
  51.  
  52.  
  53. --returns a unpack function that can unpack a packet, from the correspaonding pack function
  54. function getVerifier(secret,hashFunction,maxDelay)
  55.   --Todo: save time and day of last received message
  56.   --and discard any messages with lower or equal values
  57.   --can be saved here
  58.   return function(packet)
  59.     --get the time that the packet was received
  60.     local time2=os.time()
  61.     local day2=os.day()
  62.  
  63.     if type(packet)~="string" then
  64.       return false,nil
  65.     end
  66.  
  67.  
  68.     packet=textutils.unserialize(packet)
  69.     --type check packet and packet fields
  70.     if type(packet)~="table" or type(packet["box"])~="string" or type(packet["hash"])~="string" then
  71.       return false, nil
  72.     end
  73.  
  74.     --get and unserialize box
  75.     local serializedBox=packet["box"]
  76.     local box=textutils.unserialize(serializedBox)
  77.  
  78.     --type check box
  79.     if type(box)~="table" or type(box["time0"])~="number" or type(box["day0"])~="number" then
  80.       return false, nil
  81.     end
  82.  
  83.     --check that time stamps aren't to old
  84.     if maxDelay==nil then maxDelay=10 end
  85.     if math.abs(timeDiff(time2,day2,box["time0"],box["day0"]))>maxDelay then
  86.       return false, nil
  87.     end
  88.  
  89.     --check that the hash is correct
  90.     local hashValue=hashFunction(serializedBox..secret)
  91.     if hashValue~=packet["hash"] then
  92.       return false, nil
  93.     end
  94.  
  95.     --passed all tests
  96.     return true, box["data"]
  97.   end
  98. end
  99.  
  100.  
  101.  
  102.  
  103. --returns the time difference in seconds
  104. --is used in verifier
  105. function timeDiff(time1,day1,time2,day2)
  106.   --converts to minecraft ingame hours
  107.   local minecraftDiff=(day1-day2)*24+(time1-time2)
  108.   --converts to real seconds
  109.   local secondsDiff=minecraftDiff*((20/24)*60)
  110.   return secondsDiff
  111. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement