Skip to content

Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →

class method

OpenSSL::KDF.scrypt

(::String pass, salt: ::String, N: ::Integer, r: ::Integer, p: ::Integer, length: ::Integer) -> ::String
KDF.scrypt(pass, salt:, N:, r:, p:, length:) → aString

Derives a key from pass using given parameters with the scrypt password-based key derivation function. The result can be used for password storage.

scrypt is designed to be memory-hard and more secure against brute-force attacks using custom hardwares than alternative KDFs such as PBKDF2 or bcrypt.

The keyword arguments N, r and p can be used to tune scrypt. RFC 7914 (published on 2016-08, www.rfc-editor.org/rfc/rfc7914#section-2) states that using values r=8 and p=1 appears to yield good results.

See RFC 7914 (www.rfc-editor.org/rfc/rfc7914) for more information.

NOTE: This method cannot be interrupted by Timeout.timeout from the “timeout” library. Do not take parameters from untrusted sources without enforcing reasonable limits.

Parameters

pass

Passphrase.

salt

Salt.

N

CPU/memory cost parameter. This must be a power of 2.

r

Block size parameter.

p

Parallelization parameter.

length

Length in octets of the derived key.

Example

Example
pass = "password"
salt = SecureRandom.random_bytes(16)
dk = OpenSSL::KDF.scrypt(pass, salt: salt, N: 2**14, r: 8, p: 1, length: 32)
p dk #=> "\xDA\xE4\xE2...\x7F\xA1\x01T"
See also .pbkdf2_hmac .hkdf also in 4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0