From d51d25a84594f579a7f4c03a5b342573bddd3ff1 Mon Sep 17 00:00:00 2001 From: nixo Date: Wed, 11 Mar 2020 10:46:24 +0100 Subject: [PATCH] Init. Implemented only password hashing functions --- Project.toml | 4 +++ src/Sodium.jl | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+) create mode 100644 Project.toml create mode 100644 src/Sodium.jl diff --git a/Project.toml b/Project.toml new file mode 100644 index 0000000..2be8a31 --- /dev/null +++ b/Project.toml @@ -0,0 +1,4 @@ +name = "Sodium" +uuid = "04b35f30-b1d3-48e3-a9a8-b40e01b02b62" +authors = ["nixo "] +version = "0.1.0" diff --git a/src/Sodium.jl b/src/Sodium.jl new file mode 100644 index 0000000..706dfaa --- /dev/null +++ b/src/Sodium.jl @@ -0,0 +1,75 @@ +module Sodium + +# BinaryBuilder builder has dozen of dependencies, it's extremely complex and +# does nothing more than guix. +const lib = "libsodium.so" + +# crypto_pwhash_str is 2x fast if sodium has been initalized +# So remember to init it before using crypto functions +"""Starts libsodium. Makes crypto_pwhash functions fasters, +so you need to call it. + +Returns true if success, false else""" +sodium_init() = if ccall((:sodium_init,lib),Int,()) != -1 + @info "Sodium initialized" +else + @error "Could not init libsodium" +end + +const crypto_pwhash_STRBYTES = Ref(Csize_t(0)) +const crypto_pwhash_OPSLIMIT_INTERACTIVE = Ref(Csize_t(0)) +const crypto_pwhash_OPSLIMIT_MODERATE = Ref(Csize_t(0)) +const crypto_pwhash_OPSLIMIT_SENSITIVE = Ref(Csize_t(0)) +const crypto_pwhash_MEMLIMIT_INTERACTIVE = Ref(Csize_t(0)) +const crypto_pwhash_MEMLIMIT_MODERATE = Ref(Csize_t(0)) +const crypto_pwhash_MEMLIMIT_SENSITIVE = Ref(Csize_t(0)) + +function __init__() + sodium_init() + crypto_pwhash_STRBYTES[] = ccall((:crypto_pwhash_strbytes,lib),Csize_t,()) + + crypto_pwhash_OPSLIMIT_INTERACTIVE[] = + ccall((:crypto_pwhash_opslimit_interactive,lib),Csize_t,()) + crypto_pwhash_MEMLIMIT_INTERACTIVE[] = + ccall((:crypto_pwhash_memlimit_interactive,lib),Csize_t,()) + + crypto_pwhash_OPSLIMIT_MODERATE[] = + ccall((:crypto_pwhash_opslimit_moderate,lib),Csize_t,()) + crypto_pwhash_MEMLIMIT_MODERATE[] = + ccall((:crypto_pwhash_memlimit_moderate,lib),Csize_t,()) + + crypto_pwhash_OPSLIMIT_SENSITIVE[] = + ccall((:crypto_pwhash_opslimit_sensitive,lib),Csize_t,()) + crypto_pwhash_MEMLIMIT_SENSITIVE[] = + ccall((:crypto_pwhash_memlimit_sensitive,lib),Csize_t,()) +end + +"See the libsodium documentation for info" +function crypto_pwhash_str(password::AbstractString, + opslimit = crypto_pwhash_OPSLIMIT_SENSITIVE[], + memlimit = crypto_pwhash_MEMLIMIT_SENSITIVE[]) + hashed = zeros(UInt8, crypto_pwhash_STRBYTES[]) + if ccall((:crypto_pwhash_str, lib), Int, + (Ref{UInt8}, Cstring, Int, Csize_t, Csize_t), + hashed, password, length(password), opslimit, memlimit) == 0 + let s = String(hashed) + return s[1:(findfirst('\0', s)-1)] + end + else + @error "OOM error" + end +end + +"See the libsodium documentation for info" +function crypto_pwhash_str_verify(password::String, hashed::String) + ccall((:crypto_pwhash_str_verify, lib), Int, + (Cstring, Cstring, Int), + hashed, password, length(password)) == 0 +end + +"returns an unpredictable value between 0 and 0xffffffff (included)" +randombyte() = ccall((:randombytes_random, lib), UInt32, ()) + +export sodium_init, crypto_pwhash_str, crypto_pwhash_str_verify, randombyte + +end # module