OpenWebNet password: Difference between revisions
Content added Content deleted
SqrtNegInf (talk | contribs) m (→{{header|Perl}}: note a gotcha) |
Thundergnat (talk | contribs) (Rename Perl 6 -> Raku, alphabetize, minor clean-up) |
||
Line 15: | Line 15: | ||
← *#*1##</pre> |
← *#*1##</pre> |
||
=={{ |
=={{header|D}}== |
||
{{trans|Python}} |
{{trans|Python}} |
||
<lang d>import std.stdio, std.string, std.conv, std.ascii, std.algorithm; |
<lang d>import std.stdio, std.string, std.conv, std.ascii, std.algorithm; |
||
Line 236: | Line 236: | ||
</pre> |
</pre> |
||
=={{ |
=={{header|JavaScript}}== |
||
<lang javascript> |
<lang javascript> |
||
function calcPass (pass, nonce) { |
function calcPass (pass, nonce) { |
||
Line 380: | Line 380: | ||
testcalcpass() |
testcalcpass() |
||
</lang> |
</lang> |
||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
Line 553: | Line 552: | ||
say own_password( 12345, 410501656 ); |
say own_password( 12345, 410501656 ); |
||
say own_password( 12345, 630292165 );</lang> |
say own_password( 12345, 630292165 );</lang> |
||
{{out}} |
|||
<pre>25280520 |
|||
119537670 |
|||
4269684735</pre> |
|||
=={{header|Perl 6}}== |
|||
Perl 6 doesn't really have good support for unsigned fixed bit integers yet so emulating them with regular Ints and bitmasks. |
|||
<lang perl6>sub own-password (Int $password, Int $nonce) { |
|||
my int $n1 = 0; |
|||
my int $n2 = $password; |
|||
for $nonce.comb { |
|||
given $_ { |
|||
when 1 { |
|||
$n1 = $n2 +& 0xFFFFFF80 +> 7; |
|||
$n2 +<= 25; |
|||
} |
|||
when 2 { |
|||
$n1 = $n2 +& 0xFFFFFFF0 +> 4; |
|||
$n2 +<= 28; |
|||
} |
|||
when 3 { |
|||
$n1 = $n2 +& 0xFFFFFFF8 +> 3; |
|||
$n2 +<= 29; |
|||
} |
|||
when 4 { |
|||
$n1 = $n2 +< 1; |
|||
$n2 +>= 31; |
|||
} |
|||
when 5 { |
|||
$n1 = $n2 +< 5; |
|||
$n2 +>= 27; |
|||
} |
|||
when 6 { |
|||
$n1 = $n2 +< 12; |
|||
$n2 +>= 20; |
|||
} |
|||
when 7 { |
|||
$n1 = $n2 +& 0x0000FF00 +| ($n2 +& 0x000000FF +< 24) +| ($n2 +& 0x00FF0000 +> 16); |
|||
$n2 = $n2 +& 0xFF000000 +> 8; |
|||
} |
|||
when 8 { |
|||
$n1 = $n2 +& 0x0000FFFF +< 16 +| $n2 +> 24; |
|||
$n2 = $n2 +& 0x00FF0000 +> 8; |
|||
} |
|||
when 9 { $n1 = +^$n2 } |
|||
default { $n1 = $n2 } |
|||
} |
|||
given $_ { |
|||
when 0 {} |
|||
when 9 {} |
|||
default { $n1 = ($n1 +| $n2) +& 0xFFFFFFFF } |
|||
} |
|||
$n2 = $n1; |
|||
} |
|||
$n1 |
|||
} |
|||
say own-password( 12345, 603356072 ); |
|||
say own-password( 12345, 410501656 ); |
|||
say own-password( 12345, 630292165 );</lang> |
|||
{{out}} |
{{out}} |
||
<pre>25280520 |
<pre>25280520 |
||
Line 687: | Line 625: | ||
</pre> |
</pre> |
||
=={{ |
=={{header|PHP}}== |
||
<lang python> |
|||
def ownCalcPass (password, nonce, test=False) : |
|||
start = True |
|||
num1 = 0 |
|||
num2 = 0 |
|||
password = int(password) |
|||
if test: |
|||
print("password: %08x" % (password)) |
|||
for c in nonce : |
|||
if c != "0": |
|||
if start: |
|||
num2 = password |
|||
start = False |
|||
if test: |
|||
print("c: %s num1: %08x num2: %08x" % (c, num1, num2)) |
|||
if c == '1': |
|||
num1 = (num2 & 0xFFFFFF80) >> 7 |
|||
num2 = num2 << 25 |
|||
elif c == '2': |
|||
num1 = (num2 & 0xFFFFFFF0) >> 4 |
|||
num2 = num2 << 28 |
|||
elif c == '3': |
|||
num1 = (num2 & 0xFFFFFFF8) >> 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 ) |
|||
num2 = ( num2 & 0xFF000000 ) >> 8 |
|||
elif c == '8': |
|||
num1 = (num2 & 0x0000FFFF) << 16 | ( num2 >> 24 ) |
|||
num2 = (num2 & 0x00FF0000) >> 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 test_passwd_calc(passwd, nonce, expected): |
|||
res = ownCalcPass(passwd, nonce, False) |
|||
m = passwd+' '+nonce+' '+str(res)+' '+str(expected) |
|||
if res == int(expected) : |
|||
print('PASS '+m) |
|||
else : |
|||
print('FAIL '+m) |
|||
if __name__ == '__main__': |
|||
test_passwd_calc('12345','603356072','25280520') |
|||
test_passwd_calc('12345','410501656','119537670') |
|||
test_passwd_calc('12345','630292165','4269684735') |
|||
</lang> |
|||
=={{Header|PHP}}== |
|||
<lang PHP>function ownCalcPass($password, $nonce) { |
<lang PHP>function ownCalcPass($password, $nonce) { |
||
$msr = 0x7FFFFFFF; |
$msr = 0x7FFFFFFF; |
||
Line 885: | Line 754: | ||
return sprintf('%u', $num1 & $m_1); |
return sprintf('%u', $num1 & $m_1); |
||
} |
} |
||
</lang> |
|||
=={{header|Python}}== |
|||
<lang python> |
|||
def ownCalcPass (password, nonce, test=False) : |
|||
start = True |
|||
num1 = 0 |
|||
num2 = 0 |
|||
password = int(password) |
|||
if test: |
|||
print("password: %08x" % (password)) |
|||
for c in nonce : |
|||
if c != "0": |
|||
if start: |
|||
num2 = password |
|||
start = False |
|||
if test: |
|||
print("c: %s num1: %08x num2: %08x" % (c, num1, num2)) |
|||
if c == '1': |
|||
num1 = (num2 & 0xFFFFFF80) >> 7 |
|||
num2 = num2 << 25 |
|||
elif c == '2': |
|||
num1 = (num2 & 0xFFFFFFF0) >> 4 |
|||
num2 = num2 << 28 |
|||
elif c == '3': |
|||
num1 = (num2 & 0xFFFFFFF8) >> 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 ) |
|||
num2 = ( num2 & 0xFF000000 ) >> 8 |
|||
elif c == '8': |
|||
num1 = (num2 & 0x0000FFFF) << 16 | ( num2 >> 24 ) |
|||
num2 = (num2 & 0x00FF0000) >> 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 test_passwd_calc(passwd, nonce, expected): |
|||
res = ownCalcPass(passwd, nonce, False) |
|||
m = passwd+' '+nonce+' '+str(res)+' '+str(expected) |
|||
if res == int(expected) : |
|||
print('PASS '+m) |
|||
else : |
|||
print('FAIL '+m) |
|||
if __name__ == '__main__': |
|||
test_passwd_calc('12345','603356072','25280520') |
|||
test_passwd_calc('12345','410501656','119537670') |
|||
test_passwd_calc('12345','630292165','4269684735') |
|||
</lang> |
</lang> |
||
Line 950: | Line 888: | ||
(own-test-calc-pass "12345" "410501656" 119537670))</lang> |
(own-test-calc-pass "12345" "410501656" 119537670))</lang> |
||
=={{ |
=={{header|Raku}}== |
||
(formerly Perl 6) |
|||
Perl 6 doesn't really have good support for unsigned fixed bit integers yet so emulating them with regular Ints and bitmasks. |
|||
<lang perl6>sub own-password (Int $password, Int $nonce) { |
|||
my int $n1 = 0; |
|||
my int $n2 = $password; |
|||
for $nonce.comb { |
|||
given $_ { |
|||
when 1 { |
|||
$n1 = $n2 +& 0xFFFFFF80 +> 7; |
|||
$n2 +<= 25; |
|||
} |
|||
when 2 { |
|||
$n1 = $n2 +& 0xFFFFFFF0 +> 4; |
|||
$n2 +<= 28; |
|||
} |
|||
when 3 { |
|||
$n1 = $n2 +& 0xFFFFFFF8 +> 3; |
|||
$n2 +<= 29; |
|||
} |
|||
when 4 { |
|||
$n1 = $n2 +< 1; |
|||
$n2 +>= 31; |
|||
} |
|||
when 5 { |
|||
$n1 = $n2 +< 5; |
|||
$n2 +>= 27; |
|||
} |
|||
when 6 { |
|||
$n1 = $n2 +< 12; |
|||
$n2 +>= 20; |
|||
} |
|||
when 7 { |
|||
$n1 = $n2 +& 0x0000FF00 +| ($n2 +& 0x000000FF +< 24) +| ($n2 +& 0x00FF0000 +> 16); |
|||
$n2 = $n2 +& 0xFF000000 +> 8; |
|||
} |
|||
when 8 { |
|||
$n1 = $n2 +& 0x0000FFFF +< 16 +| $n2 +> 24; |
|||
$n2 = $n2 +& 0x00FF0000 +> 8; |
|||
} |
|||
when 9 { $n1 = +^$n2 } |
|||
default { $n1 = $n2 } |
|||
} |
|||
given $_ { |
|||
when 0 {} |
|||
when 9 {} |
|||
default { $n1 = ($n1 +| $n2) +& 0xFFFFFFFF } |
|||
} |
|||
$n2 = $n1; |
|||
} |
|||
$n1 |
|||
} |
|||
say own-password( 12345, 603356072 ); |
|||
say own-password( 12345, 410501656 ); |
|||
say own-password( 12345, 630292165 );</lang> |
|||
{{out}} |
|||
<pre>25280520 |
|||
119537670 |
|||
4269684735</pre> |
|||
=={{header|Swift}}== |
|||
<lang swift> |
<lang swift> |
||
func openAuthenticationResponse(_password: String, operations: String) -> String? { |
func openAuthenticationResponse(_password: String, operations: String) -> String? { |