frank
Goto Top

Libsodium Extension als Alternative für entfernte Mcrypt Extension installieren (ab PHP 7.2)

Da die PHP Mcrypt Extension ab PHP 7.2 entfernt wird, suche ich schon einige Zeit lang eine gute und sichere Alternative. Siehe dazu auch PHP RFC: Mcrypt Extension wird ab PHP 7.2 entfernt.

Meine Lösung heißt: Libsodium (a portable, cross-compilable, installable, packageable, API-compatible version of NaCl (Networking and Cryptography library)).

Der folgende Installationsvorgang ist nur für PHP Versionen kleiner als 7.2.0 nötig. Alle Versionen darüber haben libsodium bereits integriert.

Dazu muss man die libsodium PHP Extension installieren (Alternativ gibt es sie auch auf GitHUB):
pecl install libsodium
und in die php.ini folgende Zeile hinzufügen:
extension=sodium.so

Dann kann man überprüfen, ob alle funktioniert:
<?php
if (extension_loaded("sodium")) {  
    $constants = get_defined_constants(true);
    var_dump([
        SODIUM_LIBRARY_VERSION,
        get_extension_funcs('sodium'),  
        $constants["sodium"]  
    ]);
} else {
    // sodium not found
}

Es sollte die aktuelle Version der libsodium Extension erscheinen (kann natürlich variieren)
$ php version_check.php
array(3) {
  =>
  string(6) "1.0.15"  
  [1]=>
  array(82) {
    =>
    string(41) "sodium_crypto_aead_aes256gcm_is_available"  
    [1]=>
    string(36) "sodium_crypto_aead_aes256gcm_decrypt"  
    [2]=>
    string(36) "sodium_crypto_aead_aes256gcm_encrypt"  
    [3]=>
    string(43) "sodium_crypto_aead_chacha20poly1305_decrypt"  
    [4]=>
    string(43) "sodium_crypto_aead_chacha20poly1305_encrypt"  
    [5]=>
    usw.
}

Eine detailierte Installationsanleitung findet ihr hier:
https://paragonie.com/book/pecl-libsodium/read/00-intro.md#installing-li ...


Sichere Passwörter mit PHP erzeugen (benutzt Argon2 - Gewinner der Hashing Competition 2015)

// Password hashing:
$hash_str = sodium_crypto_pwhash_str(
    $password,
    SODIUM_CRYPTO_PWHASH_OPSLIMIT_INTERACTIVE,
    SODIUM_CRYPTO_PWHASH_MEMLIMIT_INTERACTIVE
);
// Password verification:
if (sodium_crypto_pwhash_str_verify($hash_str, $password)) {
    // recommended: wipe the plaintext password from memory
    sodium_memzero($password);
    
    // Password was valid.
} else {
    // recommended: wipe the plaintext password from memory
    sodium_memzero($password);
    
    // Password was invalid.
}


Basic Encryption

$key = sodium_randombytes_buf(SODIUM_CRYPTO_SECRETBOX_KEYBYTES);

// Using your key to encrypt information
$nonce = sodium_randombytes_buf(SODIUM_CRYPTO_SECRETBOX_NONCEBYTES);
$ciphertext = sodium_crypto_secretbox('test', $nonce, $key);  


Basic Decryption

Dazu brauchen wir als Eingabe natürlich das selbe "nonce" und den selben "key" (siehe oben) um die Nachricht wieder zu entschlüsseln.

$plaintext = sodium_crypto_secretbox_open($ciphertext, $nonce, $key);
if ($plaintext === false) {
    throw new Exception("Bad ciphertext");  
}


Hier eine erweitere Version mit AES-256 + GCM:

AEAD Encryption - Authenticated (secret-key) Encryption with Associated Data - AES-256 + GCM

$key = sodium_randombytes_buf(SODIUM_CRYPTO_AEAD_AES256GCM_KEYBYTES);
if (sodium_crypto_aead_aes256gcm_is_available()) {
    $nonce = sodium_randombytes_buf(SODIUM_CRYPTO_AEAD_AES256GCM_NPUBBYTES);
    $ad = 'Additional (public) data';  
    $ciphertext = sodium_crypto_aead_aes256gcm_encrypt(
        $message,
        $ad,
        $nonce,
        $key
    );
}


AEAD Decryption

Dazu brauchen wir als Eingabe natürlich das selbe "nonce" und den selben "key" (siehe oben) um die Nachricht wieder zu entschlüsseln.

if (sodium_crypto_aead_aes256gcm_is_available()) {
    $decrypted = sodium_crypto_aead_aes256gcm_decrypt(
        $ciphertext,
        $ad,
        $nonce,
        $key
    );
    if ($decrypted === false) {
        throw new Exception("Bad ciphertext");  
    }
}

Weitere Informationen und Beispiele für PHP findet ihr auf dieser Seite:
https://paragonie.com/book/pecl-libsodium

Viel Spaß beim Umbauen ...

Update vom 30.11.2017

Die Version PHP 7.2.0 hat seit dem 30.11.2017 libsodium integriert. Es wird libsodium-php 2.x verwendet. In der ursprünglichen Anleitung wurde libsodium-php 1.x verwendet. Ich habe die Codebeispiele für die Version 2.x angepasst. Auch https://paragonie.com/book/pecl-libsodium hat seine Beispiele an die neue Version angepasst.


Gruß
Frank

Content-Key: 327712

Url: https://administrator.de/contentid/327712

Ausgedruckt am: 19.03.2024 um 07:03 Uhr

Mitglied: Dani
Dani 21.02.2017 um 10:36:45 Uhr
Goto Top
Mitglied: Frank
Frank 30.11.2017 aktualisiert um 21:10:04 Uhr
Goto Top
Die Anleitung wurde zum heutigen Erscheinen von PHP 7.2.0 (30.11.2017) auf libsodium-php 2.x aktualisiert. libsodium ab jetzt fester Bestandteil von PHP.

Migrationshinweise für libsodium-php 1.x (< PHP 7.2) vs libsodium-php 2.x (>=PHP 7.2)

This extension was originally named libsodium. The module was named libsodium.so or libsodium.dll.

All the related functions and constants were contained within the \Sodium\ namespace.

This extension was accepted to be distributed with PHP >= 7.2, albeit with a couple breaking changes:

  • No more \Sodium\ namespace. Everything must be in the global namespace.
  • The extension should be renamed sodium. So, the module becomes sodium.so or sodium.dll.

The standalone extension (this repository; also the extension available on PECL) was updated to match these expectations, so that its API is compatible with what will be shipped with PHP 7.2.

libsodium-php 2.x is thus not compatible with libsodium-php 1.x.
The 1.x branch will not receive any public updates any more.


Gruß
Frank