Permalink
Browse files

Provide method for querying # of nonce bytes

This allows the RandomNonceBox to funciton in an indeterminate primivite
world.
  • Loading branch information...
1 parent f619ade commit 34d1de4c6578d6d42ccb0645ea68733af2c850ee @namelessjon namelessjon committed Mar 13, 2013
Showing with 17 additions and 5 deletions.
  1. +2 −4 lib/rbnacl/random_nonce_box.rb
  2. +6 −0 lib/rbnacl/secret_box.rb
  3. +9 −1 lib/rbnacl/secret_box/xsalsa20_poly1305.rb
View
6 lib/rbnacl/random_nonce_box.rb
@@ -27,8 +27,6 @@ module Crypto
# there is no protection against messages being reordered and replayed by an
# active adversary.
class RandomNonceBox
- # the size of the nonce
- NONCEBYTES = NaCl::NONCEBYTES
# Create a new RandomNonceBox
#
@@ -98,11 +96,11 @@ def open(enciphered_message, encoding = :raw)
private
def generate_nonce
- Random.random_bytes(NONCEBYTES)
+ Random.random_bytes(box.nonce_bytes)
end
def extract_nonce(bytes)
- nonce = bytes.slice!(0, NONCEBYTES)
+ nonce = bytes.slice!(0, box.nonce_bytes)
[nonce, bytes]
end
end
View
6 lib/rbnacl/secret_box.rb
@@ -50,6 +50,12 @@ def primitive
@primitive.primitive
end
+ # returns the number of bytes in a nonce
+ #
+ # @return [Integer] Number of nonce bytes
+ def nonce_bytes
+ @primitive.nonce_bytes
+ end
# Encrypts a message
#
View
10 lib/rbnacl/secret_box/xsalsa20_poly1305.rb
@@ -61,6 +61,14 @@ def primitive
self.class.primitive
end
+ # returns the number of bytes in a nonce
+ #
+ # @return [Integer] Number of nonce bytes
+ def nonce_bytes
+ NONCEBYTES
+ end
+
+
# Encrypts a message
#
# Encrypts the message with the given nonce to the key set up when
@@ -74,7 +82,7 @@ def primitive
#
# @raise [Crypto::LengthError] If the nonce is not valid
#
- # @return [String] The ciphertext without the nonce prepended (BINARY encoded)
+ # @return [Crypto::Ciphertext] The ciphertext without the nonce prepended (BINARY encoded)
def box(nonce, message)
Util.check_length(nonce, NONCEBYTES, "Nonce")
msg = Util.prepend_zeros(NaCl::ZEROBYTES, message)

0 comments on commit 34d1de4

Please sign in to comment.