sodium_bin2base64

(PHP 7 >= 7.2.0, PHP 8)

sodium_bin2base64 — Codifica una string binaria bruta en base64.

Descripción

function sodium_bin2base64(#[\SensitiveParameter] string $string, int $id): string

Convierte una string binaria bruta en una string codificada en base64. A diferencia de base64_encode(), sodium_bin2base64() es de tiempo constante (una propiedad importante para cualquier código que manipule entradas criptográficas, como textos en claro o claves) y admite varios conjuntos de caracteres.

Parámetros

string

La string binaria bruta.

id

Valores devueltos

La string codificada en base64.

Ejemplos

Ejemplo #1 Ejemplo de sodium_bin2base64()

<?php
$string = 'This is an encoded string';
echo sodium_bin2base64($string, SODIUM_BASE64_VARIANT_ORIGINAL);
?>

El ejemplo anterior mostrará:

VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw==

Ejemplo #2 Ejemplo del parámetro id

<?php
$binaryString = sodium_hex2bin('aeb6798fef4e5184ff3b5596af753487');
var_dump(sodium_bin2base64($binaryString, SODIUM_BASE64_VARIANT_ORIGINAL));
var_dump(sodium_bin2base64($binaryString, SODIUM_BASE64_VARIANT_ORIGINAL_NO_PADDING));
var_dump(sodium_bin2base64($binaryString, SODIUM_BASE64_VARIANT_URLSAFE));
var_dump(sodium_bin2base64($binaryString, SODIUM_BASE64_VARIANT_URLSAFE_NO_PADDING));
?>

El ejemplo anterior mostrará:

string(24) "rrZ5j+9OUYT/O1WWr3U0hw=="
string(22) "rrZ5j+9OUYT/O1WWr3U0hw"
string(24) "rrZ5j-9OUYT_O1WWr3U0hw=="
string(22) "rrZ5j-9OUYT_O1WWr3U0hw"

Véase también

+add a note

User Contributed Notes 1 note

up
7
davidw at example dot com
7 years ago
bin: The data you wish to encode
id: The variant of encoding to use, which can be one of the following constants. You'll need to reuse this value when decoding with sodium_base642bin.

SODIUM_BASE64_VARIANT_ORIGINAL            = 1
SODIUM_BASE64_VARIANT_ORIGINAL_NO_PADDING = 3
SODIUM_BASE64_VARIANT_URLSAFE             = 5
SODIUM_BASE64_VARIANT_URLSAFE_NO_PADDING  = 7