OpenWebNet password: Difference between revisions

m
m (→‎{{header|Phix}}: syntax coloured, made p2js compatible)
 
(10 intermediate revisions by 6 users not shown)
Line 18:
{{trans|Nim}}
 
<langsyntaxhighlight lang="11l">F ownCalcPass(password, nonce)
UInt32 result
V start = 1B
Line 46:
result = result << 16 [|] result >> 24 [|] (result [&] 00FF'0000) >> 8
‘9’
result = (-)~result
E
X.throw ValueError(‘non-digit in nonce.’)
R result
 
Line 61:
test_passwd_calc(‘12345’, ‘603356072’, 25280520)
test_passwd_calc(‘12345’, ‘410501656’, 119537670)
test_passwd_calc(‘12345’, ‘630292165’, 4269684735)</langsyntaxhighlight>
 
{{out}}
Line 68:
PASS 12345 410501656 119537670 119537670
PASS 12345 630292165 4269684735 4269684735
</pre>
 
=={{header|C++}}==
<syntaxhighlight lang="c++">
#include <cstdint>
#include <iostream>
#include <string>
 
int64_t own_password_calculation(const int64_t& password, const std::string& nonce) {
const int64_t m1 = 0xFFFF'FFFF;
const int64_t m8 = 0xFFFF'FFF8;
const int64_t m16 = 0xFFFF'FFF0;
const int64_t m128 = 0xFFFF'FF80;
const int64_t m16777216 = 0xFF00'0000;
 
bool flag = true;
int64_t number1 = 0;
int64_t number2 = 0;
 
for ( char ch : nonce ) {
number2 = number2 & m1;
 
switch (ch) {
case '1':
if ( flag ) { number2 = password; }
flag = false;
number1 = number2 & m128;
number1 = number1 >> 7;
number2 = number2 << 25;
number1 = number1 + number2;
break;
 
case '2':
if ( flag ) { number2 = password; }
flag = false;
number1 = number2 & m16;
number1 = number1 >> 4;
number2 = number2 << 28;
number1 = number1 + number2;
break;
 
case '3':
if ( flag ) { number2 = password; }
flag = false;
number1 = number2 & m8;
number1 = number1 >> 3;
number2 = number2 << 29;
number1 = number1 + number2;
break;
 
case '4':
if ( flag ) { number2 = password; }
flag = false;
number1 = number2 << 1;
number2 = number2 >> 31;
number1 = number1 + number2;
break;
 
case '5':
if ( flag ) { number2 = password; }
flag = false;
number1 = number2 << 5;
number2 = number2 >> 27;
number1 = number1 + number2;
break;
 
case '6':
if ( flag ) { number2 = password; }
flag = false;
number1 = number2 << 12;
number2 = number2 >> 20;
number1 = number1 + number2;
break;
 
case '7':
if ( flag ) { number2 = password; }
flag = false;
number1 = number2 & 0xFF00L;
number1 = number1 + ( ( number2 & 0xFFL ) << 24 );
number1 = number1 + ( ( number2 & 0xFF0000L ) >> 16 );
number2 = ( number2 & m16777216 ) >> 8;
number1 = number1 + number2;
break;
 
case '8':
if ( flag ) { number2 = password;}
flag = false;
number1 = number2 & 0xFFFFL;
number1 = number1 << 16;
number1 = number1 + ( number2 >> 24 );
number2 = number2 & 0xFF0000L;
number2 = number2 >> 8;
number1 = number1 + number2;
break;
 
case '9':
if ( flag ) { number2 = password; }
flag = false;
number1 = ~number2;
break;
 
default:
number1 = number2;
break;
}
number2 = number1;
}
 
return number1 & m1;
}
 
void own_password_calculation_test(const std::string& password, const std::string& nonce, const int64_t& expected) {
const int64_t result = own_password_calculation(std::stoll(password), nonce);
std::string message = password + " " + nonce + " " + std::to_string(result) + " " + std::to_string(expected);
std::cout << ( ( result == expected ) ? "PASS " + message : "FAIL " + message ) << std::endl;
}
 
int main() {
own_password_calculation_test("12345", "603356072", 25280520);
own_password_calculation_test("12345", "410501656", 119537670);
}
</syntaxhighlight>
{{ out }}
<pre>
PASS 12345 603356072 25280520 25280520
PASS 12345 410501656 119537670 119537670
</pre>
 
=={{header|D}}==
{{trans|Python}}
<langsyntaxhighlight lang="d">import std.stdio, std.string, std.conv, std.ascii, std.algorithm;
 
ulong ownCalcPass(in ulong password, in string nonce)
Line 198 ⟶ 324:
ownTestCalcPass("12345", "603356072", 25280520UL);
ownTestCalcPass("12345", "410501656", 119537670UL);
}</langsyntaxhighlight>
{{out}}
<pre>PASS 12345 603356072 25280520 25280520
Line 206 ⟶ 332:
{{libheader| System.SysUtils}}
{{Trans|Go}}
<syntaxhighlight lang="delphi">
<lang Delphi>
program OpenWebNet_password;
 
