sodium_bin2base64

(PHP 7 >= 7.2.0, PHP 8)

sodium_bin2base64 — Encode une chaîne binaire brute en base64.

Description

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

Convertit une chaîne binaire brute en une chaîne encodée en base64. Contrairement à base64_encode(), sodium_bin2base64() est en temps constant (une propriété importante pour tout code qui manipule des entrées cryptographiques, telles que des textes en clair ou des clés) et prend en charge plusieurs jeux de caractères.

Liste de paramètres

string

La chaîne binaire brute.

id

Valeurs de retour

La chaîne encodée en base64.

Exemples

Exemple #1 Exemple de sodium_bin2base64()

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

L'exemple ci-dessus va afficher :

VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw==

Exemple #2 Exemple du paramètre 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));
?>

L'exemple ci-dessus va afficher :

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

Voir aussi

+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