Class: Decidim::Tokenizer
- Inherits:
-
Object
- Object
- Decidim::Tokenizer
- Defined in:
- decidim-core/app/services/decidim/tokenizer.rb
Overview
This class is used to generate secure tokens
Instance Attribute Summary collapse
-
#length ⇒ Object
readonly
Returns the value of attribute length.
-
#salt ⇒ Object
readonly
Returns the value of attribute salt.
Class Method Summary collapse
Instance Method Summary collapse
-
#digest(string) ⇒ Object
returns a securely generated string of bytes.
- #hex_digest(string) ⇒ Object
-
#initialize(salt: nil, length: 32) ⇒ Tokenizer
constructor
Initializes the Tokenizer.
- #int_digest(string) ⇒ Object
Constructor Details
#initialize(salt: nil, length: 32) ⇒ Tokenizer
Initializes the Tokenizer.
salt - The salt fr the encryption (it should be at leas 30 chars long) length - How long the key generated should be (in bytes)
14 15 16 17 |
# File 'decidim-core/app/services/decidim/tokenizer.rb', line 14 def initialize(salt: nil, length: 32) @salt = salt.presence || Tokenizer.random_salt @length = length end |
Instance Attribute Details
#length ⇒ Object (readonly)
Returns the value of attribute length.
23 24 25 |
# File 'decidim-core/app/services/decidim/tokenizer.rb', line 23 def length @length end |
#salt ⇒ Object (readonly)
Returns the value of attribute salt.
23 24 25 |
# File 'decidim-core/app/services/decidim/tokenizer.rb', line 23 def salt @salt end |
Class Method Details
.random_salt ⇒ Object
19 20 21 |
# File 'decidim-core/app/services/decidim/tokenizer.rb', line 19 def self.random_salt SecureRandom.hex(32) end |
Instance Method Details
#digest(string) ⇒ Object
returns a securely generated string of bytes
26 27 28 |
# File 'decidim-core/app/services/decidim/tokenizer.rb', line 26 def digest(string) OpenSSL::PKCS5.pbkdf2_hmac(string.to_s, salt, 20_000, length, "sha256") end |
#hex_digest(string) ⇒ Object
34 35 36 |
# File 'decidim-core/app/services/decidim/tokenizer.rb', line 34 def hex_digest(string) digest(string.to_s).bytes.map { |c| c.ord.to_s(16) }.join end |
#int_digest(string) ⇒ Object
30 31 32 |
# File 'decidim-core/app/services/decidim/tokenizer.rb', line 30 def int_digest(string) digest(string.to_s).bytes.inject { |a, b| (a << 8) + b } end |