mrpeardotnet / WinProdKeyFinder

Windows Product Key Finder written in C#.
http://www.mrpear.net
MIT License
133 stars 42 forks source link

same algorithm in two favor #12

Closed hawajrambo closed 1 year ago

hawajrambo commented 2 years ago

Hello,

The DecodeProductKey and DecodeProductKeyWin8AndUp almost does the same. It can be merged into one function:

    public static string DecodeProductKeyMerged(byte[] digitalProductId, bool isWin8Up)
    {
        var key = String.Empty;
        const int keyOffset = 52;

        // by the way, this has no sense bacause of &1 &2:
        // var isWin8 = (byte)((digitalProductId[66] / 6) & 1);
        // digitalProductId[66] = (byte)((digitalProductId[66] & 0xf7) | (isWin8 & 2) * 4);
        if (isWin8Up) digitalProductId[66] = (byte)((digitalProductId[66] & 0xf7);

        const string digits = "BCDFGHJKMPQRTVWXY2346789";
        var last = 0;
        for (var i = 24; i >= 0; i--)
        {
            var current = 0;
            for (var j = 14; j >= 0; j--)
            {
                current = current*256;
                current = digitalProductId[j + keyOffset] + current;
                digitalProductId[j + keyOffset] = (byte)(current/24);
                current = current%24;
                last = current;
            }
            key = digits[current] + key;
        }

        if (isWin8Up) {
          var keypart1 = key.Substring(1, last);
          var keypart2 = key.Substring(last + 1, key.Length - (last + 1));
          key = keypart1 + "N" + keypart2;
       }

        for (var i = 5; i < key.Length; i += 6)
        {
            key = key.Insert(i, "-");
        }

        return key;
    }
}

}

mrpeardotnet commented 1 year ago

It is just about the part of a code, not functionality. No need to fix.