sodium_base642bin

(PHP 7 >= 7.2.0, PHP 8)

sodium_base642bin — Décode une chaîne encodée en base64 en binaire brut.

Description

function sodium_base642bin(#[\SensitiveParameter] string $string, int $id, string $ignore = ""): string

Convertit une chaîne encodée en base64 en binaire brut. Contrairement à base64_decode(), sodium_base642bin() 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

string; Chaîne encodée.

id
ignore

Les caractères à ignorer lors du décodage (par exemple, les caractères d'espacement).

Valeurs de retour

La chaîne décodée.

Exemples

Exemple #1 Exemple de sodium_base642bin()

<?php
$string = 'VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw==';
echo sodium_base642bin($string, SODIUM_BASE64_VARIANT_ORIGINAL);
?>

L'exemple ci-dessus va afficher :

This is an encoded string

Exemple #2 Exemple du paramètre id

<?php
var_dump(sodium_bin2hex(sodium_base642bin('rrZ5j+9OUYT/O1WWr3U0hw==', SODIUM_BASE64_VARIANT_ORIGINAL)));
var_dump(sodium_bin2hex(sodium_base642bin('rrZ5j+9OUYT/O1WWr3U0hw', SODIUM_BASE64_VARIANT_ORIGINAL_NO_PADDING)));
var_dump(sodium_bin2hex(sodium_base642bin('rrZ5j-9OUYT_O1WWr3U0hw==', SODIUM_BASE64_VARIANT_URLSAFE)));
var_dump(sodium_bin2hex(sodium_base642bin('rrZ5j-9OUYT_O1WWr3U0hw', SODIUM_BASE64_VARIANT_URLSAFE_NO_PADDING)));
?>

L'exemple ci-dessus va afficher :

string(32) "aeb6798fef4e5184ff3b5596af753487"
string(32) "aeb6798fef4e5184ff3b5596af753487"
string(32) "aeb6798fef4e5184ff3b5596af753487"
string(32) "aeb6798fef4e5184ff3b5596af753487"

Exemple #3 Exemple du paramètre ignore

<?php
$string = <<<'STR'
    VGhpcyBpcy
    BhbiBlbmNv
    ZGVkIHN0cm
    luZw==
STR;
var_dump(sodium_base642bin($string, SODIUM_BASE64_VARIANT_ORIGINAL, " \r\n"));
?>

L'exemple ci-dessus va afficher :

string(25) "This is an encoded string"

Voir aussi