github.com/iDigitalFlame/xmt@v0.5.4/device/local/windows_crypt.go (about)

     1  //go:build windows && crypt
     2  // +build windows,crypt
     3  
     4  // Copyright (C) 2020 - 2023 iDigitalFlame
     5  //
     6  // This program is free software: you can redistribute it and/or modify
     7  // it under the terms of the GNU General Public License as published by
     8  // the Free Software Foundation, either version 3 of the License, or
     9  // any later version.
    10  //
    11  // This program is distributed in the hope that it will be useful,
    12  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  // GNU General Public License for more details.
    15  //
    16  // You should have received a copy of the GNU General Public License
    17  // along with this program.  If not, see <https://www.gnu.org/licenses/>.
    18  //
    19  
    20  package local
    21  
    22  import (
    23  	"github.com/iDigitalFlame/xmt/device/winapi"
    24  	"github.com/iDigitalFlame/xmt/device/winapi/registry"
    25  	"github.com/iDigitalFlame/xmt/util"
    26  	"github.com/iDigitalFlame/xmt/util/crypt"
    27  )
    28  
    29  func sysID() []byte {
    30  	if s, err := winapi.GetSystemSID(); err == nil {
    31  		return []byte(s.String())
    32  	}
    33  	// 0x101 - KEY_WOW64_64KEY | KEY_QUERY_VALUE
    34  	k, err := registry.Open(registry.KeyLocalMachine, crypt.Get(94), 0x101) // Software\Microsoft\Cryptography
    35  	if err != nil {
    36  		return nil
    37  	}
    38  	v, _, err := k.String(crypt.Get(95)) // MachineGuid
    39  	if k.Close(); err == nil {
    40  		return []byte(v)
    41  	}
    42  	return nil
    43  }
    44  func version() string {
    45  	var n string
    46  	// 0x101 - KEY_WOW64_64KEY | KEY_QUERY_VALUE
    47  	if k, err := registry.Open(registry.KeyLocalMachine, crypt.Get(96), 0x101); err == nil { // Software\Microsoft\Windows NT\CurrentVersion
    48  		n, _, _ = k.String(crypt.Get(97)) // ProductName
    49  		k.Close()
    50  	}
    51  	var (
    52  		j, y, h = winapi.GetVersionNumbers()
    53  		b       = util.Uitoa(uint64(h))
    54  		v       string
    55  	)
    56  	if y > 0 {
    57  		v = util.Uitoa(uint64(j)) + "." + util.Uitoa(uint64(y))
    58  	} else {
    59  		v = util.Uitoa(uint64(j))
    60  	}
    61  	switch {
    62  	case len(n) == 0 && len(b) == 0 && len(v) == 0:
    63  		return crypt.Get(98) // Windows
    64  	case len(n) == 0 && len(b) > 0 && len(v) > 0:
    65  		return crypt.Get(98) + " (" + v + ", " + b + ")" // Windows
    66  	case len(n) == 0 && len(b) == 0 && len(v) > 0:
    67  		return crypt.Get(98) + " (" + v + ")" // Windows
    68  	case len(n) == 0 && len(b) > 0 && len(v) == 0:
    69  		return crypt.Get(98) + " (" + b + ")" // Windows
    70  	case len(n) > 0 && len(b) > 0 && len(v) > 0:
    71  		return n + " (" + v + ", " + b + ")"
    72  	case len(n) > 0 && len(b) == 0 && len(v) > 0:
    73  		return n + " (" + v + ")"
    74  	case len(n) > 0 && len(b) > 0 && len(v) == 0:
    75  		return n + " (" + b + ")"
    76  	}
    77  	return crypt.Get(98) // Windows
    78  }