I'm working with a slider control. I'm trying to do a decimal to hex or not exactly, it is a regular number range without a decimal point. And for reference:
0 is zero
00 is 100
50 is 50
(meaning the numbers are always 1 or 2 digits only on the slider control.)
(and the hex also needs to be only 2 digits.)
I know about dechex. Is that what I would use for this? Or if its not a number with a decimal point is there another command other than dechex for this.
And how do I set the range to only manage the control from 0 to 00?
I am learning. I don't have a clue how to write the php so any and all comments might help. Thanks for the help! Any questions let me know too.
<?php
function convert($s) {
print "CONVERT: $s\n";
$s = "X".$s;
switch($s) {
case "X00":
return dechex(100);
break;
case "X0":
return null;
break;
default:
return dechex(substr($s,1));
break;
}
}
print convert('0')."\n";
print convert('1')."\n";
print convert('50')."\n";
print convert('99')."\n";
print convert('00')."\n";
output:
CONVERT: 0
CONVERT: 1
1
CONVERT: 50
32
CONVERT: 99
63
CONVERT: 00
64
PHP does do some automatich type conversion, and does not know the difference between "0" en "00", that's why I am adding an "X" in front of it to know the difference (and I am comparing strings).
P.S.: some changes might need to be done to meet your exact expectations...
This question already has answers here:
Laravel AES-256 Encryption & MySQL
(2 answers)
Closed 2 years ago.
Setup
Given the following:
$s = Crypt::encryptString('a');
Is is possible to know, for a string of length 1, the possible range of lengths of $s?
Context
Database storage - need to store an encrypted value, and would like to set validation of the input string so the longest length input string, when encrypted, is inserted into the db without truncation.
Basic tests
Running some very crude tests locally, using the following snippet:
Route::get('/test', function() {
echo '<table>';
for ($i=0; $i < 100; $i++) {
$s = str_repeat('a', $i);
$l1 = strlen($s);
$l2 = strlen(Crypt::encryptString($s));
echo "<tr><td>$l1</td><td>$l2</td></tr>";
}
echo '</table>';
});
I can see the following, but it varies between runs, for example, a string of 'a' will be of length of either 188 or 192 (longer values seem to be between 244 and 248).
So there must be a formula. I have seen output_size = input_size + (16 - (input_size % 16)) but doesn't account for the variance.
Output
0 192
1 188
2 188
3 192
4 188
5 188
6 188
7 192
8 192
9 188
10 188
11 192
12 192
13 192
14 192
15 192
16 220
17 220
18 216
19 216
20 220
Edit
Ok, so after chatting with #Luke Joshua Park below, the variance in length comes from the laravel encryption function and the way $iv is created, which is random bytes, which can contain /.
$value inside the encryption method can also contain a /.
When values that contain a / are JSON encoded, the / is escaped to \\\/ adding an additional 3 characters per occurrence.
The real problem - can $iv and $value contain more than a single '/'?
Looking through the source code for Crypt::encryptString, we can see that the final result will be a base64 encoded JSON object that has the following structure:
{ "iv": "<128 bits in base64>", "value": "<x bits in base64>", "mac": "<256 bits in hex>" }
Where the value of x is ceil(n / 128) * 128 where n is the number of bits in the original plaintext.
This means that, for an input plaintext of length 1, the size of the output should be:
24 characters for the IV (base64).
24 characters for the ciphertext (base64).
64 characters for the SHA256 mac (hex).
10 characters for the names of the JSON fields.
19 characters of extra JSON characters e.g. {, ", :.
A final round of base64 encoding of the whole thing... (ceil(141 / 3) * 4)
Gives a total of 188. The fluctuations up to 192 are odd - your inputs are not changing in size at all (since the plaintext should always be 16 bytes between 0 - 15 length).
The real problem - can $iv and $value contain more than a single '/'?
Sure. Your worst case for the IV is the IV FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF (hex), which has a Base64 value of /////////////////////w==.
21 forward slashes * extra 3 bytes each = 63 extra bytes.
For the HMAC-SHA-2-256, you could get 32 bytes of 0xFF (worst case), which is //////////////////////////////////////////8= in base64.
42 forward slashes => 126 extra bytes.
For the ciphertext, again, the entire output could be (but likely isn't) FF FF ... FF. All one letter inputs (no matter what encoding) are a single block of ciphertext, making the output be /////////////////////w== again (+63).
The generalized formula for the maximum seems to be
IV: 24 + 63 = 87
HMAC: 24 + 63 = 87
JSON Property Names: 10
JSON Structure: 19
Ciphertext: ceil(ceil((n+1) / 16) * 16 / 3) * 4 * 4 (I used n as bytes. padded ciphertext is ceil((n+1) / blocksize) * blocksize, base64 is 4 * ceil(data / 3), extra *4 is "everything is slashes")
Base64 it all again: 4 * ceil(sum / 3)
= 4 * ceil((4 * 4 * ceil(16 * ceil((n + 1) / 16) / 3) + 203) / 3)
For n=1 that produces 400 bytes. The actual maximum is (I think) 388, because the ciphertext formula is counting 24 slashes as the worst case when 21 is the worst case. So the true supremum needs to call the ciphertext something more complicated involving floor, ceiling, and subtraction.
Note I'm going to award the bounty to #Luke Joshua Park as he got me closest to what ended up being the (closest thing to a) solution, which is to follow.
(Not a) solution
The answer is, there is no concrete answer, not without unknowns and variance. Across the three people looking at this at the time of writing (myself, Luke, and bartonjs) there was still some doubt to a 100% accurate solution.
The question was posed to figure out a reliable type and size to store encrypted data, ideally in a database independent fashion (I didn't want to specify a particular database, as I wanted to know and understand how to calculate a length regardless of the way it was persisted).
However, even strings of the smallest lengths turned out to be quite long in the worst case scenario (where a random $iv was created containing many slashes - unlikely or not, it was possible). Possible encrypted strings of n=1 possibly being 400 bytes long mean that a varchar will never be the right answer.
So... what should be done?
So, instead, it seems best, most consistent and most reliable to store encrypted data as a text field and not a varchar (in mysql land), regardless of the length of the original string. This is a disappointingly boring answer with no fancy maths involved. It's not the answer I would like to accept, but makes the most sense.
But, what about passwords?
In a brief moment of stupidity, I thought, but what about the password field? That is a varchar. But of course that is a hashed value, not an encrypted value (I hadn't had enough coffee when that thought popped into my head, ok?)
I have a string which uses 128B and 128C conversion. ANCV0005YRF01234.
So
ANCV = 128B
0005 = 128C
YRF0= 128B
1234= 128 C
Cant use code 128 Auto as it converts the 0 after F into 128C (which i dont want.). At the moment using two different scripts and concatenating the barcode images,Need to calculate the check digit for that. Not sure how the check digit will be generated ?.
Any help is appreciated. Thanks
I got sum of characters by weights (rightmost column) from your example string = 5468 % 103 = 9 for a checksum if you want to switch back and forth.
I'm looking for a concept istead of a code.
I have this alphanumrical string: abcdefghijklmnopqrstuvwxyz1234567890
and I want my combination's lenght is , for example, 16
In other words I ask the main concept to have all possible combination mixes like this:
a01g51bhk898qw233
max lenght = 16
An example in php is appreciated but since I would use it in cross-platform scripts I was also interested in the explanation of the "how to"
Very Thanks
Oscar
Update Question
it sound like "I take each array element and create the maximum combination limiting to 16 char"....
I try to make this simple example with 2 element string and max lenght 4
MyStr = "A0"
MaxLen = 4
So My result is
AAAA
A000
AA00
AAA0
A0A0
A0AA
A00A
AA0A
0000
0AAA
00AA
000A
0A0A
0A00
0AA0
00A0
Another more simple example, with 4 element string and max lenght 2
MyStr = "A012"
MaxLen = 2
So My result is
AA
A0
A1
A2
0A
00
01
02
1A
10
11
12
2A
20
21
22
Now it's more clear?
you just need to look around stackoverflow ^_^ , i did it for you you just have to limit the lenght
How to create possible combinations of strings from string in php?
This May help:
$string=substr(str_shuffle("abcdefghijklmnopqrstuvwxyz1234567890"),0,16);
Explanation:
The function str_shuffle() shuffles the string in an random way.
The function substr() returns first 16 characters of the random string.
I'm just wondering, does anyone know, whether uniqid function in PHP has a certain size? And If so, how many strings is it? I'm a bit confused as PHP manual says, that uniqid is 23 characters long when extended entropy used. But based on my observation, it varies between 26 to 28 characters.
I cant confirm this behaviour:
php -r 'for($i = 0; $i < 1000; $i++) print strlen(uniqid("", true)) . "\n";'
Tested on OS X and Ubuntu with PHP 5.3.x yields 23 on each iteration.
What os/PHP version are you using?
As the OP pointed out in the comments to his question: leaving out the prefix should fix the issue ;)
To elaborate a bit more, one can look at the PHP source code: https://svn.php.net/viewvc/php/php-src/tags/php_5_3_8/ext/standard/uniqid.c?view=markup at line 79:
spprintf(&uniqid, 0, "%s%08x%05x%.8F", prefix, sec, usec, php_combined_lcg(TSRMLS_C) * 10);
If no prefix is supplied and one looks up the definition of php_combined_lcg which returns a float with one leading decimal the output of uniqid should always be 23 chars long.