Discussion Test your coding skills

  • Saturday, April 28, 2012 4:34 PM
     
      Has Code

    Here is a tiny Perl script that takes a MAC Address (xx-xx-xx-xx-xx-xx) as its only parameter and returns a hash.

    I have converted it to a console VB NET app with some difficulty and I'd like to see some other approaches to the conversion.

    Bear in mind that my skills in Perl are very very low. I had to look up most of the operators.

    #!/usr/bin/perl
    if (! defined $ARGV[0]) {
    print "+========================================== \n";
    print "+ MAC Address Hash Generator \n";
    print "+ April 23 2012 \n";
    print "+ Usage:\n$0 macaddress \n";
    print "+========================================== \n";
    exit; }
    $a = $ARGV[0];
    $a =~  s/[^A-F0-9]+//simg;
    @b = reverse split /(\S{2})/,$a;
    $c = join "", @b;
    $c .= "0000";
    $d = hex($c) % 999999929;
    print "$d\n";

    Examples:

    Input = 00-0A-DC-00-00-00
    Hash = 60644375
    Input = FE-0A-BF-00-9D-57
    Hash = 403925255

    Option Strict On please....

    If there is any significant response, I'll post my code in a few days / a week

All Replies

  • Saturday, April 28, 2012 5:52 PM
     
      Has Code
        Function GetHash(ByVal input As String) As ULong
            Dim tokens As String() = (("00-00-" & input).Split(New Char() {"-"c})).Reverse().ToArray()
            Dim number As ULong = ULong.Parse(String.Join("", tokens), Globalization.NumberStyles.HexNumber)
            Return (number Mod 999999929)
        End Function

    Sub Main()
      Console.WriteLine(GetHash("00-0A-DC-00-00-00")) 
      Console.WriteLine(GetHash("FE-0A-BF-00-9D-57"))
      Console.ReadLine()
    End Sub
    
    ' Ouput:
    60644375
    403925255

    Another one:

    Function GetHash2(ByVal input As String) As ULong
      Return (BitConverter.ToUInt64((
           From token As String In (("00-00-" & input).Split(New Char() {"-"c}))
           Select Byte.Parse(token, Globalization.NumberStyles.HexNumber)).ToArray(), 0)) _
              Mod 999999929
    End Function




    • Edited by cicatrixx Saturday, April 28, 2012 6:01 PM Added GetHash2
    • Edited by cicatrixx Sunday, April 29, 2012 9:15 AM
    • Edited by cicatrixx Sunday, April 29, 2012 9:15 AM Changed to unsigned type
    •  
  • Sunday, April 29, 2012 12:09 AM
     
     

    With a MAC Address like "03-41-BF-35-49-9F" where the last byte is > 0x80, the hash is negative.

  • Sunday, April 29, 2012 9:13 AM
     
     
    You can change the returned datatype to ULong (modified my post)
    • Edited by cicatrixx Sunday, April 29, 2012 9:16 AM
    •  
  • Tuesday, May 01, 2012 4:42 PM
     
      Has Code

    Thanks for replying, too bad nobody else could - oh well........

            Dim StrRevMAC As String = String.Join("", (args(1).Split("-"c).Reverse().ToArray())) & "0000"
            Dim PW As ULong = 0
            Dim ModVal As ULong = 999999929
            If ULong.TryParse(StrRevMAC, NumberStyles.AllowHexSpecifier, Nothing, PW) = True Then
                Console.WriteLine("Password is: " & (PW Mod ModVal).ToString)
            Else
                Console.WriteLine("Password could not be derived from specified MAC Address")
            End If
            Console.WriteLine("Press any key to exit the application")
            Console.ReadKey()

    Thats the code I used.......

    Cheers - Devon

  • Tuesday, May 01, 2012 10:03 PM
     
     

    Devon,

    I not sure what you were expecting from the community.

    If you are just interested in seeing alternate ways to accomplish this task, here is my entry for the "let's confuse them category" ;)

    Private Function MACandCheeseHash(ByVal MAC As String) As UInt64
       'Validate MAC format
          MAC = MAC.ToUpperInvariant.Trim 'Prep it
          Dim validchars() As Char = New Char() {"0"c, "1"c, "2"c, "3"c, "4"c, "5"c, "6"c, "7"c, "8"c, "9"c, "A"c, "B"c, "C"c, "D"c, "E"c, "F"c}

          Dim Invalid As Boolean = MAC.Length <> 17 _
                                   OrElse MAC.Replace("-", "").AsEnumerable.Union(validchars).ToArray.Count <> validchars.Count _
                                   OrElse (MAC(2) & MAC(5) & MAC(8) & MAC(11) & MAC(14) <> "-----")
          If Invalid Then
             Throw New ArgumentException("Invalid MAC")
          End If

       'Value to get remainder from
       Dim sum As UInt64 = (Convert.ToUInt64(MAC.Substring(15, 2), 16) << 56) + _
                           (Convert.ToUInt64(MAC.Substring(12, 2), 16) << 48) + _
                           (Convert.ToUInt64(MAC.Substring(9, 2), 16) << 40) + _
                           (Convert.ToUInt64(MAC.Substring(6, 2), 16) << 32) + _
                           (Convert.ToUInt64(MAC.Substring(3, 2), 16) << 24) + _
                           (Convert.ToUInt64(MAC.Substring(0, 2), 16) << 16)

       Const Divisor As Decimal = 999999929D
       Return CType(Decimal.Remainder(CType(sum, Decimal), Divisor), UInt64)

    End Function

  • Tuesday, May 01, 2012 10:41 PM
     
      Has Code

    Hi Devon_Nullman,

    You could use your code in a Function and even make it an extension method like this.>>

    Option Strict On
    Option Explicit On
    Option Infer Off
    
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim hashValue As ULong
            hashValue = "00-0A-DC-00-00-00".GetHash
            MessageBox.Show(hashValue.ToString)
    
            hashValue = "FE-0A-BF-00-9D-57".GetHash
            MessageBox.Show(hashValue.ToString)
    
        End Sub
    
    End Class
    
    Public Module MyExtensions
    
        <System.Runtime.CompilerServices.Extension()> _
        Public Function GetHash(ByVal input As String) As ULong
            Dim strRevMAC As String = String.Join("", (input.Split("-"c).Reverse().ToArray())) & "0000"
            Dim pW As ULong = 0
            If ULong.TryParse(strRevMAC, System.Globalization.NumberStyles.AllowHexSpecifier, Nothing, pW) = True Then
                Return Convert.ToUInt64(pW Mod 999999929)
            Else
                Throw New Exception("Password could not be derived from specified MAC Address.")
            End If
        End Function
    
    End Module




    Regards,

    profile for John Anthony Oliver at Stack Overflow, Q&A for professional and enthusiast programmers

    Click this link to see the NEW way of how to insert a picture into a forum post.

    Installing VB6 on Windows 7

    App Hub for Windows Phone & XBOX 360 developers.

  • Wednesday, May 02, 2012 12:20 AM
     
     

    What I was hoping for is exactly what I see here - several different ways to solve the same problem.

    Your approach is certainly unique.

  • Wednesday, May 02, 2012 12:30 AM
     
     

    That's a new concept to me - thanks

  • Monday, May 21, 2012 3:56 PM
     
     
    very smooth John