Core\Secret::revealBytes
Core\Secret::revealBytes(bytes $value, string $reason): bytesCore\Secret::revealBytes( bytes $value, string $reason, ): bytes
Core\Secret::revealBytes() returns secret bytes as plain bytes.
It works like Core\Secret::reveal(), for binary data such as an encryption key. Keys that Core\Crypto creates are secret bytes, so your program cannot print them, log them or save them by mistake. When the program must save a key or send it somewhere, it calls revealBytes() on that line, with a reason for the people who read the code.
The result is the same bytes, and every byte is kept, including bytes that are not valid text. A value that was also tainted stays tainted.
The examples below measure a new key, turn a key made from a password into hex, and write a new key into a line for a configuration file.
Parameters
$valuebytesThe secret bytes. Plainbytesare also allowed, and are returned unchanged.$reasonstringWhy this line needs the secret. It is for the people who read the code, and the program never reads it. Asecretvalue is not allowed here.
Return value
Returns bytes. The same bytes, as bytes. If $value was also tainted, the result is still tainted.
Examples
Measure a new key
<?nvs// Shows how to check the length of a new encryption key.
// `Core\Crypto::generateKey` returns a new random key as `secret bytes`.secret bytes $key = Core\Crypto::generateKey();
// `Core\Secret::revealBytes` returns the same bytes as plain `bytes`.bytes $raw = Core\Secret::revealBytes($key, 'the setup check measures the key');
// A key is 32 bytes, so this prints 32.echo Core\Bytes::length($raw), "\n";32A key made from a password
<?nvs// Shows how to turn a key made from a password into hex text.
secret string $password = 'correct horse battery staple';bytes $salt = Core\Bytes::fill(16, 7);
// `Core\Crypto::deriveKey` makes a key from a password and a salt.// The same password and salt always give the same key.secret bytes $key = Core\Crypto::deriveKey($password, $salt, 100000);
// `Core\Encoding::toHex` writes each byte as two hex digits.// This prints 64 hex digits, one pair for each of the 32 bytes.bytes $raw = Core\Secret::revealBytes($key, 'the key is compared with the one on the server');echo Core\Encoding::toHex($raw), "\n";2f553eab602970df3ea9c348110aa66dd05281e3a8fd717cbee7eeea9cd4c565Save a new key for the config file
<?nvs// Shows how to write a new encryption key into a line for a configuration file.
secret bytes $key = Core\Crypto::generateKey();
// Base64 turns the 32 bytes into text that fits on one line.bytes $raw = Core\Secret::revealBytes($key, 'the setup command writes the key to the config file');string $line = 'APP_KEY=' . Core\Encoding::toBase64($raw);
// The key is new every time, so this prints only the parts that stay the same.// This prints "APP_KEY=" and then 52, the length of the whole line.echo Core\Str::slice($line, 0, 8), "\n";echo Core\Str::length($line), "\n";APP_KEY=52