Line 307 ⟶ 433:
testPasswordCalc('12345', '630292165', 4269684735);
readln;
end.</langsyntaxhighlight>
 
=={{header|EasyLang}}==
{{trans|Python}}
<syntaxhighlight>
func calcpass passw$ nonce$ .
start = 1
passw = number passw$
for c$ in strchars nonce$
if c$ <> "0"
if start = 1
num2 = passw
.
start = 0
.
if c$ = "1"
num1 = bitshift bitand num2 0xFFFFFF80 -7
num2 = bitshift num2 25
elif c$ = "2"
num1 = bitshift bitand num2 0xFFFFFFF0 -4
num2 = bitshift num2 28
elif c$ = "3"
num1 = bitshift bitand num2 0xFFFFFFF8 -3
num2 = bitshift num2 29
elif c$ = "4"
num1 = bitshift num2 1
num2 = bitshift num2 -31
elif c$ = "5"
num1 = bitshift num2 5
num2 = bitshift num2 -27
elif c$ = "6"
num1 = bitshift num2 12
num2 = bitshift num2 -20
elif c$ = "7"
h1 = bitand num2 0x0000FF00
h2 = bitshift bitand num2 0x000000FF 24
h3 = bitshift bitand num2 0x00FF0000 -16
num1 = bitor bitor h1 h2 h3
num2 = bitshift bitand num2 0xFF000000 -8
elif c$ = "8"
num1 = bitor bitshift bitand num2 0x0000FFFF 16 bitshift num2 -24
num2 = bitshift bitand num2 0x00FF0000 -8
elif c$ = "9"
num1 = bitnot num2
else
num1 = num2
.
if c$ <> "0" and c$ <> "9"
num1 = bitor num1 num2
.
num2 = num1
.
return num1
.
proc test_passwd passwd$ nonce$ expected$ . .
res = calcpass passwd$ nonce$
m$ = passwd$ & " " & nonce$ & " " & res & " " & expected$
if res = number expected$
print "PASS " & m$
else
print "FAIL " & m$
.
.
test_passwd "12345" "603356072" "25280520"
test_passwd "12345" "410501656" "119537670"
test_passwd "12345" "630292165" "4269684735"
</syntaxhighlight>
 
=={{header|FreeBASIC}}==
{{trans|Go}}
<syntaxhighlight lang="vb">Function ownCalcPass(password As String, nonce As String) As Integer
Dim As Boolean start = True
Dim As Integer b, num1 = 0, num2 = 0
For b = 0 To Len(nonce)
Dim As String c = Mid(nonce,b,1)
If c <> "0" And start Then
num2 = Cint(password)
start = False
End If
Select Case c
Case "1"
num1 = (num2 And &HFFFFFF80) Shr 7
num2 = num2 Shl 25
Case "2"
num1 = (num2 And &HFFFFFFF0) Shr 4
num2 = num2 Shl 28
Case "3"
num1 = (num2 And &HFFFFFFF8) Shr 3
num2 = num2 Shl 29
Case "4"
num1 = num2 Shl 1
num2 = num2 Shr 31
Case "5"
num1 = num2 Shl 5
num2 = num2 Shr 27
Case "6"
num1 = num2 Shl 12
num2 = num2 Shr 20
Case "7"
num1 = num2 And &H0000FF00 Or ((num2 And &H000000FF) Shl 24) Or ((num2 And &H00FF0000) Shr 16)
num2 = (num2 And &HFF000000) Shr 8
Case "8"
num1 = (num2 And &H0000FFFF) Shl 16 Or (num2 Shr 24)
num2 = (num2 And &H00FF0000) Shr 8
Case "9"
num1 = Not num2
Case Else
num1 = num2
End Select
num1 And= &HFFFFFFFF
num2 And= &HFFFFFFFF
If c <> "0" And c <> "9" Then num1 Or= num2
num2 = num1
Next b
Return num1
End Function
 
