Skip to content

Commit

Permalink
Merge pull request #65 from cryptosphere/nuke-encoding-system
Browse files Browse the repository at this point in the history
Remove the encoding system

This makes things one hell of a lot simpler to work with.  We do keep bin2hex/hex2bin though.  We're not monsters.
  • Loading branch information
namelessjon committed Aug 12, 2013
2 parents 269baed + 8f41d94 commit 11ef589
Show file tree
Hide file tree
Showing 43 changed files with 286 additions and 564 deletions.
1 change: 0 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,5 @@ group :development do
end

group :test do
gem 'base32'
gem 'coveralls', :require => false
end
3 changes: 1 addition & 2 deletions Guardfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

guard :rspec do
watch(%r{^spec/.+_spec\.rb$})
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
watch('spec/spec_helper.rb') { "spec" }
end

4 changes: 0 additions & 4 deletions lib/rbnacl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@ class IncorrectPrimitiveError < ArgumentError; end
require "rbnacl/hmac/sha256"
require "rbnacl/auth/one_time"
require "rbnacl/random"
require "rbnacl/encoder"
require "rbnacl/encoders/base64"
require "rbnacl/encoders/hex"
require "rbnacl/encoders/raw"
require "rbnacl/point"
require "rbnacl/random_nonce_box"
require "rbnacl/test_vectors"
Expand Down
16 changes: 6 additions & 10 deletions lib/rbnacl/auth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@ class Auth
# A new authenticator, ready for auth and verification
#
# @param [#to_str] key the key used for authenticators, 32 bytes.
# @param [#to_sym] encoding decode key from this format (default raw)
def initialize(key, encoding = :raw)
@key = Encoder[encoding].decode(key)
Util.check_length(@key, key_bytes, "#{self.class} key")
def initialize(key)
@key = Util.check_string(key, key_bytes, "#{self.class} key")
end

# Compute authenticator for message
Expand All @@ -48,25 +46,23 @@ def self.verify(key, message, authenticator)
# Compute authenticator for message
#
# @param [#to_str] message the message to authenticate
# @param [#to_sym] authenticator_encoding format of the authenticator (default raw)
#
# @return [String] The authenticator in the requested encoding (default raw)
def auth(message, authenticator_encoding = :raw)
def auth(message)
authenticator = Util.zeros(tag_bytes)
message = message.to_str
compute_authenticator(message, authenticator)
Encoder[authenticator_encoding].encode(authenticator)
authenticator
end

# Verifies the given authenticator with the message.
#
# @param [#to_str] authenticator to be checked
# @param [#to_str] message the message to be authenticated
# @param [#to_sym] authenticator_encoding format of the authenticator (default raw)
#
# @return [Boolean] Was it valid?
def verify(message, authenticator, authenticator_encoding = :raw)
auth = Encoder[authenticator_encoding].decode(authenticator)
def verify(message, authenticator)
auth = authenticator.to_s
return false unless auth.bytesize == tag_bytes
verify_message(message, auth)
end
Expand Down
4 changes: 2 additions & 2 deletions lib/rbnacl/box.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ class Box
#
# @return [Crypto::Box] The new Box, ready to use
def initialize(public_key, private_key, encoding = :raw)
@public_key = PublicKey === public_key ? public_key : PublicKey.new(public_key, encoding)
@private_key = PrivateKey === private_key ? private_key : PrivateKey.new(private_key, encoding)
@public_key = PublicKey === public_key ? public_key : PublicKey.new(public_key)
@private_key = PrivateKey === private_key ? private_key : PrivateKey.new(private_key)
raise IncorrectPrimitiveError unless @public_key.primitive == primitive && @private_key.primitive == primitive
end

Expand Down
44 changes: 0 additions & 44 deletions lib/rbnacl/encoder.rb

This file was deleted.

33 changes: 0 additions & 33 deletions lib/rbnacl/encoders/base32.rb

This file was deleted.

30 changes: 0 additions & 30 deletions lib/rbnacl/encoders/base64.rb

This file was deleted.

30 changes: 0 additions & 30 deletions lib/rbnacl/encoders/hex.rb

This file was deleted.

12 changes: 0 additions & 12 deletions lib/rbnacl/encoders/raw.rb

This file was deleted.

23 changes: 9 additions & 14 deletions lib/rbnacl/hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,31 @@ module Hash
#
# There's no streaming done, just pass in the data and be done with it.
#
# @param [String] data The data, as a collection of bytes
# @param [#to_sym] encoding Encoding of the returned hash.
# @param [#to_str] data The data, as a collection of bytes
#
# @raise [CryptoError] If the hashing fails for some reason.
#
# @return [String] The SHA-256 hash as raw bytes (Or encoded as per the second argument)
def self.sha256(data, encoding = :raw)
def self.sha256(data)
data = data.to_str
digest = Util.zeros(NaCl::SHA256BYTES)
NaCl.crypto_hash_sha256(digest, data, data.bytesize) || raise(CryptoError, "Hashing failed!")
Encoder[encoding].encode(digest)
digest
end

