Class: JWA::Support::PBKDF2
- Inherits:
-
Object
- Object
- JWA::Support::PBKDF2
- Defined in:
- lib/jwa/support/pbkdf2.rb
Instance Method Summary collapse
- #calculate_block(block_num, salt, password, iterations) ⇒ Object
-
#initialize(hash) ⇒ PBKDF2
constructor
A new instance of PBKDF2.
- #run(password, salt, iterations, key_length = nil) ⇒ Object
- #run_hex(password, salt, iterations, key_length = nil) ⇒ Object
Constructor Details
#initialize(hash) ⇒ PBKDF2
Returns a new instance of PBKDF2.
4 5 6 |
# File 'lib/jwa/support/pbkdf2.rb', line 4 def initialize(hash) @hash = hash end |
Instance Method Details
#calculate_block(block_num, salt, password, iterations) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/jwa/support/pbkdf2.rb', line 24 def calculate_block(block_num, salt, password, iterations) u = prf(salt + [block_num].pack('N'), password) ret = u 2.upto(iterations) do u = prf(u, password) ret = xor(ret, u) end ret end |
#run(password, salt, iterations, key_length = nil) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/jwa/support/pbkdf2.rb', line 12 def run(password, salt, iterations, key_length = nil) key_length ||= @hash.size blocks_needed = (key_length / @hash.size.to_f).ceil v = 1.upto(blocks_needed).map do |block_num| calculate_block(block_num, salt, password, iterations) end.join v[0...key_length] end |
#run_hex(password, salt, iterations, key_length = nil) ⇒ Object
8 9 10 |
# File 'lib/jwa/support/pbkdf2.rb', line 8 def run_hex(password, salt, iterations, key_length = nil) run(password, salt, iterations, key_length).unpack('H*').first end |