Class: Krypt::HMAC

Inherits:
Object
  • Object
show all
Defined in:
lib/krypt/hmac.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(digest, key) ⇒ HMAC

Returns a new instance of HMAC.



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

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

  # hash ipad
  hash_pad(0x36)
end

Class Method Details

.digest(md, key, data) ⇒ Object



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

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

.hexdigest(md, key, data) ⇒ Object



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

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

Instance Method Details

#digestObject



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

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

#hexdigestObject



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

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

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



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

def update(data)
  @digest << data
end