Skip to content

Commit 174ee3e

Browse files
committed
Various improvements and ruggedizations
Improvements include the following: - Include Pipfile for easily setting a Pipenv virtual environment for running the Python examples - Improved README build instructions for libsodium and Python examples - Ruggedized libsodium CMake build to help prevent linker errors on some platforms - Improved comments in nacl_encrypt_file.c libsodium file encryption example to warn that it is suitable only for files which can fit in the computer's RAM and a stream-based encryption should be used for larger files
1 parent 638f154 commit 174ee3e

File tree

6 files changed

+402
-5
lines changed

6 files changed

+402
-5
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ dkms.conf
5454
# Python
5555
__pycache__
5656
*.pyc
57+
.venv
58+
Pipfile.lock
5759

5860
* CLion and/or PyCharm IDEs
5961
.idea

Pipfile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[[source]]
2+
name = "pypi"
3+
url = "https://pypi.org/simple"
4+
verify_ssl = true
5+
6+
[dev-packages]
7+
ipython = "*"
8+
9+
[packages]
10+
cryptography = "*"
11+
colorama = "*"
12+
pynacl = "*"

README.md

Lines changed: 77 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,81 @@ use the cryptography module.
9393
Building
9494
========
9595

96+
libsodium C examples
97+
--------------------
98+
99+
The [libsodium](https://download.libsodium.org/doc/) C code examples are all in the **sodium** directory and can be
100+
built using the [Cmake](https://cmake.org) cross-platform build tool along with your platform default C compiler
101+
installed on Windows, macOS, or Linux.
102+
103+
The first stage of building is the same on all platforms:
104+
105+
```bash
106+
cd sodium
107+
rm -rf build
108+
mkdir build
109+
cd build
110+
cmake ..
111+
```
112+
113+
The second stage of building is platform dependent and will create the following executable files:
114+
115+
* hello_sodium
116+
* nacl_keygen
117+
* nacl_sign
118+
* nacl_verify
119+
* symmetric_decrypt
120+
* symmetric_encrypt
121+
* symmetric_keygen
122+
* test_ed25519
123+
* test_pynacl_compatibility
124+
125+
### Linux or macOS
126+
```bash
127+
make
128+
```
129+
130+
This produces the executable files directly in the **build** directory.
131+
132+
### Windows
133+
```bash
134+
devenv hello_sodium.sln /build Debug
135+
```
136+
This creates the executable files under the **build\Debug** directory.
137+
138+
Python examples
139+
---------------
140+
141+
The Python examples are located in the root directory and should work with Python 3.4 or newer. The Python examples
142+
require a mix of the following Python packages:
143+
144+
* [cryptography](https://cryptography.io/en/latest/) - high-level wrapper around [OpenSSL](https://www.openssl.org)
145+
* [pynacl](https://pynacl.readthedocs.io/en/stable/) - Python binding to [libsodium](https://libsodium.org)
146+
* [colorama](https://github.com/tartley/colorama) - cross-platform colored terminal text
147+
148+
The required dependencies can easily be installed using [Pipenv](https://github.com/pypa/pipenv):
149+
```shell script
150+
pipenv install
151+
```
152+
153+
Then a shell using the underlying virtual environment can be entered with:
154+
```shell script
155+
pipenv shell
156+
```
157+
158+
Inside that Pipenv shell, any of the examples can be ran directly. e.g.:
159+
```shell script
160+
python ./aes_gcm_cryptography.py
161+
```
162+
163+
The Python examples are intended to interoperate with either the libsodium or mbedTLS C code examples. Thus encryption
164+
or signing can be done in C and decryption or verifying can be done in Python or vice versa.
165+
166+
mbedtls C examples
167+
------------------
168+
The [mbedTLS](https://github.com/ARMmbed/mbedtls) C code examples are located in the root directory and build mbedTLS
169+
from source from the **mbedtls** directory.
170+
96171
Build requires CMake and platform default C compiler installed and works on both Windows, macOS, and Linux.
97172

98173
The first stage of building is the same on all platforms:
@@ -106,8 +181,7 @@ cmake ..
106181

107182
The second stage of building is platform dependent ...
108183

109-
Linux or macOS
110-
--------------
184+
### Linux or macOS
111185
```bash
112186
make
113187
```
@@ -120,8 +194,7 @@ This produces the following executable files directly in the **build** directory
120194
* kdf
121195
* rsa_signature
122196

123-
Windows
124-
-------
197+
### Windows
125198
```bash
126199
devenv mbed_AES.sln /build Debug
127200
```

sodium/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,17 @@ project("hello_sodium" C)
55
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O2 -Wall")
66
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -g -O2 -Wall")
77

8+
# Set the CMAKE_MODULE_PATH so it can find the Findsodium.cmake file
9+
set(CMAKE_MODULE_PATH
10+
"${CMAKE_CURRENT_SOURCE_DIR}/cmake"
11+
${CMAKE_MODULE_PATH})
12+
813
# Projects which statically link Sodium in Visual Studio must define a macro named SODIUM_STATIC
914
add_definitions(-DSODIUM_STATIC)
1015

16+
# Find where libsodium is installed (prevent linker errors)
17+
find_package(sodium REQUIRED)
18+
1119
# Tell CMake where to look for header files
1220
include_directories(${CMAKE_SOURCE_DIR}/include)
1321

0 commit comments

Comments
 (0)