예제 #1
0
        private static int getMachineCode()
        {
            //This code will generate a 5 digits long key, finger print, of the system
            //where this method is being executed. However, that might be changed in the
            //hash function "GetStableHash", by changing the amount of zeroes in
            //MUST_BE_LESS_OR_EQUAL_TO to the one you want to have. Ex 1000 will return
            //3 digits long hash.
            methods m = new methods();

            //string collectedInfo = System.Environment.MachineName;
            string collectedInfo = string.Empty;

            var x = new List <string>();

            //取得CPU ID;//
            ManagementObjectSearcher SearchCPU = new ManagementObjectSearcher(@"SELECT * FROM Win32_Processor");

            foreach (ManagementObject Win32ProcessObj in SearchCPU.Get())
            {
                // 取得CPU 序號
                if (Win32ProcessObj["ProcessorId"] != null)
                {
                    x.Add(Win32ProcessObj["ProcessorId"].ToString());
                }
            }
            x.Sort();
            collectedInfo += string.Join("", x);
            x.Clear();

            //取得Bios UUID;//
            ManagementObjectSearcher SearchSystemProduct = new ManagementObjectSearcher(@"root\CIMV2", "SELECT * FROM Win32_ComputerSystemProduct");

            foreach (ManagementObject SystemObj in SearchSystemProduct.Get())
            {
                if (SystemObj["UUID"] != null)
                {
                    x.Add(SystemObj["UUID"].ToString().Replace("-", ""));
                }
            }
            x.Sort();
            collectedInfo += string.Join("", x);
            x.Clear();

            //取得網卡MAC 碼;//
            //var Name = new List<string>();
            //foreach (NetworkInterface ni in NetworkInterface.GetAllNetworkInterfaces())
            //{
            //    // probably need to do some filtering on ni.NetworkInterfaceType here
            //    //collectedInfo += ni.GetPhysicalAddress();
            //    x.Add(ni.GetPhysicalAddress().ToString());
            //    Name.Add(ni.Name);
            //}
            //x.Sort();
            //collectedInfo += x[x.Count - 1];
            //x.Clear();

            ManagementObjectSearcher SearchPhysicalMedia = new ManagementObjectSearcher("SELECT * FROM Win32_PhysicalMedia");

            foreach (ManagementObject wmi_HD in SearchPhysicalMedia.Get())
            {
                // get the hardware serial no.
                if (wmi_HD["SerialNumber"] == null &&
                    wmi_HD["Removable"] == null &&
                    wmi_HD["Replaceable"] == null)
                {
                    // Don't add
                }
                else
                {
                    //serialNo = wmi_HD["SerialNumber"].ToString();
                    //hotSwap = Convert.ToBoolean(wmi_HD["HotSwappable"]);
                    //Console.WriteLine(hotSwap);
                    x.Add(wmi_HD["SerialNumber"].ToString());
                }
            }
            x.Sort();
            collectedInfo += string.Join("", x);

            //表示都沒有取到設備的識別序號,直接給定一個值;//
            if (collectedInfo.Length <= 0)
            {
                collectedInfo = "productmachine";
            }
            int LessThan = Convert.ToInt32(Math.Pow(10, BaseConfiguration.MachineCodeMaxDigit));

            return(m.getEightByteHash(collectedInfo, LessThan));
        }