Skip to content

Commit

Permalink
spec passes
Browse files Browse the repository at this point in the history
  • Loading branch information
stakach committed Jul 10, 2013
1 parent 7922c7c commit a964b44
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 96 deletions.
35 changes: 20 additions & 15 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,32 +1,37 @@
PATH
remote: .
specs:
scrypt (1.1.0)
scrypt (1.2.0)
ffi-compiler (>= 0.0.2)
rake

GEM
remote: http://rubygems.org/
specs:
awesome_print (1.0.2)
diff-lcs (1.1.3)
json (1.7.0)
rake (0.9.2.2)
rdoc (3.12)
awesome_print (1.1.0)
diff-lcs (1.2.4)
ffi (1.9.0)
ffi-compiler (0.1.3)
ffi (>= 1.0.0)
rake
json (1.8.0)
rake (10.1.0)
rdoc (4.0.1)
json (~> 1.4)
rspec (2.9.0)
rspec-core (~> 2.9.0)
rspec-expectations (~> 2.9.0)
rspec-mocks (~> 2.9.0)
rspec-core (2.9.0)
rspec-expectations (2.9.1)
diff-lcs (~> 1.1.3)
rspec-mocks (2.9.0)
rspec (2.14.0)
rspec-core (~> 2.14.0)
rspec-expectations (~> 2.14.0)
rspec-mocks (~> 2.14.0)
rspec-core (2.14.0)
rspec-expectations (2.14.0)
diff-lcs (>= 1.1.3, < 2.0)
rspec-mocks (2.14.1)

PLATFORMS
ruby

DEPENDENCIES
awesome_print
rake (~> 0.9.2)
rdoc
rspec
scrypt!
52 changes: 27 additions & 25 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,23 +1,39 @@
require 'bundler'
Bundler::GemHelper.install_tasks
require 'bundler/setup'
require 'bundler/gem_tasks'

require 'rake'
require 'rake/clean'
require 'rdoc/task'

require 'rspec/core/rake_task'

require 'ffi'
require 'ffi-compiler/compile_task'

require 'rubygems'
require 'rubygems/package_task'
require 'ffi-compiler/export_task'

require 'rdoc/task'

def gem_spec
@gem_spec ||= Gem::Specification.load('scrypt.gemspec')
end
task :default => [:clean, :compile_ffi, :spec]

desc "clean, make and run specs"
task :spec do
RSpec::Core::RakeTask.new
end

task :default => [:compile, :spec]
desc "FFI compiler"
namespace "ffi-compiler" do
FFI::Compiler::CompileTask.new('ext/scrypt/scrypt_ext') do |t|
t.cflags << "-Wall -msse -msse2"
t.cflags << "-D_GNU_SOURCE=1" if RbConfig::CONFIG["host_os"].downcase =~ /mingw/
t.cflags << "-arch x86_64 -arch i386" if t.platform.mac?
t.ldflags << "-arch x86_64 -arch i386" if t.platform.mac?
end
end
task :compile_ffi => ["ffi-compiler:default"]

CLEAN.include('ext/scrypt/*{.o,.log,.so,.bundle}')
CLEAN.include('lib/**/*{.o,.log,.so,.bundle}')

desc 'Generate RDoc'
rd = Rake::RDocTask.new do |rdoc|
Expand All @@ -33,28 +49,14 @@ RSpec::Core::RakeTask.new do |t|
rspec_opts = ['--colour','--backtrace']
end


desc "Clean native extension build files."
task :clean do
end


desc "Compile the native extension."
task :compile do
Dir.chdir('ext/scrypt') do
ruby "rake"
end
end


FFI::Compiler::ExportTask.new('lib/scrypt', 'ext', :gem_spec => gem_spec) do |t|
t.export 'scrypt_ext.rb'
def gem_spec
@gem_spec ||= Gem::Specification.load('scrypt.gemspec')
end


Gem::PackageTask.new(gem_spec) do |pkg|
pkg.need_zip = true
pkg.need_tar = true
pkg.package_dir = 'pkg'
end


File renamed without changes.
File renamed without changes.
5 changes: 0 additions & 5 deletions ext/scrypt/extconf.rb

This file was deleted.

19 changes: 2 additions & 17 deletions ext/scrypt/scrypt_ext.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,10 @@ typedef struct {
uint64_t n;
uint32_t r;
uint32_t p;
uint64_t size;
} Calibration;


RBFFI_EXPORT int sc_calibrate(double maxmemfrac, double maxtime, void *out)
RBFFI_EXPORT int sc_calibrate(size_t maxmem, double maxmemfrac, double maxtime, Calibration *result)
{
Calibration *result = (Calibration *) out;
// result->size == maxmem
return calibrate(result->size, maxmemfrac, maxtime, &result->n, &result->r, &result->p); // 0 == success
}


RBFFI_EXPORT int sc_crypt(const char *safe_key, const char *safe_salt, void *buffer, void *in)
{
Calibration *settings = (Calibration *) in;
return crypto_scrypt(
(uint8_t *) safe_key, strlen(safe_key),
(uint8_t *) safe_salt, strlen(safe_salt),
settings->n, settings->r, settings->p,
(uint8_t *) buffer, settings->size
); // 0 == success
return calibrate(maxmem, maxmemfrac, maxtime, &result->n, &result->r, &result->p); // 0 == success
}
13 changes: 13 additions & 0 deletions ext/scrypt/scrypt_ext.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef SCRYPT_EXT_H
#define SCRYPT_EXT_H 1

