write c++ code Example: 1 key length < block size Input data:Message: “Hello” → x48656C6C 6FSecret Key: “Key” → x4B6579 Constants for our case hash algorithm (SHA1) with block size 64 bytes:ipad (in HEX): x36363636 36363636 36363636 36363636 36363636 36363636 36363636 36363636 36363636 36363636 36363636 36363636 36363636 36363636 36363636 36363636 (x36 repeated 64 time)opad (in HEX): x5C5C5C5C 5C5C5C5C 5C5C5C5C 5C5C5C5C 5C5C5C5C 5C5C5C5C 5C5C5C5C 5C5C5C5C 5C5C5C5C 5C5C5C5C 5C5C5C5C 5C5C5C5C 5C5C5C5C 5C5C5C5C 5C5C5C5C 5C5C5C5C (x5C repeated 64 time) Steps of creating HMAC (you could use the calculator to verify the result): If the key length < block size, we need to add 0s until the key length equals the block size. In our case, the key length is 3 bytes, and the block size is 64 bytes. So new key is K0 = 4B657900 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 We need to perform the operation K0 XOR ipadHow to make XOR operation you can see hereor use the calculator.K0 XOR ipad = 7D534F36 36363636 36363636 36363636 36363636 36363636 36363636 36363636 36363636 36363636 36363636 36363636 36363636 36363636 36363636 36363636 Now, we append the message to K0 XOR ipad(K0 XOR ipad) || message = 7D534F36 36363636 36363636 36363636 36363636 36363636 36363636 36363636 36363636 36363636 36363636 36363636 36363636 36363636 36363636 36363636 48656C6C 6F We put the result of the previous operation into the SHA-1 hash.Please note that you need to specify to the calculator that your data is in HEX (use this calculator). Also, you should remove all spaces (remember spaces are added here for readability but should not be included when hashing). In other words, you should input into the hash function:7D534F3636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363648656C6C6Fsha1(K0 XOR ipad || message) = 8e32567d57353a91515458d65b4cc7802450172c We need to perform the operation K0 XOR opad.K0 XOR opad = 1739255C 5C5C5C5C 5C5C5C5C 5C5C5C5C 5C5C5C5C 5C5C5C5C 5C5C5C5C 5C5C5C5C 5C5C5C5C 5C5C5C5C 5C5C5C5C 5C5C5C5C 5C5C5C5C 5C5C5C5C 5C5C5C5C 5C5C5C5C Append to the result of the previous operation the wich hash obtained in step 4.(K0 XOR opad) || (sha1(K0 XOR ipad || message)) =1739255C 5C5C5C5C 5C5C5C5C 5C5C5C5C 5C5C5C5C 5C5C5C5C 5C5C5C5C 5C5C5C5C 5C5C5C5C 5C5C5C5C 5C5C5C5C 5C5C5C5C 5C5C5C5C 5C5C5C5C 5C5C5C5C 5C5C5C5C 8E32567D 57353A91 515458D6 5B4CC780 2450172C Put the result of step 6 into the SHA-1 hash.HMAC = 173ac40fb6ac57cc7524594c523bea1bdd54836a

icon
Related questions
Question

write c++ code

Example: 1 key length < block size

Input data:
Message: “Hello” → x48656C6C 6F
Secret Key: “Key” → x4B6579

Constants for our case hash algorithm (SHA1) with block size 64 bytes:
ipad (in HEX): x36363636 36363636 36363636 36363636 36363636 36363636 36363636 36363636 36363636 36363636 36363636 36363636 36363636 36363636 36363636 36363636 (x36 repeated 64 time)
opad (in HEX): x5C5C5C5C 5C5C5C5C 5C5C5C5C 5C5C5C5C 5C5C5C5C 5C5C5C5C 5C5C5C5C 5C5C5C5C 5C5C5C5C 5C5C5C5C 5C5C5C5C 5C5C5C5C 5C5C5C5C 5C5C5C5C 5C5C5C5C 5C5C5C5C (x5C repeated 64 time)

Steps of creating HMAC (you could use the calculator to verify the result):

  1. If the key length < block size, we need to add 0s until the key length equals the block size. In our case, the key length is 3 bytes, and the block size is 64 bytes. So new key is K0 = 4B657900 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000
  2. We need to perform the operation K0 XOR ipad
    How to make XOR operation you can see hereor use the calculator.
    K0 XOR ipad = 7D534F36 36363636 36363636 36363636 36363636 36363636 36363636 36363636 36363636 36363636 36363636 36363636 36363636 36363636 36363636 36363636
  3. Now, we append the message to K0 XOR ipad
    (K0 XOR ipad) || message = 7D534F36 36363636 36363636 36363636 36363636 36363636 36363636 36363636 36363636 36363636 36363636 36363636 36363636 36363636 36363636 36363636 48656C6C 6F
  4. We put the result of the previous operation into the SHA-1 hash.
    Please note that you need to specify to the calculator that your data is in HEX (use this calculator). Also, you should remove all spaces (remember spaces are added here for readability but should not be included when hashing). In other words, you should input into the hash function:
    7D534F3636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363636363648656C6C6F
    sha1(K0 XOR ipad || message) = 8e32567d57353a91515458d65b4cc7802450172c
  5. We need to perform the operation K0 XOR opad.
    K0 XOR opad = 1739255C 5C5C5C5C 5C5C5C5C 5C5C5C5C 5C5C5C5C 5C5C5C5C 5C5C5C5C 5C5C5C5C 5C5C5C5C 5C5C5C5C 5C5C5C5C 5C5C5C5C 5C5C5C5C 5C5C5C5C 5C5C5C5C 5C5C5C5C
  6. Append to the result of the previous operation the wich hash obtained in step 4.
    (K0 XOR opad) || (sha1(K0 XOR ipad || message)) =
    1739255C 5C5C5C5C 5C5C5C5C 5C5C5C5C 5C5C5C5C 5C5C5C5C 5C5C5C5C 5C5C5C5C 5C5C5C5C 5C5C5C5C 5C5C5C5C 5C5C5C5C 5C5C5C5C 5C5C5C5C 5C5C5C5C 5C5C5C5C 8E32567D 57353A91 515458D6 5B4CC780 2450172C
  7. Put the result of step 6 into the SHA-1 hash.
    HMAC = 173ac40fb6ac57cc7524594c523bea1bdd54836a
Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer