Anonymous user
OpenWebNet password: Difference between revisions
improve the python version by quite a margin, add a swift (4.2) version
(improve the python version by quite a margin, add a swift (4.2) version) |
|||
Line 1:
Calculate the password requested by ethernet gateways from the Legrand / Bticino MyHome OpenWebNet home automation system when the user's ip address is not in the gateway's whitelist
Line 363 ⟶ 362:
=={{Header|Python}}==
<lang python>
def ownCalcPass (password, nonce, test=False) :
start = True
num1 = 0
print("password: %08x" % (password))
for c in nonce :
num2 = password
if
print("c: %s num1: %08x num2: %08x" % (c, num1, num2))
if c == '1':
num1 = (num2 & 0xFFFFFF80) >> 7
num2 = num2 << 25
elif c == '2':
num2 = num2 << 28
elif c == '3':
num2 = num2 << 29
elif c == '4':
num1 = num2 << 1
num2 = num2 >> 31
elif c == '5':
num1 = num2 << 5
num2 = num2 >> 27
elif c == '6':
num1 = num2 << 12
num2 = num2 >> 20
elif c == '7':
num1 = num2 & 0x0000FF00 | (( num2 & 0x000000FF ) << 24 ) | (( num2 & 0x00FF0000 ) >> 16 )
elif c == '8':
elif c == '9':
num1 = ~num2
else :
num1 = num2
num1 &= 0xFFFFFFFF
num2 &= 0xFFFFFFFF
if (c not in "09"):
num1 |= num2
if test:
print(" num1: %08x num2: %08x" % (num1, num2))
num2 = num1
return num1
def
if __name__ == '__main__':
test_passwd_calc('12345','603356072','25280520')
test_passwd_calc('12345','410501656','119537670')
test_passwd_calc('12345','630292165','4269684735')
</lang>
=={{Header|Swift}}==
<lang swift>
func openAuthenticationResponse(_password: String, operations: String) -> String? {
var num1 = UInt32(0)
var num2 = UInt32(0)
var start = true
let password = UInt32(_password)!
for c in operations {
if (c != "0") {
if start {
num2 = password
}
start = false
}
switch c {
case "1":
num1 = (num2 & 0xffffff80) >> 7
num2 = num2 << 25
case "2":
num1 = (num2 & 0xfffffff0) >> 4
num2 = num2 << 28
case "3":
num1 = (num2 & 0xfffffff8) >> 3
num2 = num2 << 29
case "4":
num1 = num2 << 1
num2 = num2 >> 31
case "5":
num1 = num1 << 5
num2 = num2 >> 27
case "6":
num1 = num2 << 12
num2 = num2 >> 20
case "7":
num1 = (num2 & 0x0000ff00) | ((num2 & 0x000000ff) << 24) | ((num2 & 0x00ff0000) >> 16)
num2 = (num2 & 0xff000000) >> 8
case "8":
num1 = ((num2 & 0x0000ffff) << 16) | (num2 >> 24)
num2 = (num2 & 0x00ff0000) >> 8
case "9":
num1 = ~num2
case "0":
num1 = num2
default:
print("unexpected char \(c)")
return nil
}
if (c != "9") && (c != "0") {
num1 |= num2
}
num2 = num1
}
return String(num1)
}
</lang>
|