From 307ae379f63aad4ee669d95031343d0806f41135 Mon Sep 17 00:00:00 2001 From: Jonathan Stott Date: Wed, 13 Mar 2013 21:51:57 +0000 Subject: [PATCH] Allow strings of arbitrary bytes to be hashed Some versions of FFI barf on null bytes with a :string type --- lib/rbnacl/nacl.rb | 4 ++-- spec/rbnacl/hash_spec.rb | 8 ++++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/rbnacl/nacl.rb b/lib/rbnacl/nacl.rb index 7d38c2e..2bdad1d 100644 --- a/lib/rbnacl/nacl.rb +++ b/lib/rbnacl/nacl.rb @@ -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 diff --git a/spec/rbnacl/hash_spec.rb b/spec/rbnacl/hash_spec.rb index 944e6ee..5a3c883 100644 --- a/spec/rbnacl/hash_spec.rb +++ b/spec/rbnacl/hash_spec.rb @@ -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 @@ -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