bcrypt-ruby-maglev- 3.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,111 @@
1
+ /*
2
+ * Written by Solar Designer and placed in the public domain.
3
+ * See crypt_blowfish.c for more information.
4
+ *
5
+ * This file contains salt generation functions for the traditional and
6
+ * other common crypt(3) algorithms, except for bcrypt which is defined
7
+ * entirely in crypt_blowfish.c.
8
+ */
9
+
10
+ #include <string.h>
11
+
12
+ #include <errno.h>
13
+ #ifndef __set_errno
14
+ #define __set_errno(val) errno = (val)
15
+ #endif
16
+
17
+ #undef __CONST
18
+ #ifdef __GNUC__
19
+ #define __CONST __const
20
+ #else
21
+ #define __CONST
22
+ #endif
23
+
24
+ unsigned char _crypt_itoa64[64 + 1] =
25
+ "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
26
+
27
+ char *_crypt_gensalt_traditional_rn(unsigned long count,
28
+ __CONST char *input, int size, char *output, int output_size)
29
+ {
30
+ if (size < 2 || output_size < 2 + 1 || (count && count != 25)) {
31
+ if (output_size > 0) output[0] = '\0';
32
+ __set_errno((output_size < 2 + 1) ? ERANGE : EINVAL);
33
+ return NULL;
34
+ }
35
+
36
+ output[0] = _crypt_itoa64[(unsigned int)input[0] & 0x3f];
37
+ output[1] = _crypt_itoa64[(unsigned int)input[1] & 0x3f];
38
+ output[2] = '\0';
39
+
40
+ return output;
41
+ }
42
+
43
+ char *_crypt_gensalt_extended_rn(unsigned long count,
44
+ __CONST char *input, int size, char *output, int output_size)
45
+ {
46
+ unsigned long value;
47
+
48
+ /* Even iteration counts make it easier to detect weak DES keys from a look
49
+ * at the hash, so they should be avoided */
50
+ if (size < 3 || output_size < 1 + 4 + 4 + 1 ||
51
+ (count && (count > 0xffffff || !(count & 1)))) {
52
+ if (output_size > 0) output[0] = '\0';
53
+ __set_errno((output_size < 1 + 4 + 4 + 1) ? ERANGE : EINVAL);
54
+ return NULL;
55
+ }
56
+
57
+ if (!count) count = 725;
58
+
59
+ output[0] = '_';
60
+ output[1] = _crypt_itoa64[count & 0x3f];
61
+ output[2] = _crypt_itoa64[(count >> 6) & 0x3f];
62
+ output[3] = _crypt_itoa64[(count >> 12) & 0x3f];
63
+ output[4] = _crypt_itoa64[(count >> 18) & 0x3f];
64
+ value = (unsigned long)(unsigned char)input[0] |
65
+ ((unsigned long)(unsigned char)input[1] << 8) |
66
+ ((unsigned long)(unsigned char)input[2] << 16);
67
+ output[5] = _crypt_itoa64[value & 0x3f];
68
+ output[6] = _crypt_itoa64[(value >> 6) & 0x3f];
69
+ output[7] = _crypt_itoa64[(value >> 12) & 0x3f];
70
+ output[8] = _crypt_itoa64[(value >> 18) & 0x3f];
71
+ output[9] = '\0';
72
+
73
+ return output;
74
+ }
75
+
76
+ char *_crypt_gensalt_md5_rn(unsigned long count,
77
+ __CONST char *input, int size, char *output, int output_size)
78
+ {
79
+ unsigned long value;
80
+
81
+ if (size < 3 || output_size < 3 + 4 + 1 || (count && count != 1000)) {
82
+ if (output_size > 0) output[0] = '\0';
83
+ __set_errno((output_size < 3 + 4 + 1) ? ERANGE : EINVAL);
84
+ return NULL;
85
+ }
86
+
87
+ output[0] = '$';
88
+ output[1] = '1';
89
+ output[2] = '$';
90
+ value = (unsigned long)(unsigned char)input[0] |
91
+ ((unsigned long)(unsigned char)input[1] << 8) |
92
+ ((unsigned long)(unsigned char)input[2] << 16);
93
+ output[3] = _crypt_itoa64[value & 0x3f];
94
+ output[4] = _crypt_itoa64[(value >> 6) & 0x3f];
95
+ output[5] = _crypt_itoa64[(value >> 12) & 0x3f];
96
+ output[6] = _crypt_itoa64[(value >> 18) & 0x3f];
97
+ output[7] = '\0';
98
+
99
+ if (size >= 6 && output_size >= 3 + 4 + 4 + 1) {
100
+ value = (unsigned long)(unsigned char)input[3] |
101
+ ((unsigned long)(unsigned char)input[4] << 8) |
102
+ ((unsigned long)(unsigned char)input[5] << 16);
103
+ output[7] = _crypt_itoa64[value & 0x3f];
104
+ output[8] = _crypt_itoa64[(value >> 6) & 0x3f];
105
+ output[9] = _crypt_itoa64[(value >> 12) & 0x3f];
106
+ output[10] = _crypt_itoa64[(value >> 18) & 0x3f];
107
+ output[11] = '\0';
108
+ }
109
+
110
+ return output;
111
+ }
@@ -0,0 +1,16 @@
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
+ create_makefile("bcrypt_ext")
16
+ end
@@ -0,0 +1,35 @@
1
+ /*
2
+ * Written by Solar Designer and placed in the public domain.
3
+ * See crypt_blowfish.c for more information.
4
+ */
5
+
6
+ #ifndef _OW_CRYPT_H
7
+ #define _OW_CRYPT_H
8
+
9
+ #undef __CONST
10
+ #if defined __GNUC__
11
+ #define __CONST __const
12
+ #elif defined _MSC_VER
13
+ #define __CONST const
14
+ #else
15
+ #endif
16
+
17
+ #ifndef __SKIP_GNU
18
+ extern char *crypt(__CONST char *key, __CONST char *setting);
19
+ extern char *crypt_r(__CONST char *key, __CONST char *setting, void *data);
20
+ #endif
21
+
22
+ #ifndef __SKIP_OW
23
+ extern char *crypt_rn(__CONST char *key, __CONST char *setting,
24
+ void *data, int size);
25
+ extern char *crypt_ra(__CONST char *key, __CONST char *setting,
26
+ void **data, int *size);
27
+ extern char *crypt_gensalt(__CONST char *prefix, unsigned long count,
28
+ __CONST char *input, int size);
29
+ extern char *crypt_gensalt_rn(__CONST char *prefix, unsigned long count,
30
+ __CONST char *input, int size, char *output, int output_size);
31
+ extern char *crypt_gensalt_ra(__CONST char *prefix, unsigned long count,
32
+ __CONST char *input, int size);
33
+ #endif
34
+
35
+ #endif
data/ext/mri/wrapper.c ADDED
@@ -0,0 +1,258 @@
1
+ /*
2
+ * Written by Solar Designer and placed in the public domain.
3
+ * See crypt_blowfish.c for more information.
4
+ */
5
+
6
+ #include <stdlib.h>
7
+ #include <string.h>
8
+
9
+ #include <errno.h>
10
+ #ifndef __set_errno
11
+ #define __set_errno(val) errno = (val)
12
+ #endif
13
+
14
+ #ifdef TEST
15
+ #include <stdio.h>
16
+ #include <unistd.h>
17
+ #include <signal.h>
18
+ #include <time.h>
19
+ #include <sys/time.h>
20
+ #include <sys/times.h>
21
+ #ifdef TEST_THREADS
22
+ #include <pthread.h>
23
+ #endif
24
+ #endif
25
+
26
+ #include <ruby.h>
27
+ #include <util.h>
28
+
29
+ #define CRYPT_OUTPUT_SIZE (7 + 22 + 31 + 1)
30
+ #define CRYPT_GENSALT_OUTPUT_SIZE (7 + 22 + 1)
31
+
32
+ #if defined(__GLIBC__) && defined(_LIBC)
33
+ #define __SKIP_GNU
34
+ #endif
35
+ #include "ow-crypt.h"
36
+
37
+ extern char *_crypt_blowfish_rn(__CONST char *key, __CONST char *setting,
38
+ char *output, int size);
39
+ extern char *_crypt_gensalt_blowfish_rn(unsigned long count,
40
+ __CONST char *input, int size, char *output, int output_size);
41
+
42
+ extern unsigned char _crypt_itoa64[];
43
+ extern char *_crypt_gensalt_traditional_rn(unsigned long count,
44
+ __CONST char *input, int size, char *output, int output_size);
45
+ extern char *_crypt_gensalt_extended_rn(unsigned long count,
46
+ __CONST char *input, int size, char *output, int output_size);
47
+ extern char *_crypt_gensalt_md5_rn(unsigned long count,
48
+ __CONST char *input, int size, char *output, int output_size);
49
+
50
+ #if defined(__GLIBC__) && defined(_LIBC)
51
+ /* crypt.h from glibc-crypt-2.1 will define struct crypt_data for us */
52
+ #include "crypt.h"
53
+ extern char *__md5_crypt_r(const char *key, const char *salt,
54
+ char *buffer, int buflen);
55
+ /* crypt-entry.c needs to be patched to define __des_crypt_r rather than
56
+ * __crypt_r, and not define crypt_r and crypt at all */
57
+ extern char *__des_crypt_r(const char *key, const char *salt,
58
+ struct crypt_data *data);
59
+ extern struct crypt_data _ufc_foobar;
60
+ #endif
61
+
62
+ static int _crypt_data_alloc(void **data, int *size, int need)
63
+ {
64
+ void *updated;
65
+
66
+ if (*data && *size >= need) return 0;
67
+
68
+ updated = realloc(*data, need);
69
+
70
+ if (!updated) {
71
+ #ifndef __GLIBC__
72
+ /* realloc(3) on glibc sets errno, so we don't need to bother */
73
+ __set_errno(ENOMEM);
74
+ #endif
75
+ return -1;
76
+ }
77
+
78
+ #if defined(__GLIBC__) && defined(_LIBC)
79
+ if (need >= sizeof(struct crypt_data))
80
+ ((struct crypt_data *)updated)->initialized = 0;
81
+ #endif
82
+
83
+ *data = updated;
84
+ *size = need;
85
+
86
+ return 0;
87
+ }
88
+
89
+ static char *_crypt_retval_magic(char *retval, __CONST char *setting,
90
+ char *output)
91
+ {
92
+ if (retval) return retval;
93
+
94
+ output[0] = '*';
95
+ output[1] = '0';
96
+ output[2] = '\0';
97
+
98
+ if (setting[0] == '*' && setting[1] == '0')
99
+ output[1] = '1';
100
+
101
+ return output;
102
+ }
103
+
104
+ #if defined(__GLIBC__) && defined(_LIBC)
105
+ /*
106
+ * Applications may re-use the same instance of struct crypt_data without
107
+ * resetting the initialized field in order to let crypt_r() skip some of
108
+ * its initialization code. Thus, it is important that our multiple hashing
109
+ * algorithms either don't conflict with each other in their use of the
110
+ * data area or reset the initialized field themselves whenever required.
111
+ * Currently, the hashing algorithms simply have no conflicts: the first
112
+ * field of struct crypt_data is the 128-byte large DES key schedule which
113
+ * __des_crypt_r() calculates each time it is called while the two other
114
+ * hashing algorithms use less than 128 bytes of the data area.
115
+ */
116
+
117
+ char *__crypt_rn(__const char *key, __const char *setting,
118
+ void *data, int size)
119
+ {
120
+ if (setting[0] == '$' && setting[1] == '2')
121
+ return _crypt_blowfish_rn(key, setting, (char *)data, size);
122
+ if (setting[0] == '$' && setting[1] == '1')
123
+ return __md5_crypt_r(key, setting, (char *)data, size);
124
+ if (setting[0] == '$' || setting[0] == '_') {
125
+ __set_errno(EINVAL);
126
+ return NULL;
127
+ }
128
+ if (size >= sizeof(struct crypt_data))
129
+ return __des_crypt_r(key, setting, (struct crypt_data *)data);
130
+ __set_errno(ERANGE);
131
+ return NULL;
132
+ }
133
+
134
+ char *__crypt_ra(__const char *key, __const char *setting,
135
+ void **data, int *size)
136
+ {
137
+ if (setting[0] == '$' && setting[1] == '2') {
138
+ if (_crypt_data_alloc(data, size, CRYPT_OUTPUT_SIZE))
139
+ return NULL;
140
+ return _crypt_blowfish_rn(key, setting, (char *)*data, *size);
141
+ }
142
+ if (setting[0] == '$' && setting[1] == '1') {
143
+ if (_crypt_data_alloc(data, size, CRYPT_OUTPUT_SIZE))
144
+ return NULL;
145
+ return __md5_crypt_r(key, setting, (char *)*data, *size);
146
+ }
147
+ if (setting[0] == '$' || setting[0] == '_') {
148
+ __set_errno(EINVAL);
149
+ return NULL;
150
+ }
151
+ if (_crypt_data_alloc(data, size, sizeof(struct crypt_data)))
152
+ return NULL;
153
+ return __des_crypt_r(key, setting, (struct crypt_data *)*data);
154
+ }
155
+
156
+ char *__crypt_r(__const char *key, __const char *setting,
157
+ struct crypt_data *data)
158
+ {
159
+ return _crypt_retval_magic(
160
+ __crypt_rn(key, setting, data, sizeof(*data)),
161
+ setting, (char *)data);
162
+ }
163
+
164
+ char *__crypt(__const char *key, __const char *setting)
165
+ {
166
+ return _crypt_retval_magic(
167
+ __crypt_rn(key, setting, &_ufc_foobar, sizeof(_ufc_foobar)),
168
+ setting, (char *)&_ufc_foobar);
169
+ }
170
+ #else
171
+ char *crypt_rn(__CONST char *key, __CONST char *setting, void *data, int size)
172
+ {
173
+ return _crypt_blowfish_rn(key, setting, (char *)data, size);
174
+ }
175
+
176
+ char *crypt_ra(__CONST char *key, __CONST char *setting,
177
+ void **data, int *size)
178
+ {
179
+ if (_crypt_data_alloc(data, size, CRYPT_OUTPUT_SIZE))
180
+ return NULL;
181
+ return _crypt_blowfish_rn(key, setting, (char *)*data, *size);
182
+ }
183
+
184
+ char *crypt_r(__CONST char *key, __CONST char *setting, void *data)
185
+ {
186
+ return _crypt_retval_magic(
187
+ crypt_rn(key, setting, data, CRYPT_OUTPUT_SIZE),
188
+ setting, (char *)data);
189
+ }
190
+
191
+ #define __crypt_gensalt_rn crypt_gensalt_rn
192
+ #define __crypt_gensalt_ra crypt_gensalt_ra
193
+ #define __crypt_gensalt crypt_gensalt
194
+ #endif
195
+
196
+ char *__crypt_gensalt_rn(__CONST char *prefix, unsigned long count,
197
+ __CONST char *input, int size, char *output, int output_size)
198
+ {
199
+ char *(*use)(unsigned long count,
200
+ __CONST char *input, int size, char *output, int output_size);
201
+
202
+ /* This may be supported on some platforms in the future */
203
+ if (!input) {
204
+ __set_errno(EINVAL);
205
+ return NULL;
206
+ }
207
+
208
+ if (!strncmp(prefix, "$2a$", 4))
209
+ use = _crypt_gensalt_blowfish_rn;
210
+ else
211
+ if (!strncmp(prefix, "$1$", 3))
212
+ use = _crypt_gensalt_md5_rn;
213
+ else
214
+ if (prefix[0] == '_')
215
+ use = _crypt_gensalt_extended_rn;
216
+ else
217
+ if (!prefix[0] ||
218
+ (prefix[0] && prefix[1] &&
219
+ memchr(_crypt_itoa64, prefix[0], 64) &&
220
+ memchr(_crypt_itoa64, prefix[1], 64)))
221
+ use = _crypt_gensalt_traditional_rn;
222
+ else {
223
+ __set_errno(EINVAL);
224
+ return NULL;
225
+ }
226
+
227
+ return use(count, input, size, output, output_size);
228
+ }
229
+
230
+ char *__crypt_gensalt_ra(__CONST char *prefix, unsigned long count,
231
+ __CONST char *input, int size)
232
+ {
233
+ char output[CRYPT_GENSALT_OUTPUT_SIZE];
234
+ char *retval;
235
+
236
+ retval = __crypt_gensalt_rn(prefix, count,
237
+ input, size, output, sizeof(output));
238
+
239
+ if (retval) {
240
+ retval = ruby_strdup(retval);
241
+ #ifndef __GLIBC__
242
+ /* strdup(3) on glibc sets errno, so we don't need to bother */
243
+ if (!retval)
244
+ __set_errno(ENOMEM);
245
+ #endif
246
+ }
247
+
248
+ return retval;
249
+ }
250
+
251
+ char *__crypt_gensalt(__CONST char *prefix, unsigned long count,
252
+ __CONST char *input, int size)
253
+ {
254
+ static char output[CRYPT_GENSALT_OUTPUT_SIZE];
255
+
256
+ return __crypt_gensalt_rn(prefix, count,
257
+ input, size, output, sizeof(output));
258
+ }
data/lib/bcrypt.rb ADDED
@@ -0,0 +1,192 @@
1
+ # A wrapper for OpenBSD's bcrypt/crypt_blowfish password-hashing algorithm.
2
+
3
+ if RUBY_PLATFORM == "java"
4
+ require 'java'
5
+ else
6
+ require "openssl"
7
+ end
8
+
9
+ require 'bcrypt_ext'
10
+
11
+ # A Ruby library implementing OpenBSD's bcrypt()/crypt_blowfish algorithm for
12
+ # hashing passwords.
13
+ module BCrypt
14
+ module Errors
15
+ class InvalidSalt < StandardError; end # The salt parameter provided to bcrypt() is invalid.
16
+ class InvalidHash < StandardError; end # The hash parameter provided to bcrypt() is invalid.
17
+ class InvalidCost < StandardError; end # The cost parameter provided to bcrypt() is invalid.
18
+ class InvalidSecret < StandardError; end # The secret parameter provided to bcrypt() is invalid.
19
+ end
20
+
21
+ # A Ruby wrapper for the bcrypt() C extension calls and the Java calls.
22
+ class Engine
23
+ # The default computational expense parameter.
24
+ DEFAULT_COST = 10
25
+ # The minimum cost supported by the algorithm.
26
+ MIN_COST = 4
27
+ # Maximum possible size of bcrypt() salts.
28
+ MAX_SALT_LENGTH = 16
29
+
30
+ if RUBY_PLATFORM != "java"
31
+ # C-level routines which, if they don't get the right input, will crash the
32
+ # hell out of the Ruby process.
33
+ private_class_method :__bc_salt
34
+ private_class_method :__bc_crypt
35
+ end
36
+
37
+ # Given a secret and a valid salt (see BCrypt::Engine.generate_salt) calculates
38
+ # a bcrypt() password hash.
39
+ def self.hash_secret(secret, salt, cost = nil)
40
+ if valid_secret?(secret)
41
+ if valid_salt?(salt)
42
+ if cost.nil?
43
+ cost = autodetect_cost(salt)
44
+ end
45
+
46
+ if RUBY_PLATFORM == "java"
47
+ Java.bcrypt_jruby.BCrypt.hashpw(secret.to_s, salt.to_s)
48
+ else
49
+ __bc_crypt(secret.to_s, salt)
50
+ end
51
+ else
52
+ raise Errors::InvalidSalt.new("invalid salt")
53
+ end
54
+ else
55
+ raise Errors::InvalidSecret.new("invalid secret")
56
+ end
57
+ end
58
+
59
+ # Generates a random salt with a given computational cost.
60
+ def self.generate_salt(cost = DEFAULT_COST)
61
+ cost = cost.to_i
62
+ if cost > 0
63
+ if cost < MIN_COST
64
+ cost = MIN_COST
65
+ end
66
+ if RUBY_PLATFORM == "java"
67
+ Java.bcrypt_jruby.BCrypt.gensalt(cost)
68
+ else
69
+ prefix = "$2a$05$CCCCCCCCCCCCCCCCCCCCC.E5YPO9kmyuRGyh0XouQYb4YMJKvyOeW"
70
+ __bc_salt(prefix, cost, OpenSSL::Random.random_bytes(MAX_SALT_LENGTH))
71
+ end
72
+ else
73
+ raise Errors::InvalidCost.new("cost must be numeric and > 0")
74
+ end
75
+ end
76
+
77
+ # Returns true if +salt+ is a valid bcrypt() salt, false if not.
78
+ def self.valid_salt?(salt)
79
+ !!(salt =~ /^\$[0-9a-z]{2,}\$[0-9]{2,}\$[A-Za-z0-9\.\/]{22,}$/)
80
+ end
81
+
82
+ # Returns true if +secret+ is a valid bcrypt() secret, false if not.
83
+ def self.valid_secret?(secret)
84
+ secret.respond_to?(:to_s)
85
+ end
86
+
87
+ # Returns the cost factor which will result in computation times less than +upper_time_limit_in_ms+.
88
+ #
89
+ # Example:
90
+ #
91
+ # BCrypt.calibrate(200) #=> 10
92
+ # BCrypt.calibrate(1000) #=> 12
93
+ #
94
+ # # should take less than 200ms
95
+ # BCrypt::Password.create("woo", :cost => 10)
96
+ #
97
+ # # should take less than 1000ms
98
+ # BCrypt::Password.create("woo", :cost => 12)
99
+ def self.calibrate(upper_time_limit_in_ms)
100
+ 40.times do |i|
101
+ start_time = Time.now
102
+ Password.create("testing testing", :cost => i+1)
103
+ end_time = Time.now - start_time
104
+ return i if end_time * 1_000 > upper_time_limit_in_ms
105
+ end
106
+ end
107
+
108
+ # Autodetects the cost from the salt string.
109
+ def self.autodetect_cost(salt)
110
+ salt[4..5].to_i
111
+ end
112
+ end
113
+
114
+ # A password management class which allows you to safely store users' passwords and compare them.
115
+ #
116
+ # Example usage:
117
+ #
118
+ # include BCrypt
119
+ #
120
+ # # hash a user's password
121
+ # @password = Password.create("my grand secret")
122
+ # @password #=> "$2a$10$GtKs1Kbsig8ULHZzO1h2TetZfhO4Fmlxphp8bVKnUlZCBYYClPohG"
123
+ #
124
+ # # store it safely
125
+ # @user.update_attribute(:password, @password)
126
+ #
127
+ # # read it back
128
+ # @user.reload!
129
+ # @db_password = Password.new(@user.password)
130
+ #
131
+ # # compare it after retrieval
132
+ # @db_password == "my grand secret" #=> true
133
+ # @db_password == "a paltry guess" #=> false
134
+ #
135
+ class Password < String
136
+ # The hash portion of the stored password hash.
137
+ attr_reader :checksum
138
+ # The salt of the store password hash (including version and cost).
139
+ attr_reader :salt
140
+ # The version of the bcrypt() algorithm used to create the hash.
141
+ attr_reader :version
142
+ # The cost factor used to create the hash.
143
+ attr_reader :cost
144
+
145
+ class << self
146
+ # Hashes a secret, returning a BCrypt::Password instance. Takes an optional <tt>:cost</tt> option, which is a
147
+ # logarithmic variable which determines how computational expensive the hash is to calculate (a <tt>:cost</tt> of
148
+ # 4 is twice as much work as a <tt>:cost</tt> of 3). The higher the <tt>:cost</tt> the harder it becomes for
149
+ # attackers to try to guess passwords (even if a copy of your database is stolen), but the slower it is to check
150
+ # users' passwords.
151
+ #
152
+ # Example:
153
+ #
154
+ # @password = BCrypt::Password.create("my secret", :cost => 13)
155
+ def create(secret, options = { :cost => BCrypt::Engine::DEFAULT_COST })
156
+ raise ArgumentError if options[:cost] > 31
157
+ Password.new(BCrypt::Engine.hash_secret(secret, BCrypt::Engine.generate_salt(options[:cost]), options[:cost]))
158
+ end
159
+ end
160
+
161
+ # Initializes a BCrypt::Password instance with the data from a stored hash.
162
+ def initialize(raw_hash)
163
+ if valid_hash?(raw_hash)
164
+ self.replace(raw_hash)
165
+ @version, @cost, @salt, @checksum = split_hash(self)
166
+ else
167
+ raise Errors::InvalidHash.new("invalid hash")
168
+ end
169
+ end
170
+
171
+ # Compares a potential secret against the hash. Returns true if the secret is the original secret, false otherwise.
172
+ def ==(secret)
173
+ super(BCrypt::Engine.hash_secret(secret, @salt))
174
+ end
175
+ alias_method :is_password?, :==
176
+
177
+ private
178
+ # Returns true if +h+ is a valid hash.
179
+ def valid_hash?(h)
180
+ h =~ /^\$[0-9a-z]{2}\$[0-9]{2}\$[A-Za-z0-9\.\/]{53}$/
181
+ end
182
+
183
+ # call-seq:
184
+ # split_hash(raw_hash) -> version, cost, salt, hash
185
+ #
186
+ # Splits +h+ into version, cost, salt, and hash and returns them in that order.
187
+ def split_hash(h)
188
+ _, v, c, mash = h.split('$')
189
+ return v, c.to_i, h[0, 29].to_str, mash[-31, 31].to_str
190
+ end
191
+ end
192
+ end