Skip to content

Commit 32150de

Browse files
committed
Fix Base Converter Issue When Value Of String === 0x30 (0), add tests to generateString
1 parent 60e50d4 commit 32150de

File tree

2 files changed

+92
-4
lines changed

2 files changed

+92
-4
lines changed

lib/CryptLib/Core/BaseConverter.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
3-
* A Utility class for converting between raw binary strings and a given
4-
* list of characters
3+
* A Utility class for converting between raw binary strings and a given
4+
* list of characters
55
*
66
* PHP version 5.3
77
*
@@ -34,7 +34,10 @@ class BaseConverter {
3434
* @return string The converted string
3535
*/
3636
public static function convertFromBinary($string, $characters) {
37-
if (empty($string) || empty($characters)) {
37+
38+
if ($string === '' || empty($characters)) {
39+
var_dump($string, $characters);
40+
echo "Returned!";
3841
return '';
3942
}
4043
$string = str_split($string);
@@ -46,7 +49,8 @@ public static function convertFromBinary($string, $characters) {
4649
$callback = function ($num) use ($characters) {
4750
return $characters[$num];
4851
};
49-
return implode('', array_map($callback, $converted));
52+
$ret = implode('', array_map($callback, $converted));
53+
return $ret;
5054
}
5155

5256
/**

test/Vectors/Random/GeneratorTest.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,75 @@ public static function provideGenerateInt() {
3434
);
3535
}
3636

37+
public function provideGenerateString() {
38+
return array(
39+
array(
40+
1,
41+
1,
42+
"01",
43+
function($j) {
44+
return str_pad(decbin($j), 1, '0', STR_PAD_LEFT);
45+
}
46+
),
47+
array(
48+
2,
49+
3,
50+
"01",
51+
function($j) {
52+
return str_pad(decbin($j), 2, '0', STR_PAD_LEFT);
53+
}
54+
),
55+
array(
56+
3,
57+
7,
58+
"01",
59+
function($j) {
60+
return str_pad(decbin($j), 3, '0', STR_PAD_LEFT);
61+
}
62+
),
63+
array(
64+
8,
65+
255,
66+
"01",
67+
function($j) {
68+
return str_pad(decbin($j), 8, '0', STR_PAD_LEFT);
69+
}
70+
),
71+
array(
72+
16,
73+
255,
74+
"01",
75+
function($j) {
76+
return str_pad(decbin($j), 16, '0', STR_PAD_LEFT);
77+
}
78+
),
79+
array(
80+
1,
81+
15,
82+
"0123456789abcdef",
83+
function($j) {
84+
return str_pad(dechex($j), 1, '0', STR_PAD_LEFT);
85+
}
86+
),
87+
array(
88+
2,
89+
255,
90+
"0123456789abcdef",
91+
function($j) {
92+
return str_pad(dechex($j), 2, '0', STR_PAD_LEFT);
93+
}
94+
),
95+
array(
96+
3,
97+
300,
98+
"0123456789abcdef",
99+
function($j) {
100+
return str_pad(dechex($j), 3, '0', STR_PAD_LEFT);
101+
}
102+
),
103+
);
104+
}
105+
37106
/**
38107
* This test asserts that the algorithm that generates the integers does not
39108
* actually introduce any bias into the generated numbers. If this test
@@ -49,6 +118,21 @@ public function testGenerateInt($min, $max, $offset = 0) {
49118
}
50119
}
51120

121+
/**
122+
* This test asserts that the algorithm that generates the strings does not
123+
* actually introduce any bias into the generated numbers. If this test
124+
* passes, the generated strings from the generator will be as unbiased as
125+
* the sources that provide the data.
126+
*
127+
* @dataProvider provideGenerateString
128+
*/
129+
public function testGenerateString($length, $max, $chrs, $func) {
130+
$generator = $this->getGenerator($max);
131+
for ($i = $max; $i >= 0; $i--) {
132+
$this->assertEquals($func($i), $generator->generateString($length, $chrs));
133+
}
134+
}
135+
52136
public function getGenerator($random) {
53137
$source1 = new Source(array(
54138
'generate' => function ($size) use (&$random) {

0 commit comments

Comments
 (0)