Skip to content

Xamarin compatible wrapper for libsodium

License

Notifications You must be signed in to change notification settings

liip/Sodium.Xamarin

Repository files navigation

Sodium.Xamarin Build status NuGet Version License

Sodium.Xamarin is a wrapper library for libsodium specifically targeting Xamarin (Native or Forms).

Why

None of the available libraries out there are compatible with Xamarin. The goal of this library is to take an existing .NET library and apply the minimal changes to make it compatible.

Installation

NuGet package

Install the Sodium.Xamarin NuGet package in your Xamarin core project.

Compile the native libraries

Installing the NuGet is not enough. You will have to compile the libsodium library for iOS and Android yourself, because they are not provided out of the box.

The following instructions have been tested on Mac OS 10.14, using libsodium 1.0.18 and the installation documentation.

Download the latest stable version (for example libsodium-1.0.18-stable.tar.gz) and untar it.

From the root folder, run the commands:

./configure
make && make check
sudo make install

Make sure everything went fine.

Compile and install on iOS

Run the command:

LIBSODIUM_FULL_BUILD=true dist-build/ios.sh

This will create a new folder libsodium-ios that contains the libsodium.a file.

In Visual Studio - in the Xamarin.iOS project - add this new file in your project. Make sure that the Build Action of the file is None. Right-click in the iOS project and do "Add > Add Native Reference". Select the file you have just added.

Compile and install on Android

First, make sure that you have installed the Android NDK, and that you have the ANDROID_NDK_HOME environment variable. For example using:

export ANDROID_NDK_HOME=/Users/john/Library/Android/sdk/ndk/20.0.5594570

Then run the commands:

dist-build/android-x86.sh
dist-build/android-x86_64.sh
dist-build/android-armv7-a.sh
dist-build/android-armv8-a.sh

This will create 4 new folders that each contain the libsodium.so file. Beware of using the .so file and not the .a file like you did for iOS.

In you Xamarin.Android project create a new folder Resources/lib/. Inside, create 4 new folders called x86, x86_64, armeabi-v7a and arm64-v8a. Inside those folders add the libsodium.so files following the mapping:

libsodium folder Xamarin.Android folder
libsodium-android-i686 x86
libsodium-android-westmere x86_64
libsodium-android-armv7-a armeabi-v7a
libsodium-android-armv8-a arm64-v8a

Change the Build Action of the files to AndroidNativeLibrary.

Usage

You can now use libsodium inside your Xamarin code, by including the package Sodium. For example:

var version = Sodium.SodiumCore.SodiumVersionString();
Console.WriteLine($"Libsodium version: {version}");

Use the documentation from libsodium and libsodium-net.

Acknowledgements

This library is based on libsodium-core, which itself is based on libsodium-net, which uses libsodium. The present work would not have been possible without the hard work of all maintainers.

License

The library and it's parents are subject to the MIT license (see LICENSE). Libsodium is subject to the ISC license.