Using Libsodium in PHP Projects

A guide to using the libsodium PHP extension for modern, secure, and fast cryptography. Open Source.

Quick Start Guide

After successfully installing the libsodium PHP extension, you can immediately begin using it to develop secure web applications.

Which Tool to Use, for Which Purpose

Deciding which libsodium feature to use for a given purpose is relatively straightforward, provided you keep these guidelines in mind:

  • Prioritize the basic features (e.g. crypto_box and crypto_secretbox) over advanced features (e.g. crypto_stream_xor).
  • If a more appropriate feature exists (e.g. crypto_pwhash_* instead of crypto_generichash), don't use a less appropriate one.
  • Avoid deploying or publishing your own cryptographic constructions (e.g. generally, you should use crypto_secretbox instead of crypto_stream_xor then crypto_auth).

Important

This documentation was originally written for version 1 of the Sodium extension's API, which used namespaced functions like \Sodium\crypto_box() instead of prefixed functions like sodium_crypto_box().

To reference the older documentation, view the old branch on Github.

To view this page in the old API documentation, click here.

Libsodium Functions

This is a comprehensive list of all the functions available in the libsodium PHP extension.

Libsodium Constants

Constant Value
SODIUM_CRYPTO_AEAD_AES256GCM_KEYBYTES 32
SODIUM_CRYPTO_AEAD_AES256GCM_NSECBYTES 0
SODIUM_CRYPTO_AEAD_AES256GCM_NPUBBYTES 12
SODIUM_CRYPTO_AEAD_AES256GCM_ABYTES 16
SODIUM_CRYPTO_AEAD_CHACHA20POLY1305_KEYBYTES 32
SODIUM_CRYPTO_AEAD_CHACHA20POLY1305_NSECBYTES 0
SODIUM_CRYPTO_AEAD_CHACHA20POLY1305_NPUBBYTES 8
SODIUM_CRYPTO_AEAD_CHACHA20POLY1305_ABYTES 16
SODIUM_CRYPTO_AEAD_CHACHA20POLY1305_IETF_KEYBYTES 32
SODIUM_CRYPTO_AEAD_CHACHA20POLY1305_IETF_NSECBYTES 0
SODIUM_CRYPTO_AEAD_CHACHA20POLY1305_IETF_NPUBBYTES 12
SODIUM_CRYPTO_AEAD_CHACHA20POLY1305_IETF_ABYTES 16
SODIUM_CRYPTO_AUTH_BYTES 32
SODIUM_CRYPTO_AUTH_KEYBYTES 32
SODIUM_CRYPTO_BOX_SEALBYTES 16
SODIUM_CRYPTO_BOX_SECRETKEYBYTES 32
SODIUM_CRYPTO_BOX_PUBLICKEYBYTES 32
SODIUM_CRYPTO_BOX_KEYPAIRBYTES 64
SODIUM_CRYPTO_BOX_MACBYTES 16
SODIUM_CRYPTO_BOX_NONCEBYTES 24
SODIUM_CRYPTO_BOX_SEEDBYTES 32
SODIUM_CRYPTO_KX_BYTES 32
SODIUM_CRYPTO_KX_PUBLICKEYBYTES 32
SODIUM_CRYPTO_KX_SECRETKEYBYTES 32
SODIUM_CRYPTO_GENERICHASH_BYTES 32
SODIUM_CRYPTO_GENERICHASH_BYTES_MIN 16
SODIUM_CRYPTO_GENERICHASH_BYTES_MAX 64
SODIUM_CRYPTO_GENERICHASH_KEYBYTES 32
SODIUM_CRYPTO_GENERICHASH_KEYBYTES_MIN 16
SODIUM_CRYPTO_GENERICHASH_KEYBYTES_MAX 64
SODIUM_CRYPTO_PWHASH_SALTBYTES 16
SODIUM_CRYPTO_PWHASH_STRPREFIX $argon2i$
SODIUM_CRYPTO_PWHASH_OPSLIMIT_INTERACTIVE 4
SODIUM_CRYPTO_PWHASH_MEMLIMIT_INTERACTIVE 33554432
SODIUM_CRYPTO_PWHASH_OPSLIMIT_MODERATE 6
SODIUM_CRYPTO_PWHASH_MEMLIMIT_MODERATE 134217728
SODIUM_CRYPTO_PWHASH_OPSLIMIT_SENSITIVE 8
SODIUM_CRYPTO_PWHASH_MEMLIMIT_SENSITIVE 536870912
SODIUM_CRYPTO_PWHASH_SCRYPTSALSA208SHA256_SALTBYTES 32
SODIUM_CRYPTO_PWHASH_SCRYPTSALSA208SHA256_STRPREFIX $7$
SODIUM_CRYPTO_SCALARMULT_BYTES 32
SODIUM_CRYPTO_SCALARMULT_SCALARBYTES 32
SODIUM_CRYPTO_SHORTHASH_BYTES 8
SODIUM_CRYPTO_SHORTHASH_KEYBYTES 16
SODIUM_CRYPTO_SECRETBOX_KEYBYTES 32
SODIUM_CRYPTO_SECRETBOX_MACBYTES 16
SODIUM_CRYPTO_SECRETBOX_NONCEBYTES 24
SODIUM_CRYPTO_SIGN_BYTES 64
SODIUM_CRYPTO_SIGN_SEEDBYTES 32
SODIUM_CRYPTO_SIGN_PUBLICKEYBYTES 32
SODIUM_CRYPTO_SIGN_SECRETKEYBYTES 64
SODIUM_CRYPTO_SIGN_KEYPAIRBYTES 96
SODIUM_CRYPTO_STREAM_KEYBYTES 32
SODIUM_CRYPTO_STREAM_NONCEBYTES 24
SODIUM_LIBRARY_MAJOR_VERSION 9
SODIUM_LIBRARY_MINOR_VERSION 6 (results may vary)
SODIUM_LIBRARY_VERSION "1.0.14" (results may vary)