Yesterday's post was surprisingly difficult to compose. Here I was, presented with all of the code from an "open source" system that Taka's company purchased, and completely unable to compress its quality into the short bit we're all used to seeing. I decided to limit myself to just the encryption module, but even that was too saturated for a single post. Here's another function I came across from the code that I felt deserved its own post ...

Function AddHex(hex1, hex2)
  'Adds two 8-digit hexadecimals

  Dim h1, h2, h3, d1, d2, d3, i, c

  'longer one first
  If Len(hex1) > Len(hex2) Then
    h1 = hex1
    h2 = hex2
  Else
    h1 = hex2
    h2 = hex1
  End If

  'pad the second
  For i = 0 to Len(h1)
    If Len(h1) > Len(h2) Then
      h2 = "0" & h2
    End If
  Next

  'add the hex
  For i = Len(h1) To 1 Step -1
    d1 = hex2dec(Mid(h1,i,1)) 
    d2 = hex2dec(Mid(h2,i,1))
         'ED: See Quantum Computering for 
         '    hex2dec implementation
    d3 = d1 + d2

    'add carryover
    If c > 0 Then
      d3 = d3 + c
      c = 0
    End If

    'carryover
    If d3 > 15 Then
      c = 1
      d3 = d3 - 16
    End If

    h3 = dec2hex(d3) & h3
         'ED: See Quantum Computering and use your 
         '    immagination for how dec2hex works
  Next

  'final carryover
  If c > 0 Then h3 = "1" & h3

  'trim / resize
  h3 = Right(h3,16)
  For i = Len(h3) to 15
    h3 = "0" & h3
  Next

  AddHex = h3

End Function

I should note that I'm still at a complete loss for why these "hexadecimal strings" were used throughout the module ...

UPDATE: I just wanted to confirm what this was doing for those non-VBScript users. Given two "hexadecimals," this will start at rightmost character, convert each char to an int, add the ints together, convert the result back into hex, and concatenate it to the resulting string.

[Advertisement] BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!