#ifndef RBFFI_EXPORT
# ifdef __cplusplus
# define RBFFI_EXPORT extern "C"
# else
# define RBFFI_EXPORT
# endif
#endif


#endif /* SCRYPT_EXT_H */
48 changes: 23 additions & 25 deletions lib/scrypt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@

module SCrypt

module Ext
# Bind the external functions
attach_function :sc_calibrate, [:size_t, :double, :double, :pointer], :int, :blocking => true
attach_function :crypto_scrypt, [:pointer, :size_t, :pointer, :size_t, :uint64, :uint32, :uint32, :pointer, :size_t], :int, :blocking => true # todo
end

module Errors
class InvalidSalt < StandardError; end # The salt parameter provided is invalid.
class InvalidHash < StandardError; end # The hash parameter provided is invalid.
Expand Down Expand Up @@ -121,23 +127,19 @@ def self.autodetect_cost(salt)
class Calibration < FFI::Struct
layout :n, :uint64,
:r, :uint32,
:p, :uint32,
:size, :uint64
:p, :uint32
end

def self.__sc_calibrate(max_mem, max_memfrac, max_time)
result = nil

FFI::MemoryPointer.new :uint8, Calibration.size, false do |pointer|
calibration = Calibration.new pointer
calibration[:size] = max_mem
retval = SCrypt::Ext.sc_calibrate(max_memfrac, max_time, calibration)
calibration = Calibration.new
retval = SCrypt::Ext.sc_calibrate(max_mem, max_memfrac, max_time, calibration)

if retval == 0
result = [calibration[:n], calibration[:r], calibration[:p]]
else
raise "calibration error #{result}"
end
if retval == 0
result = [calibration[:n], calibration[:r], calibration[:p]]
else
raise "calibration error #{result}"
end

result
Expand All @@ -146,20 +148,16 @@ def self.__sc_calibrate(max_mem, max_memfrac, max_time)
def self.__sc_crypt(secret, salt, n, r, p, key_len)
result = nil

FFI::MemoryPointer.new :uint8, Calibration.size, false do |pointer|
calibration = Calibration.new pointer
calibration[:n] = n
calibration[:r] = r
calibration[:p] = p
calibration[:size] = key_len

FFI::MemoryPointer.new(:uint8, key_len, false) do |buffer|
retval = SCrypt::Ext.sc_crypt(secret, salt, buffer, calibration)
if retval == 0
result = buffer.get_string(0, key_len)
else
raise "scrypt error #{retval}"
end
FFI::MemoryPointer.new(:char, key_len) do |buffer|
retval = SCrypt::Ext.crypto_scrypt(
secret, secret.length, salt, salt.length,
n, r, p,
buffer, key_len
)
if retval == 0
result = buffer.read_string(key_len)
else
raise "scrypt error #{retval}"
end
end

Expand Down
4 changes: 0 additions & 4 deletions lib/scrypt/scrypt_ext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,5 @@ module SCrypt
module Ext
extend FFI::Library
ffi_lib FFI::Compiler::Loader.find('scrypt_ext')

# Bind the external functions
attach_function :sc_calibrate, [:double, :double, :pointer], :int, :blocking => true
attach_function :sc_crypt, [:string, :string, :pointer, :pointer], :int, :blocking => true
end
end
4 changes: 2 additions & 2 deletions spec/scrypt/engine_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ class MyInvalidSecret

it "should raise an InvalidSecret error if the secret is invalid" do
lambda { SCrypt::Engine.hash_secret(MyInvalidSecret.new, @salt) }.should raise_error(SCrypt::Errors::InvalidSecret)
lambda { SCrypt::Engine.hash_secret(nil, @salt) }.should_not raise_error(SCrypt::Errors::InvalidSecret)
lambda { SCrypt::Engine.hash_secret(false, @salt) }.should_not raise_error(SCrypt::Errors::InvalidSecret)
lambda { SCrypt::Engine.hash_secret(nil, @salt) }.should_not raise_error
lambda { SCrypt::Engine.hash_secret(false, @salt) }.should_not raise_error
end

it "should call #to_s on the secret and use the return value as the actual secret data" do
Expand Down
6 changes: 3 additions & 3 deletions spec/scrypt/password_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
end

it "should behave normally if the secret is not a string" do
lambda { SCrypt::Password.create(nil) }.should_not raise_error(SCrypt::Errors::InvalidSecret)
lambda { SCrypt::Password.create({:woo => "yeah"}) }.should_not raise_error(SCrypt::Errors::InvalidSecret)
lambda { SCrypt::Password.create(false) }.should_not raise_error(SCrypt::Errors::InvalidSecret)
lambda { SCrypt::Password.create(nil) }.should_not raise_error
lambda { SCrypt::Password.create({:woo => "yeah"}) }.should_not raise_error
lambda { SCrypt::Password.create(false) }.should_not raise_error
end

it "should tolerate empty string secrets" do
Expand Down

0 comments on commit a964b44

Please sign in to comment.