Class: Krypt::HMAC

Inherits:
Object
  • Object
show all
Includes:
Krypt::Helper::XOR
Defined in:
lib/krypt/hmac.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Krypt::Helper::XOR

#xor, #xor!

Constructor Details

#initialize(digest, key) ⇒ HMAC

Returns a new instance of HMAC.



5
6
7
8
9
10
11
# File 'lib/krypt/hmac.rb', line 5

def initialize(digest, key)
  @digest = digest
  @key = process_key(key)

  # hash ipad
  hash_pad(0x36)
end

Class Method Details

.digest(md, key, data) ⇒ Object



32
33
34
35
36
# File 'lib/krypt/hmac.rb', line 32

def digest(md, key, data)
  hmac = self.new(md, key)
  hmac << data
  hmac.digest
end

.hexdigest(md, key, data) ⇒ Object



38
39
40
# File 'lib/krypt/hmac.rb', line 38

def hexdigest(md, key, data)
  Krypt::Hex.encode(digest(md, key, data))
end

Instance Method Details

#digestObject



18
19
20
21
22
23
24
# File 'lib/krypt/hmac.rb', line 18

def digest
  inner_digest = @digest.digest
  # hash opad
  hash_pad(0x5c)
  @digest << inner_digest
  @digest.digest
end

#hexdigestObject



26
27
28
# File 'lib/krypt/hmac.rb', line 26

def hexdigest
  Krypt::Hex.encode(digest)
end

#update(data) ⇒ Object Also known as: <<



13
14
15
# File 'lib/krypt/hmac.rb', line 13

def update(data)
  @digest << data
end