Show Menu
Cheatography

(lib)Sodium C++ Basic Usage Cheat Sheet (DRAFT) by

Quick reference for Sodium: A library for encryption, decryption, signatures, and key exchange. Provides high-level cryptographic primitives.

This is a draft cheat sheet. It is a work in progress and is not finished yet.

What is Sodium?

Sodium is a modern, easy-t­o-use software library for encryp­tion, decryp­tion, signat­ures, password hashing, and more.

It is a portable, cross-­com­pil­able, instal­lable, and packag­eable fork of NaCl, with a compatible but extended API to improve usability even further.

Its goal is to provide all of the core operations needed to build higher­-level crypto­graphic tools.

Sodium is cross-­pla­tform and cross-­lan­guage. It runs on many compilers and operating systems, including Windows (with MinGW or Visual Studio, x86 and x86_64), iOS, and Android. JavaScript and WebAss­embly versions are also available and fully supported. Furthe­rmore, bindings for all common progra­mming languages are available and well-s­upp­orted.

The design choices emphasize security and ease of use. But despite the emphasis on high security, primitives are faster across­-th­e-board than most implem­ent­ations.
Source: doc.li­bso­diu­m.org

Encryp­tio­n/D­ecr­yption

Secret Key Encryption (AES-like)
Encrypt
crypto­_se­cre­tbo­x_e­asy()
Decrypt
crypto­_se­cre­tbo­x_o­pen­_easy()
`
{{lang-cpp}}

unsigned char key[cr­ypt­o_s­ecr­etb­ox_­KEY­BYTES];
unsigned char nonce[­cry­pto­_se­cre­tbo­x_N­ONC­EBY­TES];
unsigned char cipher­tex­t[c­ryp­to_­sec­ret­box­_MA­CBYTES + messag­e_len];
unsigned char decryp­ted­[me­ssa­ge_­len];

crypto­_se­cre­tbo­x_e­asy­(ci­phe­rtext, message, messag­e_len, nonce, key);
crypto­_se­cre­tbo­x_o­pen­_ea­sy(­dec­rypted, cipher­text, sizeof­(ci­phe­rtext), nonce, key);
`

Instal­lation

Linux
Use package managers (e.g., apt-get install libsod­ium­-dev).
Windows
Download precom­piled binaries or build from source.
macOS
Use Homebrew (brew install libsod­ium).
 

Random bytes


unsigned char buf[32];
randombytes_buf(buf, sizeof(buf));