Class: PBKDF2
- Inherits:
-
Object
- Object
- PBKDF2
- Defined in:
- lib/pbkdf2.rb
Constant Summary collapse
- VERSION =
"0.0.1"
Class Method Summary collapse
-
.calculate!(options) ⇒ Object
the bit that actually does the calculating.
-
.calculate_block(options, block_num) ⇒ Object
this is a translation of the helper function “F” defined in the spec.
- .hex_string(options = {}) {|_self| ... } ⇒ Object
- .prf(options, data) ⇒ Object
- .validate(options) ⇒ Object
Class Method Details
.calculate!(options) ⇒ Object
the bit that actually does the calculating
75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/pbkdf2.rb', line 75 def self.calculate!() # how many blocks we'll need to calculate (the last may be truncated) blocks_needed = ([:key_length].to_f / [:hash_function].size).ceil # reset value = "" # main block-calculating loop: 1.upto(blocks_needed) do |block_num| value << calculate_block(,block_num) end # truncate to desired length: value = value.slice(0,[:key_length]).unpack("H*").first value end |
.calculate_block(options, block_num) ⇒ Object
this is a translation of the helper function “F” defined in the spec
60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/pbkdf2.rb', line 60 def self.calculate_block(, block_num) # u_1: u = prf(, [:salt]+[block_num].pack("N")) ret = u # u_2 through u_c: 2.upto([:iterations]) do # calculate u_n u = prf(, u) # xor it with the previous results ret = ret^u end ret end |
.hex_string(options = {}) {|_self| ... } ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/pbkdf2.rb', line 31 def self.hex_string( = {}) yield self if block_given? # Raise errors for unset options, all are mandatory raise ArgumentError, ":password is not set" if [:password].nil? raise ArgumentError, ":salt is not set" if [:salt].nil? raise ArgumentError, ":iterations is not set" if [:iterations].nil? raise ArgumentError, ":key_length is not set" if [:key_length].nil? raise ArgumentError, ":hash_function is not set" if [:hash_function].nil? [:hash_function] = [:hash_function].new validate() [:key_length] = [:key_length]/8 calculate!() end |
.prf(options, data) ⇒ Object
55 56 57 |
# File 'lib/pbkdf2.rb', line 55 def self.prf(, data) OpenSSL::HMAC.digest([:hash_function], [:password], data) end |
.validate(options) ⇒ Object
49 50 51 52 53 |
# File 'lib/pbkdf2.rb', line 49 def self.validate() raise ArgumentError, "Key is too short (< 1)" if [:key_length] < 1 raise ArgumentError, "key is too long for hash function" if [:key_length]/8 > ((2**32 - 1) * [:hash_function].size) raise ArgumentError, "iterations can't be less than 1" if [:iterations] < 1 end |