appcore/crypto/random.go
2025-06-16 22:26:47 +05:30

27 lines
606 B
Go

// Copyright 2024 Patial Tech (Ankit Patial).
//
// This file is part of code.patial.tech/go/appcore, which is MIT licensed.
// See http://opensource.org/licenses/MIT
package crypto
import (
"crypto/rand"
"encoding/hex"
)
// RandomBytes generates totally random bytes, each byte is generated by [0,255] range
func RandomBytes(length uint8) ([]byte, error) {
buf := make([]byte, length)
_, err := rand.Read(buf)
return buf, err
}
func RandomBytesHex(length uint8) (string, error) {
if b, err := RandomBytes(length); err != nil {
return "", err
} else {
return hex.EncodeToString(b), nil
}
}