sodium_memcmp

(PHP 7 >= 7.2.0, PHP 8)

sodium_memcmp定数時間での文字列の比較

説明

sodium_memcmp(string $string1, string $string2): int

ふたつの文字列を、定数時間で比較します。

実際、ほとんどの場合は hash_equals() を使いたいかもしれません。 なぜなら、同じロジックを提供しているにもかかわらず、 整数型ではなく、bool を返すからです。 しかしながら、タイミング攻撃に敏感な比較計算の結果を扱う場合、 かつ、bool値から整数型への変換の際の、 タイミングに依存した情報のリークを気にする場合、 sodium_memcmp() 関数が理想的な代替になります。

パラメータ

string1

比較対象の文字列

string2

もう一つの、比較する文字列

戻り値

双方の文字列が等しければ 0 を返します。 等しくない場合 -1 を返します。

add a note add a note

User Contributed Notes 1 note

up
0
divinity76 at gmail dot com
4 years ago
seems to me that this function does the same as the hash_equals() function. hash_equals() has nothing to do with hashes really, it is just a constant-time string equality check function, apparently like sodium_memcmp()
To Top