Skip to content

Commit

Permalink
s/block/early return
Browse files Browse the repository at this point in the history
  • Loading branch information
jshawl committed Feb 19, 2024
1 parent d83d2eb commit 563eb7d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
16 changes: 7 additions & 9 deletions lib/minisign/nacl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,27 @@
module Minisign
# A module that invokes RbNaCl with user-focused actionable error messages.
module NaCl
def self.safely
yield
rescue NameError
def self.assert_libsodium_dependency_met!
return if RbNaCl.const_defined?(:PasswordHash)

raise Minisign::LibSodiumDependencyError, 'libsodium is not installed!'
end

module Hash
# see RbNaCl::Hash::Blake2b
module Blake2b
def self.digest(*args)
NaCl.safely do
RbNaCl::Hash::Blake2b.digest(*args)
end
NaCl.assert_libsodium_dependency_met!
RbNaCl::Hash::Blake2b.digest(*args)
end
end
end

# see RbNaCl::PasswordHash
module PasswordHash
def self.scrypt(*args)
NaCl.safely do
RbNaCl::PasswordHash.scrypt(*args)
end
NaCl.assert_libsodium_dependency_met!
RbNaCl::PasswordHash.scrypt(*args)
end
end
end
Expand Down
2 changes: 2 additions & 0 deletions spec/minisign/nacl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
describe Minisign::NaCl do
it 'raises LibSodiumDependencyError if libsodium not installed' do
hash = RbNaCl.send(:remove_const, :Hash)
password_hash = RbNaCl.send(:remove_const, :PasswordHash)
expect do
Minisign::NaCl::Hash::Blake2b.digest('message', { digest_size: 32 })
end.to raise_error(Minisign::LibSodiumDependencyError)
RbNaCl.const_set(:Hash, hash)
RbNaCl.const_set(:PasswordHash, password_hash)
end
end

0 comments on commit 563eb7d

Please sign in to comment.