PHP 8.1.28 Released!

sodium_crypto_stream_xchacha20_xor_ic

(PHP 8 >= 8.2.0)

sodium_crypto_stream_xchacha20_xor_icnonce と秘密鍵を使ってメッセージを暗号化する(認証なし)

説明

sodium_crypto_stream_xchacha20_xor_ic(
    string $message,
    string $nonce,
    int $counter,
    string $key
): string

この関数は、sodium_crypto_stream_xchacha20_xor() に似ていますが、ブロックカウンタの初期値を非ゼロの値に設定する機能が追加されています。 これによって、以前の値を計算せずに直接任意のブロックにアクセスすることができます。

警告

この暗号化処理は認証を行いませんし、 選択暗号文攻撃(chosen-ciphertext attack) を防ぐことができません。 必ず暗号化されたテキストを認証コードと組み合わせるようにして下さい。 たとえば、 sodium_crypto_aead_xchacha20poly1305_ietf_encrypt()sodium_crypto_auth() を使うことが考えられます。

パラメータ

message

暗号化するメッセージ。

nonce

24バイトの nonce。

counter

ブロックカウンタの初期値。

key

暗号化キー。 sodium_crypto_stream_xchacha20_keygen() で生成されたものです。

戻り値

暗号化されたメッセージを返します。 失敗した場合に false を返します

例1 sodium_crypto_stream_xchacha20_xor_ic() の例

<?php
$n2
= random_bytes(SODIUM_CRYPTO_STREAM_XCHACHA20_NONCEBYTES);
$left = str_repeat("\x01", 64);
$right = str_repeat("\xfe", 64);

// All at once:
$stream7_unified = sodium_crypto_stream_xchacha20_xor($left . $right, $n2, $key);

// Piecewise, with initial counter:
$stream7_left = sodium_crypto_stream_xchacha20_xor_ic($left, $n2, 0, $key);
$stream7_right = sodium_crypto_stream_xchacha20_xor_ic($right, $n2, 1, $key);
$stream7_concat = $stream7_left . $stream7_right;

var_dump(strlen($stream7_concat));
var_dump($stream7_unified === $stream7_concat);
?>

上の例の出力は、 たとえば以下のようになります。

int(128)
bool(true)

参考

add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top