Permalink
Browse files

Move assistance methods onto the class

  • Loading branch information...
1 parent b131cdf commit cb49f6e9ec06d13249268557e30cdff618176400 @stouset committed Mar 19, 2013
Showing with 63 additions and 62 deletions.
  1. +7 −7 lib/sodium/auth.rb
  2. +42 −41 lib/sodium/box.rb
  3. +1 −1 test/sodium/auth_test.rb
  4. +2 −2 test/sodium/box/curve25519xsalsa20poly1305_test.rb
  5. +11 −11 test/sodium/box_test.rb
View
@@ -8,11 +8,11 @@ def self.key
end
def initialize(key)
- @key = _key(key)
+ @key = self.class._key(key)
end
def auth(message)
- message = _message(message)
+ message = self.class._message(message)
authenticator = Sodium::Util.buffer self.implementation[:BYTES]
self.implementation.nacl(authenticator, message, message.length, @key) or
@@ -22,23 +22,23 @@ def auth(message)
end
def verify(message, authenticator)
- message = _message(message)
- authenticator = _authenticator(authenticator)
+ message = self.class._message(message)
+ authenticator = self.class._authenticator(authenticator)
self.implementation.nacl_verify(authenticator, message, message.length, @key)
end
private
- def _key(k)
+ def self._key(k)
Sodium::Util.assert_length k.to_str, self.implementation[:KEYBYTES], 'key'
end
- def _authenticator(a)
+ def self._authenticator(a)
Sodium::Util.assert_length a.to_str, self.implementation[:BYTES], 'authenticator'
end
- def _message(m)
+ def self._message(m)
m.to_str
end
end
View
@@ -13,112 +13,113 @@ def self.keypair
return secret_key, public_key
end
- def initialize(secret_key, public_key)
- @secret_key = _secret_key(secret_key)
- @public_key = _public_key(public_key)
- end
-
- def nonce
- Sodium::Util.nonce self.implementation[:NONCEBYTES]
- end
-
- def box(message, nonce)
+ def self.afternm(shared_key, message, nonce)
+ shared_key = _shared_key(shared_key)
message = _message(message)
nonce = _nonce(nonce)
ciphertext = Sodium::Util.buffer(message.length)
- self.implementation.nacl(
+ self.implementation.nacl_afternm(
ciphertext,
message, message.length,
nonce,
- @public_key,
- @secret_key
+ shared_key
) or raise Sodium::CryptoError, 'failed to close the box'
Sodium::Util.unpad ciphertext, self.implementation[:BOXZEROBYTES]
end
- def open(ciphertext, nonce)
+ def self.open_afternm(shared_key, ciphertext, nonce)
+ shared_key = _shared_key(shared_key)
ciphertext = _ciphertext(ciphertext)
nonce = _nonce(nonce)
message = Sodium::Util.buffer(ciphertext.length)
- self.implementation.nacl_open(
+ self.implementation.nacl_open_afternm(
message,
ciphertext, ciphertext.length,
nonce,
- @public_key,
- @secret_key
+ shared_key
) or raise Sodium::CryptoError, 'failed to open the box'
Sodium::Util.unpad message, self.implementation[:ZEROBYTES]
end
- def beforenm
- shared_key = Sodium::Util.buffer self.implementation[:BEFORENMBYTES]
- self.implementation.nacl_beforenm(
- shared_key, @public_key, @secret_key
- ) or raise Sodium::CryptoError, 'failed to create a shared key'
+ def initialize(secret_key, public_key)
+ @secret_key = self.class._secret_key(secret_key)
+ @public_key = self.class._public_key(public_key)
+ end
- shared_key
+ def nonce
+ Sodium::Util.nonce self.implementation[:NONCEBYTES]
end
- def afternm(shared_key, message, nonce)
- shared_key = _shared_key(shared_key)
- message = _message(message)
- nonce = _nonce(nonce)
+ def box(message, nonce)
+ message = self.class._message(message)
+ nonce = self.class._nonce(nonce)
ciphertext = Sodium::Util.buffer(message.length)
- self.implementation.nacl_afternm(
+ self.implementation.nacl(
ciphertext,
message, message.length,
nonce,
- shared_key
+ @public_key,
+ @secret_key
) or raise Sodium::CryptoError, 'failed to close the box'
Sodium::Util.unpad ciphertext, self.implementation[:BOXZEROBYTES]
end
- def open_afternm(shared_key, ciphertext, nonce)
- shared_key = _shared_key(shared_key)
- ciphertext = _ciphertext(ciphertext)
- nonce = _nonce(nonce)
+ def open(ciphertext, nonce)
+ ciphertext = self.class._ciphertext(ciphertext)
+ nonce = self.class._nonce(nonce)
message = Sodium::Util.buffer(ciphertext.length)
- self.implementation.nacl_open_afternm(
+ self.implementation.nacl_open(
message,
ciphertext, ciphertext.length,
nonce,
- shared_key
+ @public_key,
+ @secret_key
) or raise Sodium::CryptoError, 'failed to open the box'
Sodium::Util.unpad message, self.implementation[:ZEROBYTES]
end
+ def beforenm
+ shared_key = Sodium::Util.buffer self.implementation[:BEFORENMBYTES]
+
+ self.implementation.nacl_beforenm(
+ shared_key, @public_key, @secret_key
+ ) or raise Sodium::CryptoError, 'failed to create a shared key'
+
+ shared_key
+ end
+
private
- def _public_key(k)
+ def self._public_key(k)
Sodium::Util.assert_length k.to_str, self.implementation[:PUBLICKEYBYTES], 'public key'
end
- def _secret_key(k)
+ def self._secret_key(k)
Sodium::Util.assert_length k.to_str, self.implementation[:SECRETKEYBYTES], 'secret key'
end
- def _shared_key(k)
+ def self._shared_key(k)
Sodium::Util.assert_length k.to_str, self.implementation[:BEFORENMBYTES], 'secret key'
end
- def _message(m)
+ def self._message(m)
Sodium::Util.pad m.to_str, self.implementation[:ZEROBYTES]
end
- def _ciphertext(c)
+ def self._ciphertext(c)
Sodium::Util.pad c.to_str, self.implementation[:BOXZEROBYTES]
end
- def _nonce(n)
+ def self._nonce(n)
Sodium::Util.assert_length n.to_str, self.implementation[:NONCEBYTES], 'nonce'
end
end
View
@@ -40,7 +40,7 @@
it 'must raise when failing to generate an authenticator' do
sodium_stub_failure(self.klass, :nacl) do
- lambda { subject.auth('message') }.
+ lambda { self.subject.auth('message') }.
must_raise Sodium::CryptoError
end
end
@@ -60,15 +60,15 @@
end
it 'must generate closed boxes with shared keys' do
- self.subject.afternm(
+ self.klass.afternm(
self.shared_key,
self.plaintext,
self.nonce
).must_equal self.ciphertext
end
it 'must open boxes with shared keys' do
- self.subject.open_afternm(
+ self.klass.open_afternm(
self.shared_key,
self.ciphertext,
self.nonce
View
@@ -45,12 +45,12 @@
end
it 'must raise when receiving an invalid nonce' do
- lambda { subject.box('message', subject.nonce[0..-2]) }.
+ lambda { self.subject.box('message', self.subject.nonce[0..-2]) }.
must_raise Sodium::LengthError
end
it 'must raise when receiving an invalid shared key' do
- lambda { subject.afternm('key', 'message', subject.nonce) }.
+ lambda { self.klass.afternm('key', 'message', self.subject.nonce) }.
must_raise Sodium::LengthError
end
@@ -63,45 +63,45 @@
it 'must raise when failing to close a box' do
sodium_stub_failure(self.klass, :nacl) do
- lambda { subject.box('message', subject.nonce) }.
+ lambda { self.subject.box('message', self.subject.nonce) }.
must_raise Sodium::CryptoError
end
end
it 'must raise when failing to open a box' do
sodium_stub_failure(self.klass, :nacl_open) do
- lambda { subject.open('ciphertext', subject.nonce) }.
+ lambda { self.subject.open('ciphertext', self.subject.nonce) }.
must_raise Sodium::CryptoError
end
end
it 'must raise when failing to generate a shared key' do
sodium_stub_failure(self.klass, :nacl_beforenm) do
- lambda { subject.beforenm }.
+ lambda { self.subject.beforenm }.
must_raise Sodium::CryptoError
end
end
it 'must raise when failing to close a box with a shared key' do
sodium_stub_failure(self.klass, :nacl_afternm) do
lambda do
- key = subject.beforenm
- nonce = subject.nonce
+ key = self.subject.beforenm
+ nonce = self.subject.nonce
message = 'message'
- subject.afternm(key, message, nonce)
+ self.klass.afternm(key, message, nonce)
end.must_raise Sodium::CryptoError
end
end
it 'must raise when failing to open a box with a shared key' do
sodium_stub_failure(self.klass, :nacl_open_afternm) do
lambda do
- key = subject.beforenm
- nonce = subject.nonce
+ key = self.subject.beforenm
+ nonce = self.subject.nonce
ciphertext = 'ciphertext'
- subject.open_afternm(key, ciphertext, nonce)
+ self.klass.open_afternm(key, ciphertext, nonce)
end.must_raise Sodium::CryptoError
end
end

0 comments on commit cb49f6e

Please sign in to comment.