# Returns the SHA-512 hash of the given data
#
# There's no streaming done, just pass in the data and be done with it.
#
# @param [String] data The data, as a collection of bytes
# @param [#to_sym] encoding Encoding of the returned hash.
# @param [#to_str] data The data, as a collection of bytes
#
# @raise [CryptoError] If the hashing fails for some reason.
#
# @return [String] The SHA-512 hash as raw bytes (Or encoded as per the second argument)
def self.sha512(data, encoding = :raw)
def self.sha512(data)
digest = Util.zeros(NaCl::SHA512BYTES)
NaCl.crypto_hash_sha512(digest, data, data.bytesize) || raise(CryptoError, "Hashing failed!")
Encoder[encoding].encode(digest)
digest
end

if NaCl.supported_version? :libsodium, '0.4.0'
Expand All @@ -54,22 +53,18 @@ def self.sha512(data, encoding = :raw)
# @param [String] data The data, as a collection of bytes
# @option options [Fixnum] digest_size Size in bytes (1-64, default 64)
# @option options [String] key 64-byte (or less) key for keyed mode
# @option options [Symbol] encoding Output encoding format (default raw)
#
# @raise [CryptoError] If the hashing fails for some reason.
#
# @return [String] The blake2b hash as raw bytes (Or encoded as per the second argument)
def self.blake2b(data, options = {})
key = options[:key]
digest_size = options[:digest_size] || NaCl::BLAKE2B_OUTBYTES
encoding = options[:encoding] || :raw

digest = Blake2b.new(options).hash(data)
Encoder[encoding].encode(digest)
Blake2b.new(options).hash(data)
end

else
def self.blake2b(data, encoding = :raw)
def self.blake2b(data, options = {})
raise NotImplementedError, "not supported by this version of libsodium"
end
end
Expand Down
7 changes: 3 additions & 4 deletions lib/rbnacl/hash/blake2b.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Blake2b
# Create a new Blake2b hash object
#
# @param [Hash] opts Blake2b configuration
# @option opts [String] :key for Blake2b keyed mode
# @option opts [String] :key for Blake2b keyed mode
# @option opts [Integer] :digest_size size of output digest in bytes
#
# @raise [Crypto::LengthError] Invalid length specified for one or more options
Expand All @@ -31,13 +31,12 @@ def initialize(opts = {})
# Calculate a Blake2b hash
#
# @param [String] message Message to be hashed
# @param [#to_sym] encoding Encoding of the returned hash
#
# @return [String] Blake2b digest of the string as raw bytes
def hash(message, encoding = :raw)
def hash(message)
digest = Util.zeros(@digest_size)
NaCl.crypto_hash_blake2b(digest, @digest_size, message, message.bytesize, @key, @key_size) || raise(CryptoError, "Hashing failed!")
Encoder[encoding].encode(digest)
digest
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions lib/rbnacl/keys/private_key.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ class PrivateKey
# @param private_key [String] The private key
# @param key_encoding [Symbol] The encoding of the key
#
# @raise [TypeError] If the key is nil
# @raise [Crypto::LengthError] If the key is not valid after decoding.
#
# @return A new PrivateKey
def initialize(private_key, key_encoding = :raw)
@private_key = Crypto::Encoder[key_encoding].decode(private_key)
Util.check_length(@private_key, BYTES, "Private key")
def initialize(private_key)
@private_key = Util.check_string(private_key, BYTES, "Private key")
end

# Generates a new keypair
Expand Down
6 changes: 2 additions & 4 deletions lib/rbnacl/keys/public_key.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,12 @@ class PublicKey
# the exchanging of messages using a Crypto::Box
#
# @param public_key [String] The public key
# @param key_encoding [Symbol] The encoding of the key
#
# @raise [Crypto::LengthError] If the key is not valid after decoding.
#
# @return A new PublicKey
def initialize(public_key, key_encoding = :raw)
@public_key = Crypto::Encoder[key_encoding].decode(public_key)
Util.check_length(@public_key, BYTES, "Public key")
def initialize(public_key)
@public_key = Util.check_string(public_key, BYTES, "Public key")
end

# The raw bytes of the key
Expand Down
11 changes: 4 additions & 7 deletions lib/rbnacl/keys/signing_key.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,10 @@ def self.generate
# Create a SigningKey from a seed value
#
# @param seed [String] Random 32-byte value (i.e. private key)
# @param encoding [Symbol] Parse seed from the given encoding
#
# @return [Crypto::SigningKey] Key which can sign messages
def initialize(seed, encoding = :raw)
seed = Encoder[encoding].decode(seed)
def initialize(seed)
seed = seed.to_s

Util.check_length(seed, NaCl::ED25519_SEED_BYTES, "seed")

Expand All @@ -53,17 +52,15 @@ def initialize(seed, encoding = :raw)
# Sign a message using this key
#
# @param message [String] Message to be signed by this key
# @param encoding [Symbol] Encode signature in the given format
#
# @return [String] Signature as bytes
def sign(message, encoding = :raw)
def sign(message)
buffer = Util.prepend_zeros(signature_bytes, message)
buffer_len = Util.zeros(FFI::Type::LONG_LONG.size)

NaCl.crypto_sign_ed25519(buffer, buffer_len, message, message.bytesize, @signing_key)

signature = buffer[0, signature_bytes]
Encoder[encoding].encode(signature)
buffer[0, signature_bytes]
end

# Return the raw seed value of this key
Expand Down
Loading

0 comments on commit 11ef589

Please sign in to comment.