title: How to add another parameter for message in hmac?
body: <p>i want to write a hmac (hash-based message authentication code) in python. So far i managed to write the basic hmac but i want to add another parameter in the message. For example, message=(mac_address || index_value). Can somebody show me how to do it? And how can i save the output in another list (e.g. digest_hmac_list)?</p>
  <pre><code>from hashlib import shake_256
  from zlib import crc32, adler32
  
  
  class HMAC:
  
  
      def __init__(self, key, message, hash_h=shake_256):
  
          &quot;&quot;&quot; key and message must be byte object &quot;&quot;&quot;
          self.i_key_pad = bytearray()
          self.o_key_pad = bytearray()
          self.key = key
          self.message = message
          self.blocksize = 64
          self.hash_h = hash_h
          self.init_flag = False
  
  
      def init_pads(self):
  
          &quot;&quot;&quot; creating inner padding and outer padding &quot;&quot;&quot;
          for i in range(self.blocksize):
              self.i_key_pad.append(0x36 ^ self.key[i])
              self.o_key_pad.append(0x5c ^ self.key[i])
  
  
      def init_key(self):
  
          &quot;&quot;&quot; key regeneration &quot;&quot;&quot;
          if len(self.key) &gt; self.blocksize:
              self.key = bytearray(shake_256(key).digest())
          elif len(self.key) &lt; self.blocksize:
              i = len(self.key)
              while i &lt; self.blocksize:
                  self.key += b&quot;\x00&quot;
                  i += 1
  
  
      def digest(self):
  
          if self.hash_h == adler32 or self.hash_h == crc32:
              return self.hash_h(bytes(self.o_key_pad)+str(self.hash_h(bytes(self.i_key_pad)+self.message)).encode())
          &quot;&quot;&quot; returns a digest, byte object. &quot;&quot;&quot;
          &quot;&quot;&quot; check if init_flag is set &quot;&quot;&quot;
          if self.init_flag == False:
  
              self.init_key()
              self.init_pads()
  
              &quot;&quot;&quot; hold init_flag for good. &quot;&quot;&quot;
              self.init_flag = True
  
          return self.hash_h(bytes(self.o_key_pad)+self.hash_h(bytes(self.i_key_pad)+self.message).digest()).digest()
  
  
      def hexdigest(self):
  
          if self.hash_h == adler32 or self.hash_h == crc32:
              return hex(self.hash_h(bytes(self.o_key_pad)+str(self.hash_h(bytes(self.i_key_pad)+self.message)).encode()))[2:]
  
          &quot;&quot;&quot; returns a digest in hexadecimal. &quot;&quot;&quot;
          &quot;&quot;&quot; check if init_flag is set &quot;&quot;&quot;
          if self.init_flag == False:
  
              &quot;&quot;&quot; init key and padding. &quot;&quot;&quot;
              self.init_key()
              self.init_pads()
  
              &quot;&quot;&quot; set init_flag for good. &quot;&quot;&quot;
              self.init_flag = True
  </code></pre>