Skip to content

Commit

Permalink
Allow strings of arbitrary bytes to be hashed
Browse files Browse the repository at this point in the history
Some versions of FFI barf on null bytes with a :string type
  • Loading branch information
namelessjon committed Mar 13, 2013
1 parent 0340428 commit 307ae37
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/rbnacl/nacl.rb
Expand Up @@ -33,12 +33,12 @@ def self.#{name}(*args)
SHA256BYTES = 32
wrap_nacl_function :crypto_hash_sha256,
:crypto_hash_sha256_ref,
[:pointer, :string, :long_long]
[:pointer, :pointer, :long_long]

SHA512BYTES = 64
wrap_nacl_function :crypto_hash_sha512,
:crypto_hash_sha512_ref,
[:pointer, :string, :long_long]
[:pointer, :pointer, :long_long]

PUBLICKEYBYTES = 32
SECRETKEYBYTES = 32
Expand Down
8 changes: 8 additions & 0 deletions spec/rbnacl/hash_spec.rb
Expand Up @@ -24,6 +24,10 @@
it "calculates the correct hash for an empty string and returns it in hex" do
Crypto::Hash.sha256("", :hex).should eq empty_string_hash_hex
end

it "doesn't raise on a null byte" do
expect { Crypto::Hash.sha256("\0") }.to_not raise_error(/ArgumentError: string contains null byte/)
end
end

context "sha512" do
Expand All @@ -48,5 +52,9 @@
it "calculates the correct hash for an empty string and returns it in hex" do
Crypto::Hash.sha512("", :hex).should eq empty_string_hash_hex
end

it "doesn't raise on a null byte" do
expect { Crypto::Hash.sha512("\0") }.to_not raise_error(/ArgumentError: string contains null byte/)
end
end
end

0 comments on commit 307ae37

Please sign in to comment.