Skip to content

Instantly share code, notes, and snippets.

@salman0ansari
Created March 12, 2023 13:08
Show Gist options
  • Save salman0ansari/4dccb1085f633a3428b185a3274bfe80 to your computer and use it in GitHub Desktop.
Save salman0ansari/4dccb1085f633a3428b185a3274bfe80 to your computer and use it in GitHub Desktop.
hwid generator
package main
import (
"crypto/rsa"
"crypto/sha256"
"crypto/x509"
"encoding/base64"
"encoding/pem"
"fmt"
"io/ioutil"
"math/big"
"github.com/denisbrodbeck/machineid"
)
const (
secretSalt = "my-secret-salt"
)
func main() {
privKeyBytes, err := ioutil.ReadFile("privkey.pem")
if err != nil {
panic(err)
}
block, _ := pem.Decode(privKeyBytes)
if block == nil {
panic("failed to parse private key")
}
privKey, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
panic(err)
}
pubKeyBytes, err := ioutil.ReadFile("pubkey.pem")
if err != nil {
panic(err)
}
block, _ = pem.Decode(pubKeyBytes)
if block == nil {
panic("failed to parse public key")
}
pubKeyStruct, err := x509.ParsePKCS1PublicKey(block.Bytes)
if err != nil {
panic(err)
}
hwid, err := machineid.ProtectedID("")
if err != nil {
panic(err)
}
h := sha256.New()
h.Write([]byte(hwid + secretSalt))
hash := h.Sum(nil)
signature, err := rsa.SignPKCS1v15(nil, privKey, crypto.SHA256, hash)
if err != nil {
panic(err)
}
hashedHwid := base64.StdEncoding.EncodeToString(hash)
sig := base64.StdEncoding.EncodeToString(signature)
pubKey := base64.StdEncoding.EncodeToString(pubKeyStruct.N.Bytes())
pubKeyBytes, err := base64.StdEncoding.DecodeString(pubKey)
if err != nil {
panic(err)
}
pubKeyStruct = rsa.PublicKey{N: new(big.Int).SetBytes(pubKeyBytes), E: 65537}
sigBytes, err := base64.StdEncoding.DecodeString(sig)
if err != nil {
panic(err)
}
err = rsa.VerifyPKCS1v15(&pubKeyStruct, crypto.SHA256, hash, sigBytes)
if err != nil {
fmt.Println("HWID verification failed!")
return
}
clientHwid, err := machineid.ProtectedID("")
if err != nil {
panic(err)
}
clientHwidHash := sha256.Sum256([]byte(clientHwid + secretSalt))
if hashedHwid == base64.StdEncoding.EncodeToString(clientHwidHash[:]) {
fmt.Println("HWID verified!")
} else {
fmt.Println("HWID verification failed!")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment