Saturday 29 September 2018

SHA Hash Algorithm

Important property:
  • One way from input to hash value, cannot reverse
  • Different input cannot generate the same hash value

Actual SHA example:
  • Choose a word to hash, eg CRYPTO
  • Convert the word to ASCII
         CRYPTO becomes 67 82 89 80 84 79
  • Convert from ASCII to binary
         01000011-01010010-01011001-01010000-01010100-01001111 
        (it becomes a 48 bit message)
  • Join and add 1 at the end
         0100001101010010010110010101000001010100010011111
  • Add zeros to make message equal to 448 mod 512, a 48 bit message with the added one will need to have 399 zeros added to the end
  • Add original message length to the 64 bit field (which is the left over field after the 448 modular arithmetic), and let the message become 16 sections of 32 bits
  • 01000011010100100101100101010000
    01010100010011111000000000000000
    00000000000000000000000000000000
    00000000000000000000000000000000
    00000000000000000000000000000000
    00000000000000000000000000000000
    00000000000000000000000000000000
    00000000000000000000000000000000
    00000000000000000000000000000000
    00000000000000000000000000000000
    00000000000000000000000000000000
    00000000000000000000000000000000
    00000000000000000000000000000000
    00000000000000000000000000000000
    00000000000000000000000000000000
    00000000000000000000000000110000
  • Transform the 16 x 32 message into 80 words using step loop function, firstly, do ((14 XOR 9) XOR 3) XOR 1), we get
         01000011010100100101100101010000
  • Rotate left, we get
         10000110101001001011001010100000
  • Process is repeated until there are 80 words (one word = 32 bits)
  • The 1st, 3rd, 9th 14th words are chosen from the algorithm:
  • for i from 16 to 79
          w[i] = (w[i-3] xor w[i-8] xor w[i-14] xor w[i-16]) leftrotate 1

  • Run a set of operations on the 80 words in specific order using the five variables

  • H0 - 01100111010001010010001100000001
    H1 - 11101111110011011010101110001001
    H2 - 10011000101110101101110011111110
    H3 - 00010000001100100101010001110110
    H4 – 11000011110100101110000111110000
    • The Operations are combination of AND, OR and NOT operators
    • The five variables are from first 32 bits of the fractional part of the square roots of the first 5 prime numbers
    • The result : get five variables
    H0 – 01000100101010010111000100110011
    H1- 01010000111001010011100001011000
    H2-11110000010110000100011000111101
    H3-01001011111101111111000111100101
    H4-01000010110110011100101001001011

    • Convert the five variables to hex
    H0- 44a97133
    H1- 50e53858
    H2- f058463d
    H3 - 4bf7f1e5
    H4 - 42d9ca4b
    • Join the variables together, get the hash
    44a9713350e53858f058463d4bf7f1e542d9ca4b