aktuellen stand eingepflegt
This commit is contained in:
parent
a461fa5944
commit
c55b494d6c
|
@ -0,0 +1,10 @@
|
||||||
|
{
|
||||||
|
"autoload": {
|
||||||
|
"classmap": [
|
||||||
|
"src/"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"require-dev": {
|
||||||
|
"phpunit/phpunit": "^10"
|
||||||
|
}
|
||||||
|
}
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,163 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace NAE\Crypt;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Klasse um sehr Simple Kryptografie als Basis-Konvertierung zu implementieren.
|
||||||
|
* @package NAE\Crypt
|
||||||
|
* @author <Marcel Naeve> <php@naeve.info>
|
||||||
|
*/
|
||||||
|
class NumberBaseCrypt {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Holds the digits, base is defined by the amount of digits.
|
||||||
|
* @var array Digits
|
||||||
|
*/
|
||||||
|
protected $digits = [
|
||||||
|
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
|
||||||
|
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l','m', 'n', 'o', 'p', 'q', 'r', 's','t', 'u', 'v', 'w', 'x', 'y', 'z',
|
||||||
|
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
|
||||||
|
];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructor.
|
||||||
|
* @param array $digits Digits to use.
|
||||||
|
*/
|
||||||
|
public function __construct(array $digits=null) {
|
||||||
|
if($digits !== null) {
|
||||||
|
$this->digits = array_unique($digits);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the digits to use.
|
||||||
|
* @param array $digits Digits to use.
|
||||||
|
* @return NumberBaseCrypt $this
|
||||||
|
*/
|
||||||
|
public function setDigits(array $digits) : NumberBaseCrypt {
|
||||||
|
if($digits !== null) {
|
||||||
|
$this->digits = array_unique($digits);
|
||||||
|
}
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the digits to use from a string.
|
||||||
|
* @param string $digits Digits to use.
|
||||||
|
* @return NumberBaseCrypt $this
|
||||||
|
*/
|
||||||
|
public function setDigitsFromString(string $digits) : NumberBaseCrypt {
|
||||||
|
if(strlen($digits) > 0) {
|
||||||
|
$temp = str_split($digits);
|
||||||
|
$this->digits = array_unique($temp);
|
||||||
|
}
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Shuffles the digits.
|
||||||
|
* @return NumberBaseCrypt $this
|
||||||
|
*/
|
||||||
|
public function shuffleDigits() : NumberBaseCrypt {
|
||||||
|
shuffle($this->digits);
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the digits.
|
||||||
|
* @return array Digits.
|
||||||
|
*/
|
||||||
|
public function getDigits() : array {
|
||||||
|
return $this->digits;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the digits as a string.
|
||||||
|
* @return string Digits as a string.
|
||||||
|
*/
|
||||||
|
public function getDigitsAsString() : string {
|
||||||
|
return implode('', $this->digits);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Dezimal zu Basis-Konvertierung.
|
||||||
|
* @return int Dezimalzahl.
|
||||||
|
*/
|
||||||
|
public function base10ToBase (int $base10) : string {
|
||||||
|
$base = count($this->digits);
|
||||||
|
$result = '';
|
||||||
|
|
||||||
|
while($base10 > 0) {
|
||||||
|
$remaining = $base10 % $base;
|
||||||
|
$result = $this->digits[$remaining]. $result;
|
||||||
|
$base10 = floor($base10 / $base);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Basis-Konvertierung zu Dezimal.
|
||||||
|
* @return int Basiszahl.
|
||||||
|
*/
|
||||||
|
public function baseToBase10(string $maxBase): int {
|
||||||
|
$base = count($this->digits);
|
||||||
|
$result = 0;
|
||||||
|
for ($i = 0; $i < strlen($maxBase); $i++) {
|
||||||
|
$digit = strpos(implode('',$this->digits), $maxBase[$i]);
|
||||||
|
$result = $result * $base + $digit;
|
||||||
|
}
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a length prefix to the base.
|
||||||
|
* @param string $based Base to add the length to.
|
||||||
|
* @return string Resulting base.
|
||||||
|
*/
|
||||||
|
public function addLength(string $based) : string {
|
||||||
|
$strlen = strlen($based);
|
||||||
|
$bStrlen = $this->base10ToBase($strlen);
|
||||||
|
return $bStrlen . $based;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parses a base data collection.
|
||||||
|
* @param string $data Data to parse.
|
||||||
|
* @return array Resulting data collection.
|
||||||
|
*/
|
||||||
|
public function parseBaseDataCollection(string $data) : array {
|
||||||
|
$cd = [];
|
||||||
|
$len = 0;
|
||||||
|
while(strlen($data) > 0) {
|
||||||
|
$len = substr($data, 0, 1);
|
||||||
|
$len = $this->baseToBase10($len);
|
||||||
|
$data = substr($data, 1);
|
||||||
|
|
||||||
|
if($len <= strlen($data)) {
|
||||||
|
$tmp = substr($data, 0, $len);
|
||||||
|
$data = substr($data, $len);
|
||||||
|
|
||||||
|
$cd[] = $this->baseToBase10($tmp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $cd;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a base data collection from an Array of Dezimals.
|
||||||
|
* @param array $data Array of Dezimals.
|
||||||
|
* @return string Resulting data collection string.
|
||||||
|
*/
|
||||||
|
public function createBaseCollectionFromArray(array $data) : string {
|
||||||
|
$cd = '';
|
||||||
|
foreach($data as $item) {
|
||||||
|
$cd.= $this->addLength($this->base10ToBase($item));
|
||||||
|
}
|
||||||
|
return $cd;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,158 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\AppBundle;
|
||||||
|
|
||||||
|
include_once __DIR__.'/../vendor/autoload.php';
|
||||||
|
require_once __DIR__.'/../src/class.NumberBaseCrypt.php';
|
||||||
|
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
|
class test extends TestCase {
|
||||||
|
|
||||||
|
// Test setDigits method
|
||||||
|
public function testSetDigits() {
|
||||||
|
$numberBaseCrypt = new \NAE\Crypt\NumberBaseCrypt();
|
||||||
|
$numberBaseCrypt->setDigits([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
|
||||||
|
$this->assertEquals($numberBaseCrypt->getDigits(), [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test setDigitsFromString method
|
||||||
|
public function testSetDigitsFromString() {
|
||||||
|
$numberBaseCrypt = new \NAE\Crypt\NumberBaseCrypt();
|
||||||
|
$numberBaseCrypt->setDigitsFromString('0123456789');
|
||||||
|
$this->assertEquals($numberBaseCrypt->getDigits(), [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test shuffleDigits method
|
||||||
|
public function testShuffleDigits() {
|
||||||
|
$numberBaseCrypt = new \NAE\Crypt\NumberBaseCrypt([1, 2, 3]);
|
||||||
|
$numberBaseCrypt->shuffleDigits();
|
||||||
|
$this->assertNotEquals([1, 2, 3], $numberBaseCrypt->getDigits());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test getDigits method
|
||||||
|
public function testGetDigits() {
|
||||||
|
$numberBaseCrypt = new \NAE\Crypt\NumberBaseCrypt([1, 2, 3]);
|
||||||
|
$this->assertEquals($numberBaseCrypt->getDigits(), [1, 2, 3]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test getDigitsAsString method
|
||||||
|
public function testGetDigitsAsString() {
|
||||||
|
$numberBaseCrypt = new \NAE\Crypt\NumberBaseCrypt([1, 2, 3]);
|
||||||
|
$this->assertEquals($numberBaseCrypt->getDigitsAsString(), '123');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test base10ToBase method
|
||||||
|
public function testBase10ToBase() {
|
||||||
|
$numberBaseCrypt = new \NAE\Crypt\NumberBaseCrypt([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'a', 'b', 'c']);
|
||||||
|
$this->assertEquals('21', $numberBaseCrypt->base10ToBase(27));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test baseToBase10 method
|
||||||
|
public function testBaseToBase10() {
|
||||||
|
$numberBaseCrypt = new \NAE\Crypt\NumberBaseCrypt([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'a', 'b', 'c']);
|
||||||
|
$this->assertEquals(12, $numberBaseCrypt->baseToBase10('c'));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test addLength method
|
||||||
|
public function testAddLength() {
|
||||||
|
$numberBaseCrypt = new \NAE\Crypt\NumberBaseCrypt([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'a', 'b', 'c']);
|
||||||
|
$this->assertEquals('3foo', $numberBaseCrypt->addLength('foo'));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Full circle test 1
|
||||||
|
public function testFullCircle1() {
|
||||||
|
$numberBaseCrypt = new \NAE\Crypt\NumberBaseCrypt([
|
||||||
|
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
|
||||||
|
'a', 'b', 'c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
|
||||||
|
'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'
|
||||||
|
]);
|
||||||
|
$bX = $numberBaseCrypt->base10ToBase(101337);
|
||||||
|
$b10 = $numberBaseCrypt->baseToBase10($bX);
|
||||||
|
$this->assertEquals(101337, $b10);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Full circle test 2 - shuffled
|
||||||
|
public function testFullCircle2() {
|
||||||
|
$numberBaseCrypt = new \NAE\Crypt\NumberBaseCrypt([
|
||||||
|
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
|
||||||
|
'a', 'b', 'c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
|
||||||
|
'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'
|
||||||
|
]);
|
||||||
|
$numberBaseCrypt->shuffleDigits();
|
||||||
|
$bX = $numberBaseCrypt->base10ToBase(101337);
|
||||||
|
$b10 = $numberBaseCrypt->baseToBase10($bX);
|
||||||
|
$this->assertEquals(101337, $b10);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Full circle test 3 - shuffled between converts (reverse)
|
||||||
|
public function testFullCircle3() {
|
||||||
|
$numberBaseCrypt = new \NAE\Crypt\NumberBaseCrypt([
|
||||||
|
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
|
||||||
|
'a', 'b', 'c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
|
||||||
|
'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'
|
||||||
|
]);
|
||||||
|
$bX = $numberBaseCrypt->base10ToBase(101337);
|
||||||
|
$numberBaseCrypt->shuffleDigits();
|
||||||
|
$b10 = $numberBaseCrypt->baseToBase10($bX);
|
||||||
|
$this->assertNotEquals(101337, $b10);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Full circle test 4 - string export/import
|
||||||
|
public function testFullCircle4() {
|
||||||
|
$numberBaseCrypt1 = new \NAE\Crypt\NumberBaseCrypt([
|
||||||
|
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
|
||||||
|
'a', 'b', 'c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
|
||||||
|
'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'
|
||||||
|
]);
|
||||||
|
$numberBaseCrypt1->shuffleDigits();
|
||||||
|
$numberBaseCrypt2 = new \NAE\Crypt\NumberBaseCrypt([
|
||||||
|
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
|
||||||
|
'a', 'b', 'c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y'
|
||||||
|
]);
|
||||||
|
|
||||||
|
$numberBaseCrypt2->setDigitsFromString($numberBaseCrypt1->getDigitsAsString());
|
||||||
|
|
||||||
|
$bX = $numberBaseCrypt1->base10ToBase(1013374);
|
||||||
|
$b10 = $numberBaseCrypt2->baseToBase10($bX);
|
||||||
|
$this->assertEquals(1013374, $b10);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Full Circle data collection test
|
||||||
|
public function testFullCircleDataCollection() {
|
||||||
|
$data = [1337,1338,11111,141415,12,5];
|
||||||
|
$numberBaseCrypt = new \NAE\Crypt\NumberBaseCrypt([
|
||||||
|
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
|
||||||
|
'a', 'b', 'c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
|
||||||
|
'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'
|
||||||
|
]);
|
||||||
|
$numberBaseCrypt->shuffleDigits();
|
||||||
|
$dataCollection = "";
|
||||||
|
foreach ($data as $value) {
|
||||||
|
$tmp = $numberBaseCrypt->base10ToBase($value);
|
||||||
|
$dataCollection .= $numberBaseCrypt->addLength($tmp);
|
||||||
|
}
|
||||||
|
$reverse = $numberBaseCrypt->parseBaseDataCollection($dataCollection);
|
||||||
|
$this->assertEquals($data, $reverse);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Full Circle data collection test (reverse)
|
||||||
|
public function testFullCircleDataCollectionReverse() {
|
||||||
|
$data = [1337,1338,11111,141415,12,5];
|
||||||
|
$numberBaseCrypt = new \NAE\Crypt\NumberBaseCrypt([
|
||||||
|
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
|
||||||
|
'a', 'b', 'c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
|
||||||
|
'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'
|
||||||
|
]);
|
||||||
|
$dataCollection = "";
|
||||||
|
foreach ($data as $value) {
|
||||||
|
$tmp = $numberBaseCrypt->base10ToBase($value);
|
||||||
|
$dataCollection .= $numberBaseCrypt->addLength($tmp);
|
||||||
|
}
|
||||||
|
$numberBaseCrypt->shuffleDigits();
|
||||||
|
$reverse = $numberBaseCrypt->parseBaseDataCollection($dataCollection);
|
||||||
|
$this->assertNotEquals($data, $reverse);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue