Skip to content
This repository has been archived by the owner on Mar 16, 2022. It is now read-only.

Commit

Permalink
Use new Sodium::Buffer block syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
stouset committed Jun 3, 2013
1 parent 382bcba commit 0c189a3
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 107 deletions.
21 changes: 10 additions & 11 deletions lib/sodium/auth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,16 @@ def initialize(key)
end

def auth(message)
message = self.class._message(message)
authenticator = Sodium::Buffer.empty self.implementation[:BYTES]

self.implementation.nacl(
authenticator.to_str,
message.to_str,
message.to_str.bytesize,
@key.to_str
) or raise Sodium::CryptoError, 'failed to generate an authenticator'

authenticator
message = self.class._message(message)

Sodium::Buffer.empty self.implementation[:BYTES] do |authenticator|
self.implementation.nacl(
authenticator.to_str,
message.to_str,
message.to_str.bytesize,
@key.to_str
) or raise Sodium::CryptoError, 'failed to generate an authenticator'
end
end

def verify(message, authenticator)
Expand Down
104 changes: 49 additions & 55 deletions lib/sodium/box.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,34 +24,32 @@ def self.afternm(shared_key, message, nonce)
shared_key = _shared_key(shared_key)
message = _message(message)
nonce = _nonce(nonce)
ciphertext = Sodium::Buffer.empty(message.bytesize)

self.implementation.nacl_afternm(
ciphertext.to_str,
message.to_str,
message.to_str.bytesize,
nonce.to_str,
shared_key.to_str
) or raise Sodium::CryptoError, 'failed to close the box'

ciphertext.unpad self.implementation[:BOXZEROBYTES]
Sodium::Buffer.empty(message.bytesize) do |ciphertext|
self.implementation.nacl_afternm(
ciphertext.to_str,
message.to_str,
message.to_str.bytesize,
nonce.to_str,
shared_key.to_str
) or raise Sodium::CryptoError, 'failed to close the box'
end.unpad self.implementation[:BOXZEROBYTES]
end

def self.open_afternm(shared_key, ciphertext, nonce)
shared_key = _shared_key(shared_key)
ciphertext = _ciphertext(ciphertext)
nonce = _nonce(nonce)
message = Sodium::Buffer.empty(ciphertext.bytesize)

self.implementation.nacl_open_afternm(
message.to_str,
ciphertext.to_str,
ciphertext.to_str.bytesize,
nonce.to_str,
shared_key.to_str
) or raise Sodium::CryptoError, 'failed to open the box'

message.unpad self.implementation[:ZEROBYTES]
Sodium::Buffer.empty(ciphertext.bytesize) do |message|
self.implementation.nacl_open_afternm(
message.to_str,
ciphertext.to_str,
ciphertext.to_str.bytesize,
nonce.to_str,
shared_key.to_str
) or raise Sodium::CryptoError, 'failed to open the box'
end.unpad self.implementation[:ZEROBYTES]
end


Expand All @@ -65,49 +63,45 @@ def nonce
end

def box(message, nonce)
message = self.class._message(message)
nonce = self.class._nonce(nonce)
ciphertext = Sodium::Buffer.empty(message.bytesize)

self.implementation.nacl(
ciphertext.to_str,
message.to_str,
message.to_str.bytesize,
nonce.to_str,
@public_key.to_str,
@secret_key.to_str
) or raise Sodium::CryptoError, 'failed to close the box'

ciphertext.unpad self.implementation[:BOXZEROBYTES]
message = self.class._message(message)
nonce = self.class._nonce(nonce)

Sodium::Buffer.empty(message.bytesize) do |ciphertext|
self.implementation.nacl(
ciphertext.to_str,
message.to_str,
message.to_str.bytesize,
nonce.to_str,
@public_key.to_str,
@secret_key.to_str
) or raise Sodium::CryptoError, 'failed to close the box'
end.unpad self.implementation[:BOXZEROBYTES]
end

def open(ciphertext, nonce)
ciphertext = self.class._ciphertext(ciphertext)
nonce = self.class._nonce(nonce)
message = Sodium::Buffer.empty(ciphertext.bytesize)

self.implementation.nacl_open(
message.to_str,
ciphertext.to_str,
ciphertext.to_str.bytesize,
nonce.to_str,
@public_key.to_str,
@secret_key.to_str
) or raise Sodium::CryptoError, 'failed to open the box'

message.unpad self.implementation[:ZEROBYTES]

Sodium::Buffer.empty(ciphertext.bytesize) do |message|
self.implementation.nacl_open(
message.to_str,
ciphertext.to_str,
ciphertext.to_str.bytesize,
nonce.to_str,
@public_key.to_str,
@secret_key.to_str
) or raise Sodium::CryptoError, 'failed to open the box'
end.unpad self.implementation[:ZEROBYTES]
end

def beforenm
shared_key = Sodium::Buffer.empty self.implementation[:BEFORENMBYTES]

self.implementation.nacl_beforenm(
shared_key.to_str,
@public_key.to_str,
@secret_key.to_str
) or raise Sodium::CryptoError, 'failed to create a shared key'

shared_key
Sodium::Buffer.empty self.implementation[:BEFORENMBYTES] do |shared_key|
self.implementation.nacl_beforenm(
shared_key.to_str,
@public_key.to_str,
@secret_key.to_str
) or raise Sodium::CryptoError, 'failed to create a shared key'
end
end

private
Expand Down
15 changes: 7 additions & 8 deletions lib/sodium/hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ class Sodium::Hash

def self.hash(message)
message = _message(message)
digest = Sodium::Buffer.empty self.implementation[:BYTES]

self.implementation.nacl(
digest.to_str,
message.to_str,
message.to_str.bytesize
) or raise Sodium::CryptoError, 'failed to generate a hash for the message'

digest
Sodium::Buffer.empty self.implementation[:BYTES] do |digest|
self.implementation.nacl(
digest.to_str,
message.to_str,
message.to_str.bytesize
) or raise Sodium::CryptoError, 'failed to generate a hash for the message'
end
end

private
Expand Down
21 changes: 10 additions & 11 deletions lib/sodium/one_time_auth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,16 @@ def initialize(key)
end

def one_time_auth(message)
message = self.class._message(message)
authenticator = Sodium::Buffer.empty self.implementation[:BYTES]

self.implementation.nacl(
authenticator.to_str,
message.to_str,
message.to_str.bytesize,
@key.to_str
) or raise Sodium::CryptoError, 'failed to generate an authenticator'

authenticator
message = self.class._message(message)

Sodium::Buffer.empty self.implementation[:BYTES] do |authenticator|
self.implementation.nacl(
authenticator.to_str,
message.to_str,
message.to_str.bytesize,
@key.to_str
) or raise Sodium::CryptoError, 'failed to generate an authenticator'
end
end

def verify(message, authenticator)
Expand Down
42 changes: 20 additions & 22 deletions lib/sodium/secret_box.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,33 @@ def nonce
end

def secret_box(message, nonce)
message = self.class._message(message)
nonce = self.class._nonce(nonce)
ciphertext = Sodium::Buffer.empty(message.bytesize)

self.implementation.nacl(
ciphertext.to_str,
message.to_str,
message.to_str.bytesize,
nonce.to_str,
@key.to_str
) or raise Sodium::CryptoError, 'failed to close the secret box'
message = self.class._message(message)
nonce = self.class._nonce(nonce)

ciphertext.unpad self.implementation[:BOXZEROBYTES]
Sodium::Buffer.empty(message.bytesize) do |ciphertext|
self.implementation.nacl(
ciphertext.to_str,
message.to_str,
message.to_str.bytesize,
nonce.to_str,
@key.to_str
) or raise Sodium::CryptoError, 'failed to close the secret box'
end.unpad self.implementation[:BOXZEROBYTES]
end

def open(ciphertext, nonce)
ciphertext = self.class._ciphertext(ciphertext)
nonce = self.class._nonce(nonce)
message = Sodium::Buffer.empty(ciphertext.bytesize)

self.implementation.nacl_open(
message.to_str,
ciphertext.to_str,
ciphertext.to_str.bytesize,
nonce.to_str,
@key.to_str
) or raise Sodium::CryptoError, 'failed to open the secret box'

message.unpad self.implementation[:ZEROBYTES]
Sodium::Buffer.empty(ciphertext.bytesize) do |message|
self.implementation.nacl_open(
message.to_str,
ciphertext.to_str,
ciphertext.to_str.bytesize,
nonce.to_str,
@key.to_str
) or raise Sodium::CryptoError, 'failed to open the secret box'
end.unpad self.implementation[:ZEROBYTES]
end

private
Expand Down

0 comments on commit 0c189a3

Please sign in to comment.