Sub testPasswordCalc(password As String, nonce As String, expected As Integer)
Dim As Integer res = ownCalcPass(password, nonce)
Print Iif(res = expected, "PASS ", "FAIL ");
Print Using "& & & ##########"; password; nonce; res; expected
End Sub
 
testPasswordCalc("12345", "603356072", 25280520)
testPasswordCalc("12345", "410501656", 119537670)
testPasswordCalc("12345", "630292165", 4269684735)
 
Sleep</syntaxhighlight>
{{out}}
<pre>Same as Go entry.</pre>
 
=={{header|Go}}==
{{trans|Python}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 388 ⟶ 646:
testPasswordCalc("12345", "410501656", 119537670)
testPasswordCalc("12345", "630292165", 4269684735)
}</langsyntaxhighlight>
 
{{out}}
Line 395 ⟶ 653:
PASS 12345 410501656 119537670 119537670
PASS 12345 630292165 4269684735 4269684735
</pre>
 
=={{header|Java}}==
<syntaxhighlight lang="java">
public static void main(String[] aArgs) {
ownPasswordCalculationTest("12345", "603356072", 25280520);
ownPasswordCalculationTest("12345", "410501656", 119537670);
}
private static void ownPasswordCalculationTest(String password, String nonce, long expected) {
final long result = ownPasswordCalculation(Long.valueOf(password), nonce);
String message = password + " " + nonce + " " + result + " " + expected;
System.out.println( ( result == expected ) ? "PASS " + message : "FAIL " + message );
}
private static long ownPasswordCalculation(long password, String nonce) {
final long m1 = 0xFFFF_FFFFL;
final long m8 = 0xFFFF_FFF8L;
final long m16 = 0xFFFF_FFF0L;
final long m128 = 0xFFFF_FF80L;
final long m16777216 = 0xFF00_0000L;
 
boolean flag = true;
long number1 = 0;
long number2 = 0;
 
for ( char ch : nonce.toCharArray() ) {
number2 = number2 & m1;
 
switch (ch) {
case '1' -> {
if ( flag ) { number2 = password; }
flag = false;
number1 = number2 & m128;
number1 = number1 >>> 7;
number2 = number2 << 25;
number1 = number1 + number2;
}
case '2' -> {
if ( flag ) { number2 = password; }
flag = false;
number1 = number2 & m16;
number1 = number1 >>> 4;
number2 = number2 << 28;
number1 = number1 + number2;
}
case '3' -> {
if ( flag ) { number2 = password; }
flag = false;
number1 = number2 & m8;
number1 = number1 >>> 3;
number2 = number2 << 29;
number1 = number1 + number2;
}
case '4' -> {
if ( flag ) { number2 = password; }
flag = false;
number1 = number2 << 1;
number2 = number2 >>> 31;
number1 = number1 + number2;
}
case '5' -> {
if ( flag ) { number2 = password; }
flag = false;
number1 = number2 << 5;
number2 = number2 >>> 27;
number1 = number1 + number2;
}
case '6' -> {
if ( flag ) { number2 = password; }
flag = false;
number1 = number2 << 12;
number2 = number2 >>> 20;
number1 = number1 + number2;
}
case '7' -> {
if ( flag ) { number2 = password; }
flag = false;
number1 = number2 & 0xFF00L;
number1 = number1 + ( ( number2 & 0xFFL ) << 24 );
number1 = number1 + ( ( number2 & 0xFF0000L ) >>> 16 );
number2 = ( number2 & m16777216 ) >>> 8;
number1 = number1 + number2;
}
case '8' -> {
if ( flag ) { number2 = password;}
flag = false;
number1 = number2 & 0xFFFFL;
number1 = number1 << 16;
number1 = number1 + ( number2 >>> 24 );
number2 = number2 & 0xFF0000L;
number2 = number2 >>> 8;
number1 = number1 + number2;
}
case '9' -> {
if ( flag ) { number2 = password; }
flag = false;
number1 = ~number2;
}
default -> number1 = number2;
}
number2 = number1;
}
return number1 & m1;
}
 
}
</syntaxhighlight>
{{ out }}
<pre>
PASS 12345 603356072 25280520 25280520
PASS 12345 410501656 119537670 119537670
</pre>
 
