sodium_crypto_secretstream_xchacha20poly1305_init_push
(PHP 7 >= 7.2.0, PHP 8)
sodium_crypto_secretstream_xchacha20poly1305_init_push — 암호화를 위한 secretstream 컨텍스트 초기화
설명
sodium_crypto_secretstream_xchacha20poly1305_init_push(string $key
): array
암호화를 위해 secretstream 컨텍스트를 초기화합니다.
매개변수
key
- 암호화 키. sodium_crypto_secretstream_xchacha20poly1305_keygen()을 참조하십시오.
반환 값
두 개의 문자열 값이 있는 배열:
- 추가 푸시에 필요한 secretstream 상태
- 데이터를 가져올 수 있도록 수신자에게 제공해야 하는 secretstream 헤더
Examples
예제 #1 sodium_crypto_secretstream_xchacha20poly1305_init_push() 예제
<?php
function encrypt_file(string $inputFilePath, string $outputFilePath, string $key): void
{
[$state, $header] = sodium_crypto_secretstream_xchacha20poly1305_init_push($key);
$inputFile = fopen($inputFilePath, 'rb');
$outputFile = fopen($outputFilePath, 'wb');
// Write the header:
fwrite($outputFile, $header);
$inputFileSize = fstat($inputFile)['size'];
// Encrypt the file and write its contents to the output file:
for ($i = 0; $i < $inputFileSize; $i += 8175) {
$ptxt_chunk = fread($inputFile, 8175);
$ctxt_chunk = sodium_crypto_secretstream_xchacha20poly1305_push($state, $ptxt_chunk);
fwrite($outputFile, $ctxt_chunk);
}
sodium_memzero($state);
fclose($inputFile);
fclose($outputFile);
}
// sodium_crypto_secretstream_xchacha20poly1305_keygen()
$key = sodium_base642bin('MS0lzb7HC+thY6jY01pkTE/cwsQxnRq0/2L1eL4Hxn8=', SODIUM_BASE64_VARIANT_ORIGINAL);
file_put_contents('hello.txt', 'Hello world!');
encrypt_file('hello.txt', 'hello.txt.encrypted', $key);
var_dump(sodium_bin2hex(file_get_contents('hello.txt.encrypted')));
?>
위의 예는 다음과 유사한 결과를 출력합니다.
string(106) "971e33b255f0990ef3931caf761c59136efa77b434832f28ec719e3ff73f5aec38b3bba1574ab5b70a8844d8da36a668e802cfea2c"