sodium_crypto_secretbox

(PHP 7 >= 7.2.0, PHP 8)

sodium_crypto_secretbox - 인증된 공유 키 암호화


설명

sodium_crypto_secretbox(string $message, string $nonce, string $key): string

대칭(공유) 키로 메시지를 암호화합니다.


매개변수

message
암호화할 일반 텍스트 메시지입니다.
nonce
메시지당 한 번만 사용해야 하는 번호입니다. 24바이트 길이. 이것은 무작위로 생성하기에 충분히 큰 범위입니다(즉, random_bytes()).
key
암호화 키(256비트).

반환 값

암호화된 문자열을 반환합니다.


오류/예외


Examples

예제 #1 sodium_crypto_secretbox() 예제

                  
<?php
// The $key must be kept confidential
$key = sodium_crypto_secretbox_keygen();
// Do not reuse $nonce with the same key
$nonce = random_bytes(SODIUM_CRYPTO_SECRETBOX_NONCEBYTES);
$plaintext = "message to be encrypted";
$ciphertext = sodium_crypto_secretbox($plaintext, $nonce, $key);

var_dump(bin2hex($ciphertext));
// The same nouce and key are required to decrypt the $ciphertext
var_dump(sodium_crypto_secretbox_open($ciphertext, $nonce, $key));
?>
                  
                

위의 예는 다음과 유사한 결과를 출력합니다.

string(78) "3a1fa3e9f7b72ef8be51d40abf8e296c6899c185d07b18b4c93e7f26aa776d24c50852cd6b1076"
string(23) "message to be encrypted"
                

기타