Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Handshake
- local Handshake = {}
- Handshake.prime = 625210769
- Handshake.channel = 54569
- Handshake.base = -1
- Handshake.secret = -1
- Handshake.sharedSecret = -1
- Handshake.packetHeader = "["..protocolName.."-Handshake-Packet-Header]"
- Handshake.packetMatch = "%["..protocolName.."%-Handshake%-Packet%-Header%](.+)"
- function Handshake.exponentWithModulo(base, exponent, modulo)
- local remainder = base
- for i = 1, exponent-1 do
- remainder = remainder * remainder
- if remainder >= modulo then
- remainder = remainder % modulo
- end
- end
- return remainder
- end
- function Handshake.clear()
- Handshake.base = -1
- Handshake.secret = -1
- Handshake.sharedSecret = -1
- end
- function Handshake.generateInitiatorData()
- Handshake.base = math.random(10,99999)
- Handshake.secret = math.random(10,99999)
- return {
- type = "initiate",
- prime = Handshake.prime,
- base = Handshake.base,
- moddedSecret = Handshake.exponentWithModulo(Handshake.base, Handshake.secret, Handshake.prime)
- }
- end
- function Handshake.generateResponseData(initiatorData)
- local isPrimeANumber = type(initiatorData.prime) == "number"
- local isPrimeMatching = initiatorData.prime == Handshake.prime
- local isBaseANumber = type(initiatorData.base) == "number"
- local isInitiator = initiatorData.type == "initiate"
- local isModdedSecretANumber = type(initiatorData.moddedSecret) == "number"
- local areAllNumbersNumbers = isPrimeANumber and isBaseANumber and isModdedSecretANumber
- if areAllNumbersNumbers and isPrimeMatching then
- if isInitiator then
- Handshake.base = initiatorData.base
- Handshake.secret = math.random(10,99999)
- Handshake.sharedSecret = Handshake.exponentWithModulo(initiatorData.moddedSecret, Handshake.secret, Handshake.prime)
- return {
- type = "response",
- prime = Handshake.prime,
- base = Handshake.base,
- moddedSecret = Handshake.exponentWithModulo(Handshake.base, Handshake.secret, Handshake.prime)
- }, Handshake.sharedSecret
- elseif initiatorData.type == "response" and Handshake.base > 0 and Handshake.secret > 0 then
- Handshake.sharedSecret = Handshake.exponentWithModulo(initiatorData.moddedSecret, Handshake.secret, Handshake.prime)
- return Handshake.sharedSecret
- else
- return false
- end
- else
- return false
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement