Debate general Test your coding skills

  • sábado, 28 de abril de 2012 16:34
     
      Tiene código

    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

Todas las respuestas

  • sábado, 28 de abril de 2012 17:52
     
      Tiene código
        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




    • Editado cicatrixx sábado, 28 de abril de 2012 18:01 Added GetHash2
    • Editado cicatrixx domingo, 29 de abril de 2012 9:15
    • Editado cicatrixx domingo, 29 de abril de 2012 9:15 Changed to unsigned type
    •  
  • domingo, 29 de abril de 2012 0:09
     
     

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

  • domingo, 29 de abril de 2012 9:13
     
     
    You can change the returned datatype to ULong (modified my post)
    • Editado cicatrixx domingo, 29 de abril de 2012 9:16
    •  
  • martes, 01 de mayo de 2012 16:42
     
      Tiene código

    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

  • martes, 01 de mayo de 2012 22:03
     
     

    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

  • martes, 01 de mayo de 2012 22:41
     
      Tiene código

    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.

  • miércoles, 02 de mayo de 2012 0:20
     
     

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

    Your approach is certainly unique.

  • miércoles, 02 de mayo de 2012 0:30
     
     

    That's a new concept to me - thanks

  • lunes, 21 de mayo de 2012 15:56
     
     
    very smooth John