OpenWebNet password: Difference between revisions

m
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
 
(18 intermediate revisions by 10 users not shown)
Line 14:
<pre>→ *#25280520##
← *#*1##</pre>
 
=={{header|11l}}==
{{trans|Nim}}
 
<syntaxhighlight lang="11l">F ownCalcPass(password, nonce)
UInt32 result
V start = 1B
L(c) nonce
I c != ‘0’ & start
result = UInt32(Int(password))
start = 0B
S c
‘0’
{}
‘1’
result = rotr(result, 7)
‘2’
result = rotr(result, 4)
‘3’
result = rotr(result, 3)
‘4’
result = rotl(result, 1)
‘5’
result = rotl(result, 5)
‘6’
result = rotl(result, 12)
‘7’
result = (result [&] 0000'FF00) [|] result << 24 [|]
(result [&] 00FF'0000) >> 16 [|] (result [&] FF00'0000) >> 8
‘8’
result = result << 16 [|] result >> 24 [|] (result [&] 00FF'0000) >> 8
‘9’
result = ~result
E
X.throw ValueError(‘non-digit in nonce.’)
R result
 
F test_passwd_calc(passwd, nonce, expected)
V res = ownCalcPass(passwd, nonce)
V m = passwd‘ ’nonce‘ ’res‘ ’expected
I res == expected
print(‘PASS ’m)
E
print(‘FAIL ’m)
 
test_passwd_calc(‘12345’, ‘603356072’, 25280520)
test_passwd_calc(‘12345’, ‘410501656’, 119537670)
test_passwd_calc(‘12345’, ‘630292165’, 4269684735)</syntaxhighlight>
 
{{out}}
<pre>
PASS 12345 603356072 25280520 25280520
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 143 ⟶ 324:
ownTestCalcPass("12345", "603356072", 25280520UL);
ownTestCalcPass("12345", "410501656", 119537670UL);
}</langsyntaxhighlight>
{{out}}
<pre>PASS 12345 603356072 25280520 25280520
PASS 12345 410501656 119537670 119537670</pre>
 
=={{header|Delphi}}==
{{libheader| System.SysUtils}}
{{Trans|Go}}
<syntaxhighlight lang="delphi">
program OpenWebNet_password;
 
{$APPTYPE CONSOLE}
 
uses
System.SysUtils;
 
function ownCalcPass(password, nonce: string): Cardinal;
begin
var start := True;
var num1 := 0;
var num2 := num1;
var i := password.ToInteger();
var pwd := i;
 
for var c in nonce do
begin
if c <> '0' then
begin
if start then
num2 := pwd;
start := False;
end;
 
case c of
'1':
begin
num1 := (num2 and $FFFFFF80) shr 7;
num2 := num2 shl 25;
end;
'2':
begin
num1 := (num2 and $FFFFFFF0) shr 4;
num2 := num2 shl 28;
end;
'3':
begin
num1 := (num2 and $FFFFFFF8) shr 3;
num2 := num2 shl 29;
end;
'4':
begin
num1 := num2 shl 1;
num2 := num2 shr 31;
end;
'5':
begin
num1 := num2 shl 5;
num2 := num2 shr 27;
end;
'6':
begin
num1 := num2 shl 12;
num2 := num2 shr 20;
end;
'7':
begin
var num3 := num2 and $0000FF00;
var num4 := ((num2 and $000000FF) shl 24) or ((num2 and $00FF0000) shr 16);
num1 := num3 or num4;
num2 := (num2 and $FF000000) shr 8;
end;
'8':
begin
;
num1 := (num2 and $0000FFFF) shl 16 or (num2 shr 24);
num2 := (num2 and $00FF0000) shr 8;
end;
'9':
begin
num1 := not num2;
end;
else
num1 := num2;
end;
 
num1 := num1 and $FFFFFFFF;
num2 := num2 and $FFFFFFFF;
if (c <> '0') and (c <> '9') then
num1 := num1 or num2;
num2 := num1;
end;
Result := num1;
end;
 
function TestPasswordCalc(Password, nonce: string; expected: Cardinal): Integer;
begin
var res := ownCalcPass(Password, nonce);
var m := format('%s %s %-10u %-10u', [Password, nonce, res, expected]);
if res = expected then
writeln('PASS ' + m)
else
writeln('FAIL ' + m);
end;
 
begin
testPasswordCalc('12345', '603356072', 25280520);
testPasswordCalc('12345', '410501656', 119537670);
testPasswordCalc('12345', '630292165', 4269684735);
readln;
end.</syntaxhighlight>
 
=={{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 227 ⟶ 646:
testPasswordCalc("12345", "410501656", 119537670)
testPasswordCalc("12345", "630292165", 4269684735)
}</langsyntaxhighlight>
 
{{out}}
Line 234 ⟶ 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 327 ⟶ 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 379 ⟶ 920:
 
testcalcpass()
</syntaxhighlight>
</lang>
 
=={{header|Kotlin}}==
{{trans|Python}}
<langsyntaxhighlight lang="scala">// version 1.1.51
 
fun ownCalcPass(password: Long, nonce: String): Long {
Line 494 ⟶ 1,035:
ownTestCalcPass("12345", "603356072", 25280520)
ownTestCalcPass("12345", "410501656", 119537670)
}</langsyntaxhighlight>
 
{{out}}
Line 501 ⟶ 1,042:
PASS 12345 410501656 119537670 119537670
</pre>
 
=={{header|Nim}}==
{{trans|Python}}
<syntaxhighlight lang="nim">import bitops, strutils
 
func ownCalcPass(password, nonce: string): uint32 =
 
var start = true
 
for c in nonce:
if c != '0' and start:
result = parseInt(password).uint32
start = false
case c
of '0':
discard
of '1':
result = result.rotateRightBits(7)
of '2':
result = result.rotateRightBits(4)
of '3':
result = result.rotateRightBits(3)
of '4':
result = result.rotateLeftBits(1)
of '5':
result = result.rotateLeftBits(5)
of '6':
result = result.rotateLeftBits(12)
of '7':
result = (result and 0x0000FF00) or result shl 24 or
(result and 0x00FF0000) shr 16 or (result and 0xFF000000u32) shr 8
of '8':
result = result shl 16 or result shr 24 or (result and 0x00FF0000) shr 8
of '9':
result = not result
else:
raise newException(ValueError, "non-digit in nonce.")
 
 
when isMainModule:
 
proc testPasswordCalc(password, nonce: string; expected: uint32) =
 
let res = ownCalcPass(password, nonce)
let m = "$# $# $# $#".format(password, nonce, res, expected)
echo if res == expected: "PASS " else: "FAIL ", m
 
testPasswordCalc("12345", "603356072", 25280520u32)
testPasswordCalc("12345", "410501656", 119537670u32)
testPasswordCalc("12345", "630292165", 4269684735u32)</syntaxhighlight>
 
{{out}}
<pre>PASS 12345 603356072 25280520 25280520
PASS 12345 410501656 119537670 119537670
PASS 12345 630292165 4269684735 4269684735</pre>
 
=={{header|Perl}}==
{{trans|Perl 6Raku}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
Line 551 ⟶ 1,147:
say own_password( 12345, 603356072 );
say own_password( 12345, 410501656 );
say own_password( 12345, 630292165 );</langsyntaxhighlight>
{{out}}
<pre>25280520
Line 558 ⟶ 1,154:
 
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>function unsigned(atom n)
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
atom m4 = allocate(8)
<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>
poke4(m4,n)
<span style="color: #004080;">bool</span> <span style="color: #000000;">start</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">true</span>
n = peek4u(m4)
<span style="color: #004080;">atom</span> <span style="color: #000000;">num1</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span>
free(m4)
<span style="color: #000000;">num2</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
return n
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">nonce</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
end function
<span style="color: #004080;">integer</span> <span style="color: #000000;">c</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">nonce</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
 
<span style="color: #008080;">if</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">!=</span><span style="color: #008000;">'0'</span> <span style="color: #008080;">and</span> <span style="color: #000000;">start</span> <span style="color: #008080;">then</span>
function ownCalcPass(atom pwd, string nonce)
<span style="color: #000000;">num2</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">pwd</span>
bool start = true
<span style="color: #000000;">start</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">false</span>
atom num1 = 0,
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
num2 = 0
<span style="color: #008080;">switch</span> <span style="color: #000000;">c</span> <span style="color: #008080;">do</span>
for i=1 to length(nonce) do
<span style="color: #008080;">case</span> <span style="color: #008000;">'1'</span><span style="color: #0000FF;">:</span> <span style="color: #000000;">num1</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">shift_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">num2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">7</span><span style="color: #0000FF;">)</span>
integer c = nonce[i]
<span style="color: #000000;">num2</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">shift_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">num2</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">25</span><span style="color: #0000FF;">)</span>
if c!='0' and start then
<span style="color: #008080;">case</span> <span style="color: #008000;">'2'</span><span style="color: #0000FF;">:</span> <span style="color: #000000;">num1</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">shift_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">num2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">)</span>
num2 = pwd
<span style="color: #000000;">num2</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">shift_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">num2</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">28</span><span style="color: #0000FF;">)</span>
start = false
<span style="color: #008080;">case</span> <span style="color: #008000;">'3'</span><span style="color: #0000FF;">:</span> <span style="color: #000000;">num1</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">shift_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">num2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">)</span>
end if
<span style="color: #000000;">num2</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">shift_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">num2</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">29</span><span style="color: #0000FF;">)</span>
switch c do
<span style="color: #008080;">case</span> <span style="color: #008000;">'4'</span><span style="color: #0000FF;">:</span> <span style="color: #000000;">num1</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">shift_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">num2</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
case '1': num1 = shift_bits(num2,7)
<span style="color: #000000;">num2</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">shift_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">num2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">31</span><span style="color: #0000FF;">)</span>
num2 = shift_bits(num2,-25)
<span style="color: #008080;">case</span> <span style="color: #008000;">'5'</span><span style="color: #0000FF;">:</span> <span style="color: #000000;">num1</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">shift_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">num2</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">5</span><span style="color: #0000FF;">)</span>
case '2': num1 = shift_bits(num2,4)
<span style="color: #000000;">num2</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">shift_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">num2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">27</span><span style="color: #0000FF;">)</span>
num2 = shift_bits(num2,-28)
<span style="color: #008080;">case</span> <span style="color: #008000;">'6'</span><span style="color: #0000FF;">:</span> <span style="color: #000000;">num1</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">shift_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">num2</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">12</span><span style="color: #0000FF;">)</span>
case '3': num1 = shift_bits(num2,3)
<span style="color: #000000;">num2</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">shift_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">num2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">20</span><span style="color: #0000FF;">)</span>
num2 = shift_bits(num2,-29)
<span style="color: #008080;">case</span> <span style="color: #008000;">'7'</span><span style="color: #0000FF;">:</span> <span style="color: #000000;">num1</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">or_bits</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">and_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">num2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0x0000FF00</span><span style="color: #0000FF;">),</span>
case '4': num1 = shift_bits(num2,-1)
<span style="color: #7060A8;">or_bits</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">shift_bits</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">and_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">num2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0x000000FF</span><span style="color: #0000FF;">),-</span><span style="color: #000000;">24</span><span style="color: #0000FF;">),</span>
num2 = shift_bits(num2,31)
<span style="color: #7060A8;">shift_bits</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">and_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">num2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0x00FF0000</span><span style="color: #0000FF;">),</span><span style="color: #000000;">16</span><span style="color: #0000FF;">)))</span>
case '5': num1 = shift_bits(num2,-5)
<span style="color: #000000;">num2</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">shift_bits</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">and_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">num2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0xFF000000</span><span style="color: #0000FF;">),</span><span style="color: #000000;">8</span><span style="color: #0000FF;">)</span>
num2 = shift_bits(num2,27)
<span style="color: #008080;">case</span> <span style="color: #008000;">'8'</span><span style="color: #0000FF;">:</span> <span style="color: #000000;">num1</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">or_bits</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">shift_bits</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">and_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">num2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0x0000FFFF</span><span style="color: #0000FF;">),-</span><span style="color: #000000;">16</span><span style="color: #0000FF;">),</span>
case '6': num1 = shift_bits(num2,-12)
<span style="color: #7060A8;">shift_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">num2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">24</span><span style="color: #0000FF;">))</span>
num2 = shift_bits(num2,20)
<span style="color: #000000;">num2</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">shift_bits</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">and_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">num2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0x00FF0000</span><span style="color: #0000FF;">),</span><span style="color: #000000;">8</span><span style="color: #0000FF;">)</span>
case '7': num1 = or_bits(and_bits(num2,0x0000FF00),
<span style="color: #008080;">case</span> <span style="color: #008000;">'9'</span><span style="color: #0000FF;">:</span> <span style="color: #000000;">num1</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">not_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">num2</span><span style="color: #0000FF;">)</span>
or_bits(shift_bits(and_bits(num2,0x000000FF),-24),
<span style="color: #008080;">default</span><span style="color: #0000FF;">:</span> <span style="color: #000000;">num1</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">num2</span>
shift_bits(and_bits(num2,0x00FF0000),16)))
<span style="color: #008080;">end</span> <span style="color: #008080;">switch</span>
num2 = shift_bits(and_bits(num2,0xFF000000),8)
<span style="color: #008080;">if</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">!=</span><span style="color: #008000;">'0'</span> <span style="color: #008080;">and</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">!=</span><span style="color: #008000;">'9'</span> <span style="color: #008080;">then</span>
case '8': num1 = or_bits(shift_bits(and_bits(num2,0x0000FFFF),-16),
<span style="color: #000000;">num1</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">or_bits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">num1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">num2</span><span style="color: #0000FF;">)</span>
shift_bits(num2,24))
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
num2 = shift_bits(and_bits(num2,0x00FF0000),8)
<span style="color: #000000;">num2</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">num1</span>
case '9': num1 = not_bits(num2)
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
default: num1 = num2
<span style="color: #008080;">if</span> <span style="color: #000000;">num1</span><span style="color: #0000FF;"><</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #000000;">num1</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">#1_0000_0000</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end switch
<span style="color: #008080;">return</span> <span style="color: #000000;">num1</span>
if c!='0' and c!='9' then
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
num1 = or_bits(num1,num2)
end if
<span style="color: #008080;">procedure</span> <span style="color: #000000;">testPasswordCalc</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> <span style="color: #004080;">atom</span> <span style="color: #000000;">expected</span><span style="color: #0000FF;">)</span>
num2 = num1
<span style="color: #004080;">atom</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">:=</span> <span style="color: #000000;">ownCalcPass</span><span style="color: #0000FF;">(</span><span style="color: #000000;">pwd</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">nonce</span><span style="color: #0000FF;">)</span>
end for
<span style="color: #004080;">string</span> <span style="color: #000000;">pf</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">=</span><span style="color: #000000;">expected</span><span style="color: #0000FF;">?</span><span style="color: #008000;">"PASS"</span><span style="color: #0000FF;">:</span><span style="color: #008000;">"FAIL"</span><span style="color: #0000FF;">)</span>
return unsigned(num1)
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%s %d %s %-10d %-10d\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">pf</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">pwd</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">nonce</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">res</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">expected</span><span style="color: #0000FF;">})</span>
end function
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
procedure testPasswordCalc(atom pwd, string nonce, atom expected)
<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;">"603356072"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">25280520</span><span style="color: #0000FF;">)</span>
atom res := ownCalcPass(pwd, nonce)
<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;">"410501656"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">119537670</span><span style="color: #0000FF;">)</span>
string pf = iff(res=expected?"PASS":"FAIL")
<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>
printf(1,"%s %d %s %-10d %-10d\n", {pf, pwd, nonce, res, expected})
<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>
end procedure
<!--</syntaxhighlight>-->
testPasswordCalc(12345, "603356072", 25280520)
testPasswordCalc(12345, "410501656", 119537670)
testPasswordCalc(12345, "630292165", 4269684735)
testPasswordCalc(12345, "523781130", 537331200)</lang>
{{out}}
<pre>
Line 626 ⟶ 1,218:
 
