xcrypt 0.1.1 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/xcrypt/version.rb +1 -1
- data/lib/xcrypt.rb +160 -0
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: e50289aa4e577c8954a17b6ee720629448716ba51e93673ff49bce3d065e8260
|
|
4
|
+
data.tar.gz: 8df23e9c5de9299d05456f52752a6db9f6099234ed958e3f8a49dbd5ab0eff6c
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 1ee8df7d64fdea9f032435f67f886d622f63fbea47dd9f5156a03c45834d446177d807ee4c4ba7ddf3282c65790e695f2a641455fd3a0abd48319d3ee58181a7
|
|
7
|
+
data.tar.gz: 7acd5509656ba24800a87dcc2d8f6e20e53faeecc8a1bb1d253d21849266c1be9b3cae44c00f9b45d821fe282e850656b1309b0c17d645d27a7b28406ca62e19
|
data/lib/xcrypt/version.rb
CHANGED
data/lib/xcrypt.rb
CHANGED
|
@@ -1,11 +1,36 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
# Top-level module providing a high-level Ruby interface to libxcrypt, a
|
|
4
|
+
# modern library for one-way hashing of passwords.
|
|
5
|
+
#
|
|
6
|
+
# All public methods are available directly on the module.
|
|
7
|
+
# The most common entry points are the algorithm-specific convenience methods
|
|
8
|
+
# ({yescrypt}, {bcrypt}, {sha512}, etc.) and {verify}.
|
|
9
|
+
#
|
|
10
|
+
# @example Hash a password with yescrypt (the strongest supported algorithm)
|
|
11
|
+
# hash = XCrypt.yescrypt("correct horse battery staple")
|
|
12
|
+
# XCrypt.verify("correct horse battery staple", hash) #=> true
|
|
13
|
+
#
|
|
14
|
+
# @example Hash with an explicit cost factor
|
|
15
|
+
# hash = XCrypt.bcrypt("hunter2", cost: 12)
|
|
16
|
+
#
|
|
17
|
+
# @example Use the generic interface
|
|
18
|
+
# hash = XCrypt.crypt("hunter2", algorithm: :sha512)
|
|
3
19
|
module XCrypt
|
|
4
20
|
require "xcrypt/version"
|
|
5
21
|
require "xcrypt/ffi"
|
|
6
22
|
|
|
23
|
+
# Raised when hashing or salt generation fails. Common causes include an
|
|
24
|
+
# unsupported algorithm, a malformed setting string, or a passphrase that
|
|
25
|
+
# exceeds {FFI::CRYPT_MAX_PASSPHRASE_SIZE} bytes.
|
|
7
26
|
Error ||= Class.new(StandardError)
|
|
8
27
|
|
|
28
|
+
# Maps each supported algorithm name to its setting-string prefix.
|
|
29
|
+
#
|
|
30
|
+
# The prefix is the leading characters of any hash produced by that
|
|
31
|
+
# algorithm and is used to identify the algorithm from an existing hash.
|
|
32
|
+
#
|
|
33
|
+
# @return [Hash{Symbol => String}]
|
|
9
34
|
ALGORITHMS = {
|
|
10
35
|
yescrypt: "$y$",
|
|
11
36
|
gost_yescrypt: "$gy$",
|
|
@@ -25,6 +50,77 @@ module XCrypt
|
|
|
25
50
|
|
|
26
51
|
extend self
|
|
27
52
|
|
|
53
|
+
# @!method yescrypt(phrase, setting = nil, cost: nil)
|
|
54
|
+
# Hash +phrase+ using yescrypt, the strongest supported algorithm.
|
|
55
|
+
# @param phrase [String] the password to hash
|
|
56
|
+
# @param setting [String, nil] an existing hash or salt string to use as
|
|
57
|
+
# the setting; a new setting is generated automatically when +nil+
|
|
58
|
+
# @param cost [Integer, nil] work-factor override; uses the library
|
|
59
|
+
# default when +nil+
|
|
60
|
+
# @return [String] the hashed password
|
|
61
|
+
# @raise [ArgumentError] if +setting+ belongs to a different algorithm
|
|
62
|
+
# @raise [Error] if hashing fails
|
|
63
|
+
|
|
64
|
+
# @!method gost_yescrypt(phrase, setting = nil, cost: nil)
|
|
65
|
+
# Hash +phrase+ using GOST R 34.11-2012 combined with yescrypt.
|
|
66
|
+
# @param (see #yescrypt)
|
|
67
|
+
# @return (see #yescrypt)
|
|
68
|
+
# @raise (see #yescrypt)
|
|
69
|
+
|
|
70
|
+
# @!method scrypt(phrase, setting = nil, cost: nil)
|
|
71
|
+
# Hash +phrase+ using scrypt.
|
|
72
|
+
# @param (see #yescrypt)
|
|
73
|
+
# @return (see #yescrypt)
|
|
74
|
+
# @raise (see #yescrypt)
|
|
75
|
+
|
|
76
|
+
# @!method bcrypt(phrase, setting = nil, cost: nil)
|
|
77
|
+
# Hash +phrase+ using bcrypt (Blowfish-based password hashing).
|
|
78
|
+
# @param (see #yescrypt)
|
|
79
|
+
# @return (see #yescrypt)
|
|
80
|
+
# @raise (see #yescrypt)
|
|
81
|
+
|
|
82
|
+
# @!method sha512(phrase, setting = nil, cost: nil)
|
|
83
|
+
# Hash +phrase+ using SHA-512 crypt.
|
|
84
|
+
# @param (see #yescrypt)
|
|
85
|
+
# @return (see #yescrypt)
|
|
86
|
+
# @raise (see #yescrypt)
|
|
87
|
+
|
|
88
|
+
# @!method sha256(phrase, setting = nil, cost: nil)
|
|
89
|
+
# Hash +phrase+ using SHA-256 crypt.
|
|
90
|
+
# @param (see #yescrypt)
|
|
91
|
+
# @return (see #yescrypt)
|
|
92
|
+
# @raise (see #yescrypt)
|
|
93
|
+
|
|
94
|
+
# @!method sha1(phrase, setting = nil, cost: nil)
|
|
95
|
+
# Hash +phrase+ using HMAC-SHA1 NetBSD crypt.
|
|
96
|
+
# @param (see #yescrypt)
|
|
97
|
+
# @return (see #yescrypt)
|
|
98
|
+
# @raise (see #yescrypt)
|
|
99
|
+
|
|
100
|
+
# @!method sun_md5(phrase, setting = nil, cost: nil)
|
|
101
|
+
# Hash +phrase+ using SunMD5 (Solaris MD5 crypt).
|
|
102
|
+
# @param (see #yescrypt)
|
|
103
|
+
# @return (see #yescrypt)
|
|
104
|
+
# @raise (see #yescrypt)
|
|
105
|
+
|
|
106
|
+
# @!method md5(phrase, setting = nil, cost: nil)
|
|
107
|
+
# Hash +phrase+ using MD5 crypt.
|
|
108
|
+
# @param (see #yescrypt)
|
|
109
|
+
# @return (see #yescrypt)
|
|
110
|
+
# @raise (see #yescrypt)
|
|
111
|
+
|
|
112
|
+
# @!method bsdi_des(phrase, setting = nil, cost: nil)
|
|
113
|
+
# Hash +phrase+ using BSDi extended DES crypt.
|
|
114
|
+
# @param (see #yescrypt)
|
|
115
|
+
# @return (see #yescrypt)
|
|
116
|
+
# @raise (see #yescrypt)
|
|
117
|
+
|
|
118
|
+
# @!method des(phrase, setting = nil, cost: nil)
|
|
119
|
+
# Hash +phrase+ using traditional DES crypt.
|
|
120
|
+
# @param (see #yescrypt)
|
|
121
|
+
# @return (see #yescrypt)
|
|
122
|
+
# @raise (see #yescrypt)
|
|
123
|
+
|
|
28
124
|
ALGORITHMS.each_key do |algorithm|
|
|
29
125
|
define_method(algorithm) do |phrase, setting = nil, cost: nil|
|
|
30
126
|
if setting
|
|
@@ -37,10 +133,37 @@ module XCrypt
|
|
|
37
133
|
end
|
|
38
134
|
end
|
|
39
135
|
|
|
136
|
+
# Returns the names of all supported algorithms.
|
|
137
|
+
#
|
|
138
|
+
# @return [Array<Symbol>] algorithm names in order from strongest to weakest
|
|
40
139
|
def algorithms = ALGORITHMS.keys
|
|
41
140
|
|
|
141
|
+
# Detects which algorithm produced a given setting or hash string by
|
|
142
|
+
# matching its leading prefix against {ALGORITHMS}.
|
|
143
|
+
#
|
|
144
|
+
# @param setting [String] a setting string or an existing password hash
|
|
145
|
+
# @return [Symbol, nil] the algorithm name, or +nil+ if the prefix is
|
|
146
|
+
# unrecognized
|
|
42
147
|
def detect_algorithm(setting) = PREFIXES[setting[/\A\$\w+\$?|_/].to_s]
|
|
43
148
|
|
|
149
|
+
# Hashes +phrase+ using libxcrypt's +crypt_rn+ function.
|
|
150
|
+
#
|
|
151
|
+
# When both +setting+ and +algorithm+ are omitted, a fresh setting is
|
|
152
|
+
# generated with the library's default algorithm. The result is always a
|
|
153
|
+
# self-describing string whose leading prefix identifies the algorithm and
|
|
154
|
+
# encodes the salt, making it safe to store directly.
|
|
155
|
+
#
|
|
156
|
+
# @param phrase [String] the password to hash
|
|
157
|
+
# @param setting [String, Symbol, nil] an existing hash or salt string, or
|
|
158
|
+
# an algorithm +Symbol+ as shorthand for passing only +algorithm:+;
|
|
159
|
+
# generates a fresh setting when +nil+
|
|
160
|
+
# @param algorithm [Symbol, nil] algorithm to use when generating a new
|
|
161
|
+
# setting; ignored when +setting+ is already a String
|
|
162
|
+
# @param cost [Integer, nil] work-factor override passed to
|
|
163
|
+
# {generate_setting}; uses the library default when +nil+
|
|
164
|
+
# @return [String] the hashed password
|
|
165
|
+
# @raise [Error] if +crypt_rn+ returns +NULL+, indicating an invalid
|
|
166
|
+
# setting or an unsupported algorithm
|
|
44
167
|
def crypt(phrase, setting = nil, algorithm: nil, cost: nil)
|
|
45
168
|
setting, algorithm = nil, setting if setting.is_a? Symbol
|
|
46
169
|
setting ||= generate_setting(algorithm, cost:)
|
|
@@ -52,6 +175,17 @@ module XCrypt
|
|
|
52
175
|
data&.clear
|
|
53
176
|
end
|
|
54
177
|
|
|
178
|
+
# Verifies that +phrase+ matches a previously computed +hash+.
|
|
179
|
+
#
|
|
180
|
+
# Returns +false+ immediately for any hash value that would cause
|
|
181
|
+
# libxcrypt to return a magic failure token (strings beginning with +"*"+),
|
|
182
|
+
# or for empty or +nil+ input, guarding against invalid-hash oracle attacks.
|
|
183
|
+
# The final comparison is performed in constant time to prevent timing
|
|
184
|
+
# attacks.
|
|
185
|
+
#
|
|
186
|
+
# @param phrase [String] the candidate password
|
|
187
|
+
# @param hash [String, nil] the stored password hash to verify against
|
|
188
|
+
# @return [Boolean] +true+ if +phrase+ matches +hash+, +false+ otherwise
|
|
55
189
|
def verify(phrase, hash)
|
|
56
190
|
return false if hash.nil? || hash.empty? || hash.start_with?("*")
|
|
57
191
|
result = crypt(phrase, hash)
|
|
@@ -60,6 +194,19 @@ module XCrypt
|
|
|
60
194
|
false
|
|
61
195
|
end
|
|
62
196
|
|
|
197
|
+
# Generates a fresh setting string suitable for passing to {crypt}.
|
|
198
|
+
#
|
|
199
|
+
# Delegates to libxcrypt's +crypt_gensalt_rn+, which draws entropy from
|
|
200
|
+
# the OS to produce the random salt component. When +algorithm+ is +nil+,
|
|
201
|
+
# the library selects its preferred (strongest) algorithm.
|
|
202
|
+
#
|
|
203
|
+
# @param algorithm [Symbol, nil] the desired algorithm; uses the library
|
|
204
|
+
# default when +nil+
|
|
205
|
+
# @param cost [Integer, nil] work-factor for the generated setting; a value
|
|
206
|
+
# of +0+ selects the library's own default cost
|
|
207
|
+
# @return [String] a setting string beginning with the algorithm prefix
|
|
208
|
+
# @raise [ArgumentError] if +algorithm+ is not a key in {ALGORITHMS}
|
|
209
|
+
# @raise [Error] if +crypt_gensalt_rn+ returns +NULL+
|
|
63
210
|
def generate_setting(algorithm = nil, cost: nil)
|
|
64
211
|
prefix = ALGORITHMS.fetch(algorithm) { raise ArgumentError, "unknown algorithm: #{algorithm.inspect}" } if algorithm
|
|
65
212
|
cost ||= 0
|
|
@@ -73,6 +220,19 @@ module XCrypt
|
|
|
73
220
|
|
|
74
221
|
private
|
|
75
222
|
|
|
223
|
+
# Compares two strings in constant time to prevent timing attacks.
|
|
224
|
+
#
|
|
225
|
+
# Pads or truncates +trusted+ to match +untrusted+'s byte length before
|
|
226
|
+
# comparing so that the number of loop iterations is always the same
|
|
227
|
+
# regardless of content. A separate length check at the end ensures that a
|
|
228
|
+
# length-padded match is still rejected.
|
|
229
|
+
#
|
|
230
|
+
# Uses {OpenSSL.fixed_length_secure_compare} when available (Ruby >= 2.7
|
|
231
|
+
# with openssl >= 2.2); otherwise falls back to a pure-Ruby XOR loop.
|
|
232
|
+
#
|
|
233
|
+
# @param trusted [String] the known-good value (e.g., the output of {crypt})
|
|
234
|
+
# @param untrusted [String] the value supplied by the caller
|
|
235
|
+
# @return [Boolean] +true+ only when both strings are identical
|
|
76
236
|
def secure_compare(trusted, untrusted)
|
|
77
237
|
return false unless trusted.respond_to? :to_str and trusted = trusted.to_str.b
|
|
78
238
|
return false unless untrusted.respond_to? :to_str and untrusted = untrusted.to_str.b
|