2017-08-05 33 views
2

我想實現簡單的加密。PHP paragonie/halite - 缺少鈉常量?

我使用的是最新的PHP 7.1.8,由於mcrypt已經被棄用,所以我瞭解了libsodium。安裝了它,這是我的PHP信息的外觀:

PHP sodium

一切都顯得井然有序。

然後我安裝paragonie /岩鹽V3.2.0,並且只是試圖從GitHub的頁面簡單的例子:

$passwd = new HiddenString('correct horse battery staple'); 
    // Use random_bytes(16); to generate the salt: 
    $salt = "\xdd\x7b\x1e\x38\x75\x9f\x72\x86\x0a\xe9\xc8\x58\xf6\x16\x0d\x3b"; 

    $encryptionKey = KeyFactory::deriveEncryptionKey($passwd, $salt); 

運行此返回:

Fatal error: Uncaught Error: Undefined constant 'Sodium\CRYPTO_PWHASH_OPSLIMIT_INTERACTIVE' in /app/vendor/paragonie/halite/src/KeyFactory.php on line 274

enter image description here

在查看代碼後,我可以看到代碼確實使用了這些常量。

我已經看到在\Sodium命名空間內使用了較舊版本的PHP擴展,並且由於它被確認爲PHP7.2核心擴展,所以函數被重構,以用作sodium_*

也許我錯過了一些東西,但我的問題是,誰能告訴我可能是什麼原因我錯過了這些常量?

謝謝!

+1

是的,這很奇怪。我檢查了源代碼,常量實際上是在[Sodium.stub.php](https://github.com/paragonie/halite/blob/49a324b4c83e52cd8dcf887a9bd71dc832488d22/stub/Sodium.stub.php#L33)中定義的。無論如何,我認爲你應該安裝一個更新版本的libsodium,如[答案]所示(https://stackoverflow.com/questions/37682227/including-paragonie-halite-in-project-doesnt-find-variables-and-functions) @Scott Arciszewski是該項目的維護者,如果答案沒有解決你的問題,你可以ping他 – samayo

+0

非常感謝你的答案@samayo,我會再次檢查版本,並讓你知道如果幫助:) – Adrian

+0

雖然我必須承認,我的擴展名具有'sodium_ *'函數可用,但不是這些常量是奇怪的。我用標準的'pecl install libsodium'安裝了擴展。要嘗試選擇一個特定的版本。 – Adrian

回答

0

我把它用一個簡單的版本規範工作:

pecl install libsodium-1.0.6

不知道這是怎麼回事引擎蓋下,但我的猜測是,我的paragonie/halite版本並沒有完全適應最新sodium PHP擴展。也許這將在即將到來的PHP7.2上得到解決,當它成爲核心PHP擴展。

現在我堅持使用這個版本。