git ssb

0+

dangerousbeans / %aPBe2k3ugtjBr4rrsU1…



Commit 8f7acf49d55b45dbc448f40547d9087762263ddb

Finish JRuby support.

Signed-off-by: Coda Hale <coda.hale@gmail.com>
Hongli Lai (Phusion) authored on 8/4/2009, 9:37:35 PM
Coda Hale committed on 8/12/2009, 9:54:49 PM
Parent: 13543432e645183e79ad4e626fa8f0de0d5c02bb

Files changed

Rakefilechanged
ext/extconf.rbchanged
lib/bcrypt.rbchanged
RakefileView
@@ -14,9 +14,11 @@
1414 'lib/**/*.rb',
1515 'spec/**/*.rb',
1616 'ext/*.c',
1717 'ext/*.h',
18- 'ext/*.rb'
18+ 'ext/*.rb',
19+ 'ext/jruby/bcrypt_jruby/BCrypt.java',
20+ 'ext/jruby/bcrypt_jruby/BCrypt.class'
1921 ]
2022 CLEAN.include(
2123 "ext/*.o",
2224 "ext/*.bundle",
@@ -80,14 +82,33 @@
8082 pkg.need_zip = true
8183 pkg.need_tar = true
8284 end
8385
84-desc "Clean, then compile the extension."
85-task :compile => [:clean] do
86- Dir.chdir('ext') do
87- ruby "extconf.rb"
88- sh "make"
86+desc "Clean, then compile the extension that's native to the current Ruby compiler."
87+if RUBY_PLATFORM == "java"
88+ task :compile => 'compile:jruby'
89+else
90+ task :compile => 'compile:mri'
91+end
92+
93+namespace :compile do
94+ desc "CLean, then compile all extensions"
95+ task :all => [:mri, :jruby]
96+
97+ desc "Clean, then compile the MRI extension"
98+ task :mri => :clean do
99+ Dir.chdir('ext') do
100+ ruby "extconf.rb"
101+ sh "make"
102+ end
89103 end
104+
105+ desc "Clean, then compile the JRuby extension"
106+ task :jruby => :clean do
107+ Dir.chdir('ext/jruby/bcrypt_jruby') do
108+ sh "javac BCrypt.java"
109+ end
110+ end
90111 end
91112
92113 desc "Run a set of benchmarks on the compiled extension."
93114 task :benchmark do
ext/extconf.rbView
@@ -1,5 +1,18 @@
1-require "mkmf"
2-dir_config("bcrypt_ext")
3-# enable this when we're feeling nitpicky
4-# CONFIG['CC'] << " -Wall "
5-create_makefile("bcrypt_ext")
1+if RUBY_PLATFORM == "java"
2+ # Don't do anything when run in JRuby; this allows gem installation to pass.
3+ # We need to write a dummy Makefile so that RubyGems doesn't think compilation
4+ # failed.
5+ File.open('Makefile', 'w') do |f|
6+ f.puts "all:"
7+ f.puts "\t@true"
8+ f.puts "install:"
9+ f.puts "\t@true"
10+ end
11+ exit 0
12+else
13+ require "mkmf"
14+ dir_config("bcrypt_ext")
15+ # enable this when we're feeling nitpicky
16+ # CONFIG['CC'] << " -Wall "
17+ create_makefile("bcrypt_ext")
18+end
lib/bcrypt.rbView
@@ -1,9 +1,15 @@
11 # A wrapper for OpenBSD's bcrypt/crypt_blowfish password-hashing algorithm.
22
3-$: << "ext"
4-require "bcrypt_ext"
5-require "openssl"
3+if RUBY_PLATFORM == "java"
4+ require 'java'
5+ $CLASSPATH << File.expand_path(File.join(File.dirname(__FILE__), "..", "ext", "jruby"))
6+else
7+ $LOAD_PATH.unshift(File.expand_path(File.join(File.dirname(__FILE__), "..", "ext")))
8+ puts $LOAD_PATH
9+ require "bcrypt_ext"
10+ require "openssl"
11+end
612
713 # A Ruby library implementing OpenBSD's bcrypt()/crypt_blowfish algorithm for
814 # hashing passwords.
915 module BCrypt
@@ -13,26 +19,34 @@
1319 class InvalidCost < StandardError; end # The cost parameter provided to bcrypt() is invalid.
1420 class InvalidSecret < StandardError; end # The secret parameter provided to bcrypt() is invalid.
1521 end
1622
17- # A Ruby wrapper for the bcrypt() extension calls.
23+ # A Ruby wrapper for the bcrypt() C extension calls and the Java calls.
1824 class Engine
1925 # The default computational expense parameter.
2026 DEFAULT_COST = 10
27+ # The minimum cost supported by the algorithm.
28+ MIN_COST = 4
2129 # Maximum possible size of bcrypt() salts.
2230 MAX_SALT_LENGTH = 16
2331
24- # C-level routines which, if they don't get the right input, will crash the
25- # hell out of the Ruby process.
26- private_class_method :__bc_salt
27- private_class_method :__bc_crypt
32+ if RUBY_PLATFORM != "java"
33+ # C-level routines which, if they don't get the right input, will crash the
34+ # hell out of the Ruby process.
35+ private_class_method :__bc_salt
36+ private_class_method :__bc_crypt
37+ end
2838
2939 # Given a secret and a valid salt (see BCrypt::Engine.generate_salt) calculates
3040 # a bcrypt() password hash.
3141 def self.hash_secret(secret, salt)
3242 if valid_secret?(secret)
3343 if valid_salt?(salt)
34- __bc_crypt(secret.to_s, salt)
44+ if RUBY_PLATFORM == "java"
45+ Java.bcrypt_jruby.BCrypt.hashpw(secret.to_s, salt.to_s)
46+ else
47+ __bc_crypt(secret.to_s, salt)
48+ end
3549 else
3650 raise Errors::InvalidSalt.new("invalid salt")
3751 end
3852 else
@@ -41,10 +55,18 @@
4155 end
4256
4357 # Generates a random salt with a given computational cost.
4458 def self.generate_salt(cost = DEFAULT_COST)
45- if cost.to_i > 0
46- __bc_salt(cost, OpenSSL::Random.random_bytes(MAX_SALT_LENGTH))
59+ cost = cost.to_i
60+ if cost > 0
61+ if cost < MIN_COST
62+ cost = MIN_COST
63+ end
64+ if RUBY_PLATFORM == "java"
65+ Java.bcrypt_jruby.BCrypt.gensalt(cost)
66+ else
67+ __bc_salt(cost, OpenSSL::Random.random_bytes(MAX_SALT_LENGTH))
68+ end
4769 else
4870 raise Errors::InvalidCost.new("cost must be numeric and > 0")
4971 end
5072 end

Built with git-ssb-web