|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module OpenSSL |
| 4 | + module KDF |
| 5 | + if respond_to?(:derive) |
| 6 | + # Argon2id, a variant of Argon2, is a password hashing function |
| 7 | + # described in {RFC 9106}[https://www.rfc-editor.org/rfc/rfc9106]. |
| 8 | + # |
| 9 | + # This methods requires \OpenSSL 3.2 or later. |
| 10 | + # |
| 11 | + # === Parameters |
| 12 | + # pass:: Passowrd to be hashed. Message string +P+ in RFC 9106. |
| 13 | + # salt:: Salt. Nonce +S+ in RFC 9106. |
| 14 | + # lanes:: Degree of parallelism. +p+ in RFC 9106. |
| 15 | + # length:: Desired output length in bytes. Tag length +T+ in RFC 9106. |
| 16 | + # memcost:: Memory size in the number of kibibytes. +m+ in RFC 9106. |
| 17 | + # iter:: Number of passes. +t+ in RFC 9106. |
| 18 | + # secret:: Secret value. Optional. +K+ in RFC 9106. |
| 19 | + # ad:: Associated data. Optional. +X+ in RFC 9106. |
| 20 | + # |
| 21 | + # === Example |
| 22 | + # password = "\x01" * 32 |
| 23 | + # salt = "\x02" * 16 |
| 24 | + # secret = "\x03" * 8 |
| 25 | + # ad = "\x04" * 12 |
| 26 | + # ret = OpenSSL::KDF.argon2id( |
| 27 | + # password, salt: salt, lanes: 4, length: 32, |
| 28 | + # memcost: 32, iter: 3, secret: secret, ad: ad, |
| 29 | + # ) |
| 30 | + # p ret.unpack1("H*") |
| 31 | + # #=> "0d640df58d78766c08c037a34a8b53c9d01ef0452d75b65eb52520e96b01e659" |
| 32 | + def self.argon2id(pass, salt:, lanes:, length:, memcost:, iter:, |
| 33 | + secret: "", ad: "") |
| 34 | + params = { |
| 35 | + pass: pass, salt: salt, lanes: lanes, memcost: memcost, iter: iter, |
| 36 | + secret: secret, ad: ad, |
| 37 | + } |
| 38 | + derive("ARGON2ID", length, params) |
| 39 | + end |
| 40 | + end |
| 41 | + end |
| 42 | +end |
0 commit comments