<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Lösung zu: tutorials.de - Aus Buchstaben-Array einzigartige Strings generieren</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {
font-family: monospace;
display: flex;
}
body div {
margin: 30px;
text-align: center;
}
body td, th {
width: 100px;
text-align: right;
}
</style>
</head>
<body>
<?php
// Eingabewerte:
$allowed_chars = array('A', 'B', 'C', 'D', 'E', 'G', 'H', 'K', 'M', 'N',
'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'X', 'Y', 'Z');
$wordlength = 6;
$example_ids = array(0, 1, 2, 3, 4, 19, 20, 21, 4000, 63999998, 63999999);
$example_str = array();
function get_matching_string(array $chars, int $wl, int $index) {
$chars_len = count($chars);
$word = array();
for ($i = 0; $i < $wl; $i++) {
$power_10 = pow($chars_len, $wl - $i - 1);
$digit_index = intdiv($index, $power_10);
$index %= $power_10;
array_push($word, $chars[$digit_index]);
}
return implode("", $word);
}
function get_matching_index(array $chars, int $wl, string $str) {
$index = 0;
$chars_len = count($chars);
$counter = 0;
foreach (str_split(strrev($str)) as $char) {
$i = array_search($char, $chars);
$index += $i * pow($chars_len, $counter);
$counter += 1;
}
return $index;
}
function swapped_bits(int $i, int $a, int $b) {
$b1 = 1 & ($i >> $a); // bit at position $a
$b2 = 1 & ($i >> $b); // bit at position $b
$result = $i;
if ($b1 != $b2) {
$result = $i ^ ((1 << $a) | (1 << $b));
}
return $result;
}
function pseudomize(int $i, int $max) {
$highbit = floor(log($max, 2));
$swapmask = array(
0 => $highbit - 1,
1 => $highbit - 2,
2 => $highbit - 3,
);
foreach ($swapmask as $a => $b) {
$i = swapped_bits($i, $a, $b);
}
return $i;
}
// ****************************************************************************
// TEIL 1:
printf("<div>");
printf("<h3>Nicht pseudonymisierte Strings:</h3><table>");
printf("<tr><th>Index</th><th>String</th></tr>");
foreach ($example_ids as &$index) {
$str = get_matching_string($allowed_chars,
$wordlength,
$index);
printf("<tr><td>%d</td><td>%s</td></tr>", $index, $str);
array_push($example_str, $str);
}
printf("</table>");
printf("<h3>Und rückwärts:</h3><table>");
printf("<tr><th>String</th><th>Index</th></tr>");
foreach ($example_str as &$str) {
printf("<tr><td>%s</td><td>%d</td></tr>", $str, get_matching_index($allowed_chars,
$wordlength,
$str));
}
printf("</table>");
printf("</div>");
// ****************************************************************************
// ****************************************************************************
// TEIL 2:
$max = pow(count($allowed_chars), $wordlength);
$example_str = array();
printf("<div>");
printf("<h3>Pseudonymisierte Strings:</h3><table>");
printf("<tr><th>Index</th><th>String</th></tr>");
foreach ($example_ids as &$index) {
$str = get_matching_string($allowed_chars,
$wordlength,
pseudomize($index, $max));
printf("<tr><td>%d</td><td>%s</td></tr>", $index, $str);
array_push($example_str, $str);
}
printf("</table>");
printf("<h3>Und rückwärts:</h3><table>");
printf("<tr><th>String</th><th>Index</th></tr>");
foreach ($example_str as &$str) {
$index = get_matching_index($allowed_chars,
$wordlength,
$str);
printf("<tr><td>%s</td><td>%d</td></tr>", $str, pseudomize($index, $max));
}
printf("</table>");
printf("</div>");
// ****************************************************************************
?>
</body>
</html>