=={{header|JavaScript}}==
<langsyntaxhighlight lang="javascript">
function calcPass (pass, nonce) {
var flag = true;
Line 488 ⟶ 868:
testCalcPass ('12345', '630292165', '4269684735');
testCalcPass ('12345', '523781130', '537331200');
</syntaxhighlight>
</lang>
 
=={{header|Julia}}==
Passes all tests.
<langsyntaxhighlight lang="julia">function calcpass(passwd, nonce::String)
startflag = true
n1 = 0
Line 540 ⟶ 920:
 
testcalcpass()
</syntaxhighlight>
</lang>
 
=={{header|Kotlin}}==
{{trans|Python}}
<langsyntaxhighlight lang="scala">// version 1.1.51
 
fun ownCalcPass(password: Long, nonce: String): Long {
Line 655 ⟶ 1,035:
ownTestCalcPass("12345", "603356072", 25280520)
ownTestCalcPass("12345", "410501656", 119537670)
}</langsyntaxhighlight>
 
{{out}}
Line 665 ⟶ 1,045:
=={{header|Nim}}==
{{trans|Python}}
<langsyntaxhighlight Nimlang="nim">import bitops, strutils
 
func ownCalcPass(password, nonce: string): uint32 =
Line 711 ⟶ 1,091:
testPasswordCalc("12345", "603356072", 25280520u32)
testPasswordCalc("12345", "410501656", 119537670u32)
testPasswordCalc("12345", "630292165", 4269684735u32)</langsyntaxhighlight>
 
{{out}}
Line 720 ⟶ 1,100:
=={{header|Perl}}==
{{trans|Raku}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
Line 767 ⟶ 1,147:
say own_password( 12345, 603356072 );
say own_password( 12345, 410501656 );
say own_password( 12345, 630292165 );</langsyntaxhighlight>
{{out}}
<pre>25280520
Line 774 ⟶ 1,154:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">ownCalcPass</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">pwd</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">string</span> <span style="color: #000000;">nonce</span><span style="color: #0000FF;">)</span>
Line 828 ⟶ 1,208:
<span style="color: #000000;">testPasswordCalc</span><span style="color: #0000FF;">(</span><span style="color: #000000;">12345</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"630292165"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">4269684735</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">testPasswordCalc</span><span style="color: #0000FF;">(</span><span style="color: #000000;">12345</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"523781130"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">537331200</span><span style="color: #0000FF;">)</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 838 ⟶ 1,218:
 
=={{header|PHP}}==
<langsyntaxhighlight PHPlang="php">function ownCalcPass($password, $nonce) {
$msr = 0x7FFFFFFF;
$m_1 = (int)0xFFFFFFFF;
Line 966 ⟶ 1,346:
return sprintf('%u', $num1 & $m_1);
}
</syntaxhighlight>
</lang>
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">
def ownCalcPass (password, nonce, test=False) :
start = True
Line 1,035 ⟶ 1,415:
test_passwd_calc('12345','630292165','4269684735')
 
</syntaxhighlight>
</lang>
 
=={{header|Racket}}==
Line 1,041 ⟶ 1,421:
{{trans|Python}} (followed by some aggressive refactoring)
 
<langsyntaxhighlight lang="racket">#lang racket/base
(define (32-bit-truncate n)
(bitwise-and n #xFFFFFFFF))
Line 1,098 ⟶ 1,478:
(own-test-calc-pass "12345" "603356072" 25280520)
(own-test-calc-pass "12345" "410501656" 119537670))</langsyntaxhighlight>
 
=={{header|Raku}}==
Line 1,104 ⟶ 1,484:
 
Raku doesn't really have good support for unsigned fixed bit integers yet so emulating them with regular Ints and bitmasks.
<syntaxhighlight lang="raku" perl6line>sub own-password (Int $password, Int $nonce) {
my int $n1 = 0;
my int $n2 = $password;
Line 1,156 ⟶ 1,536:
say own-password( 12345, 603356072 );
say own-password( 12345, 410501656 );
say own-password( 12345, 630292165 );</langsyntaxhighlight>
 
{{out}}
Line 1,164 ⟶ 1,544:
 
=={{header|Swift}}==
<langsyntaxhighlight lang="swift">
func openAuthenticationResponse(_password: String, operations: String) -> String? {
var num1 = UInt32(0)
Line 1,217 ⟶ 1,597:
return String(num1)
}
</syntaxhighlight>
</lang>
 
=={{header|Wren}}==
{{trans|Python}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight ecmascriptlang="wren">import "./fmt" for Fmt
 
var ownCalcPass = Fn.new { |password, nonce|
Line 1,285 ⟶ 1,665:
testPasswordCalc.call("12345", "603356072", 25280520)
testPasswordCalc.call("12345", "410501656", 119537670)
testPasswordCalc.call("12345", "630292165", 4269684735)</langsyntaxhighlight>
 
{{out}}
1,481

edits