Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove the encoding system #65

Merged
merged 4 commits into from
Aug 12, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
18 changes: 1 addition & 17 deletions Guardfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +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" }

# Rails example
watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
watch(%r{^app/(.*)(\.erb|\.haml)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
watch('config/routes.rb') { "spec/routing" }
watch('app/controllers/application_controller.rb') { "spec/controllers" }

# Capybara features specs
watch(%r{^app/views/(.+)/.*\.(erb|haml)$}) { |m| "spec/features/#{m[1]}_spec.rb" }

# Turnip features and steps
watch(%r{^spec/acceptance/(.+)\.feature$})
watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance' }
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
Loading