Skip to content

Commit

Permalink
Merge 8f6225d into 83a122e
Browse files Browse the repository at this point in the history
  • Loading branch information
tarcieri committed Mar 15, 2013
2 parents 83a122e + 8f6225d commit 882b347
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 65 deletions.
68 changes: 5 additions & 63 deletions lib/rbnacl/secret_box.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# encoding: binary
require 'rbnacl/secret_box/xsalsa20_poly1305'
module Crypto
# The SecretBox class boxes and unboxes messages
# The SecretBox class boxes and unboxes messages. The underlying primitive
# used is Crypto::SecretBox::XSalsa20Poly1305. More specific documentation
# can be found there.
#
# This class uses the given secret key to encrypt and decrypt messages.
#
Expand Down Expand Up @@ -31,68 +33,8 @@ class SecretBox
# @raise [Crypto::LengthError] on invalid keys
#
# @return [Crypto::SecretBox] The new Box, ready to use
def initialize(key, encoding = :raw, primitive = DEFAULT_PRIMITIVE)
@key = Encoder[encoding].decode(key) if key
@primitive = primitive.new(@key)
def self.new(key, encoding = :raw)
DEFAULT_PRIMITIVE.new(key, encoding)
end

# returns the defaul primitive for the SecretBox class
#
# @return [Symbol] the default primitive
def self.primitive
DEFAULT_PRIMITIVE.primitive
end

# returns the primitive of this instance
#
# @return [Symbol] the default primitive
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
#
# Encrypts the message with the given nonce to the key set up when
# initializing the class. Make sure the nonce is unique for any given
# key, or you might as well just send plain text.
#
# This function takes care of the padding required by the NaCL C API.
#
# @param nonce [String] A 24-byte string containing the nonce.
# @param message [String] The message to be encrypted.
#
# @raise [Crypto::LengthError] If the nonce is not valid
#
# @return [String] The ciphertext without the nonce prepended (BINARY encoded)
def box(nonce, message)
@primitive.box(nonce, message)
end
alias encrypt box

# Decrypts a ciphertext
#
# Decrypts the ciphertext with the given nonce using the key setup when
# initializing the class.
#
# This function takes care of the padding required by the NaCL C API.
#
# @param nonce [String] A 24-byte string containing the nonce.
# @param ciphertext [String] The message to be decrypted.
#
# @raise [Crypto::LengthError] If the nonce is not valid
# @raise [Crypto::CryptoError] If the ciphertext cannot be authenticated.
#
# @return [String] The decrypted message (BINARY encoded)
def open(nonce, ciphertext)
@primitive.open(nonce, ciphertext)
end
alias decrypt open
end
end
4 changes: 2 additions & 2 deletions lib/rbnacl/secret_box/xsalsa20_poly1305.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ class XSalsa20Poly1305
# @raise [Crypto::LengthError] on invalid keys
#
# @return [Crypto::SecretBox] The new Box, ready to use
def initialize(key)
@key = key
def initialize(key, encoding = :raw)
@key = Encoder[encoding].decode(key) if key
Util.check_length(@key, KEYBYTES, "Secret key")
end

Expand Down

0 comments on commit 882b347

Please sign in to comment.