Skip to content

Commit

Permalink
make libsodium optional
Browse files Browse the repository at this point in the history
  • Loading branch information
jshawl committed Feb 19, 2024
1 parent feb2e51 commit 9373e78
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 4 deletions.
7 changes: 7 additions & 0 deletions bin/minisign
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ rescue OptionParser::InvalidOption
exit 1
end

if options[:G] || options[:R] || options[:G] || options[:S]
unless defined? RbNaCl::Hash
STDERR.puts "Error: libsodium is not installed!"
exit 1
end
end

Minisign::CLI.generate(options) if options[:G]
Minisign::CLI.recreate(options) if options[:R]
Minisign::CLI.change_password(options) if options[:C]
Expand Down
8 changes: 7 additions & 1 deletion lib/minisign.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@

require 'ed25519'
require 'base64'
require 'rbnacl'
require 'openssl'
begin
require 'rbnacl'
rescue LoadError => e
# errors handled when invoked (see Minisign::NaCl)
end

require 'minisign/cli'
require 'minisign/utils'
require 'minisign/public_key'
require 'minisign/signature'
require 'minisign/private_key'
require 'minisign/key_pair'
require 'minisign/nacl'
require 'minisign/error'
3 changes: 3 additions & 0 deletions lib/minisign/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@ class PasswordMissingError < StandardError

class PasswordIncorrectError < StandardError
end

class LibSodiumDependencyError < StandardError
end
end
27 changes: 27 additions & 0 deletions lib/minisign/nacl.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module Minisign
module NaCl
def self.safely
begin
yield
rescue NameError
raise Minisign::LibSodiumDependencyError, 'libsodium is not installed!'
end
end
module Hash
module Blake2b
def self.digest(*args)
NaCl::safely do
RbNaCl::Hash::Blake2b.digest(*args)
end
end
end
end
module PasswordHash
def self.scrypt(*args)
NaCl::safely do
RbNaCl::PasswordHash.scrypt(*args)
end
end
end
end
end
6 changes: 3 additions & 3 deletions lib/minisign/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ module Minisign
# Helpers used in multiple classes
module Utils
def blake2b256(message)
RbNaCl::Hash::Blake2b.digest(message, { digest_size: 32 })
Minisign::NaCl::Hash::Blake2b.digest(message, { digest_size: 32 })
end

def blake2b512(message)
RbNaCl::Hash::Blake2b.digest(message, { digest_size: 64 })
OpenSSL::Digest.new('blake2b512').digest(message)
end

# @return [Array<32 bit unsigned ints>]
Expand All @@ -25,7 +25,7 @@ def hex(bytes)

# @return [String] the <kdf_output> used to xor the ed25519 keys
def derive_key(password, kdf_salt, kdf_opslimit, kdf_memlimit)
RbNaCl::PasswordHash.scrypt(
Minisign::NaCl::PasswordHash.scrypt(
password,
kdf_salt,
kdf_opslimit,
Expand Down

0 comments on commit 9373e78

Please sign in to comment.