=={{header|PHP}}==
<langsyntaxhighlight PHPlang="php">function ownCalcPass($password, $nonce) {
$msr = 0x7FFFFFFF;
$m_1 = (int)0xFFFFFFFF;
Line 754 ⟶ 1,346:
return sprintf('%u', $num1 & $m_1);
}
</syntaxhighlight>
</lang>
 
=={{header|Python}}==
<langsyntaxhighlight lang="python">
def ownCalcPass (password, nonce, test=False) :
start = True
Line 823 ⟶ 1,415:
test_passwd_calc('12345','630292165','4269684735')
 
</syntaxhighlight>
</lang>
 
=={{header|Racket}}==
Line 829 ⟶ 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 886 ⟶ 1,478:
(own-test-calc-pass "12345" "603356072" 25280520)
(own-test-calc-pass "12345" "410501656" 119537670))</langsyntaxhighlight>
 
=={{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.
Raku 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) {
<syntaxhighlight lang="raku" line>sub own-password (Int $password, Int $nonce) {
my int $n1 = 0;
my int $n2 = $password;
Line 943 ⟶ 1,536:
say own-password( 12345, 603356072 );
say own-password( 12345, 410501656 );
say own-password( 12345, 630292165 );</langsyntaxhighlight>
 
{{out}}
Line 951 ⟶ 1,544:
 
=={{header|Swift}}==
<langsyntaxhighlight lang="swift">
func openAuthenticationResponse(_password: String, operations: String) -> String? {
var num1 = UInt32(0)
Line 1,004 ⟶ 1,597:
return String(num1)
}
</syntaxhighlight>
</lang>
 
=={{header|Wren}}==
{{trans|Python}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="wren">import "./fmt" for Fmt
 
var ownCalcPass = Fn.new { |password, nonce|
var start = true
var num1 = 0
var num2 = 0
var pwd = Num.fromString(password)
for (c in nonce) {
if (c != "0") {
if (start) num2 = pwd
start = false
}
if (c == "1") {
num1 = (num2 & 0xFFFFFF80) >> 7
num2 = num2 << 25
} else if (c == "2") {
num1 = (num2 & 0xFFFFFFF0) >> 4
num2 = num2 << 28
} else if (c == "3") {
num1 = (num2 & 0xFFFFFFF8) >> 3
num2 = num2 << 29
} else if (c == "4") {
num1 = num2 << 1
num2 = num2 >> 31
} else if (c == "5") {
num1 = num2 << 5
num2 = num2 >> 27
} else if (c == "6") {
num1 = num2 << 12
num2 = num2 >> 20
} else if (c == "7") {
var num3 = num2 & 0x0000FF00
var num4 = ((num2 & 0x000000FF) << 24) | ((num2 & 0x00FF0000) >> 16)
num1 = num3 | num4
num2 = (num2 & 0xFF000000) >> 8
} else if (c == "8") {
num1 = (num2&0x0000FFFF)<<16 | (num2 >> 24)
num2 = (num2 & 0x00FF0000) >> 8
} else if (c == "9") {
num1 = ~num2
} else {
num1 = num2
}
num1 = num1 & 0xFFFFFFFF
num2 = num2 & 0xFFFFFFFF
if (c != "0" && c != "9") num1 = num1 | num2
num2 = num1
}
return num1
}
 
var testPasswordCalc = Fn.new { |password, nonce, expected|
var res = ownCalcPass.call(password, nonce)
var m = Fmt.swrite("$s $s $-10d $-10d", password, nonce, res, expected)
if (res == expected) {
System.print("PASS %(m)")
} else {
System.print("FAIL %(m)")
}
}
 
testPasswordCalc.call("12345", "603356072", 25280520)
testPasswordCalc.call("12345", "410501656", 119537670)
testPasswordCalc.call("12345", "630292165", 4269684735)</syntaxhighlight>
 
{{out}}
<pre>
PASS 12345 603356072 25280520 25280520
PASS 12345 410501656 119537670 119537670
PASS 12345 630292165 4269684735 4269684735
</pre>
1,463

edits