Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Allow runtime interrogation of the primitive

Should be a little nicer to work with in a multi-primitive setup
  • Loading branch information...
commit f619adec8433e3bd37e3f09b6b75b197b77e98d4 1 parent 8e38448
@namelessjon namelessjon authored
View
21 lib/rbnacl/secret_box.rb
@@ -18,8 +18,6 @@ module Crypto
# arbitrary valid messages, so messages you send are repudiatable. For
# non-repudiatable messages, sign them before or after encryption.
class SecretBox
- attr_reader :primitive
-
# The default primitive to use
DEFAULT_PRIMITIVE = SecretBox::XSalsa20Poly1305
@@ -38,6 +36,21 @@ def initialize(key, encoding = :raw, primitive = DEFAULT_PRIMITIVE)
@primitive = primitive.new(@key)
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
+
+
# Encrypts a message
#
# Encrypts the message with the given nonce to the key set up when
@@ -53,7 +66,7 @@ def initialize(key, encoding = :raw, primitive = DEFAULT_PRIMITIVE)
#
# @return [String] The ciphertext without the nonce prepended (BINARY encoded)
def box(nonce, message)
- self.primitive.box(nonce, message)
+ @primitive.box(nonce, message)
end
alias encrypt box
@@ -72,7 +85,7 @@ def box(nonce, message)
#
# @return [String] The decrypted message (BINARY encoded)
def open(nonce, ciphertext)
- self.primitive.open(nonce, ciphertext)
+ @primitive.open(nonce, ciphertext)
end
alias decrypt open
end
View
19 lib/rbnacl/secret_box/xsalsa20_poly1305.rb
@@ -33,9 +33,6 @@ class XSalsa20Poly1305
# Number of bytes for a nonce
NONCEBYTES = NaCl::XSALSA20_POLY1305_SECRETBOX_NONCEBYTES
- # The cryptographic primitive used
- PRIMITIVE = :xsalsa20_poly1305
-
# Create a new SecretBox
#
# Sets up the Box with a secret key fro encrypting and decrypting messages.
@@ -50,6 +47,20 @@ def initialize(key)
Util.check_length(@key, KEYBYTES, "Secret key")
end
+ # Returns the primitive name
+ #
+ # @return [Symbol] the primitive name
+ def self.primitive
+ :xsalsa20_poly1305
+ end
+
+ # Returns the primitive name
+ #
+ # @return [Symbol] the primitive name
+ def primitive
+ self.class.primitive
+ end
+
# Encrypts a message
#
# Encrypts the message with the given nonce to the key set up when
@@ -71,7 +82,7 @@ def box(nonce, message)
NaCl.crypto_secretbox_xsalsa20poly1305(ct, msg, msg.bytesize, nonce, @key) || raise(CryptoError, "Encryption failed")
ct = Util.remove_zeros(NaCl::BOXZEROBYTES, ct)
- Ciphertext.new(ct, PRIMITIVE)
+ Ciphertext.new(ct, primitive)
end
alias encrypt box
Please sign in to comment.
Something went wrong with that request. Please try again.