<?php
class Buffer extends \SplFixedArray
{
private function fill(string $data = '')
{
for ($i = 0, $l = $this->getSize(); $i < $l; $i++) {
$this[$i] = $data[$i] ?? 0;
}
}
public function __construct($arg)
{
if (\is_int($arg)) {
parent::__construct($arg);
$this->fill();
return;
}
if (\is_string($arg)) {
parent::__construct(\strlen($arg));
$this->fill($arg);
return;
}
}
public function setSize($value)
{
throw new \Error('Buffer cannot be resized');
}
public function offsetSet($index, $byte)
{
if (\is_int($byte) && $byte >= 0 && $byte <= 255) {
parent::offsetSet($index, $byte);
return;
}
if (!\is_string($byte) || \strlen($byte) != 1 || ($byte = \ord($byte)) < 0 || $byte > 255) {
throw new \Error('Buffer element value must be integer in the range 0 - 255 or single-byte string');
}
parent::offsetSet($index, $byte);
}
public function offsetUnset($index)
{
throw new \Error('Buffer elements cannot be unset');
}
public function __toString()
{
return \implode('', \array_map('chr', $this->toArray()));
}
}
$publicKey = new Buffer(\hex2bin('F1F5AC76CC3BCA543DBF7DEBFE77101343B72ABE582FF1D2E8767916EA792F32'));
$signature = new Buffer(\hex2bin('4D363B793632444DC36FCB78F7993FC1C04FCA43F498ABC883DE772A7E616C98290D326306ABC772370E73338FD34D24B207F0F69F8652004001909B6907E986'));
$publicKey[31] &= 0x7f;
$publicKey[31] |= ($signature[63] & 0x80);
$signature[63] &= 0x7f;
var_dump(bin2hex((string)$publicKey));
var_dump(bin2hex((string)$signature));
Deprecated: Return type of Buffer::offsetSet($index, $byte) should either be compatible with SplFixedArray::offsetSet($index, mixed $value): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/4Jata on line 35
Deprecated: Return type of Buffer::offsetUnset($index) should either be compatible with SplFixedArray::offsetUnset($index): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/4Jata on line 49
string(64) "f1f5ac76cc3bca543dbf7debfe77101343b72abe582ff1d2e8767916ea792fb2"
string(128) "4d363b793632444dc36fcb78f7993fc1c04fca43f498abc883de772a7e616c98290d326306abc772370e73338fd34d24b207f0f69f8652004001909b6907e906"
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
Deprecated: Return type of Buffer::offsetSet($index, $byte) should either be compatible with SplFixedArray::offsetSet($index, mixed $value): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/4Jata on line 35
Deprecated: Return type of Buffer::offsetUnset($index) should either be compatible with SplFixedArray::offsetUnset($index): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /in/4Jata on line 49
string(64) "f1f5ac76cc3bca543dbf7debfe77101343b72abe582ff1d2e8767916ea792fb2"
string(128) "4d363b793632444dc36fcb78f7993fc1c04fca43f498abc883de772a7e616c98290d326306abc772370e73338fd34d24b207f0f69f8652004001909b6907e906"