Skip to content

Instantly share code, notes, and snippets.

View linux4life798's full-sized avatar

Craig Hesling linux4life798

View GitHub Profile
@linux4life798
linux4life798 / generate_mqtt_client_id.go
Created July 10, 2018 18:08
Function to generate a random MQTT client ID with a common prefix
import CRAND "crypto/rand"
// GenMQTTClientID generates a random client id for mqtt
func GenMQTTClientID(prefix string) (string, error) {
r, err := CRAND.Int(CRAND.Reader, new(big.Int).SetInt64(100000))
if err != nil {
return "", fmt.Errorf("Failed to generate MQTT client ID: %v", err)
}
return prefix + r.String(), nil
}
@linux4life798
linux4life798 / isValidHex.go
Created May 31, 2017 14:55
Validate large HEX strings in Go
// isValidHex indicates if value is a proper hex strings that can be contained
// with the given number of bits
func isValidHex(value string, bits int) bool {
str := strings.ToLower(value)
precZeros := true
bitcount := 0
for _, c := range str {
// Ensure the rune is a HEX character
if !strings.Contains("0123456789abcdef", string(c)) {
return false