appcore/crypto/random.go

27 lines
606 B
Go
Raw Normal View History

2025-06-16 22:19:00 +05:30
// Copyright 2024 Patial Tech (Ankit Patial).
2025-06-16 22:26:47 +05:30
//
// This file is part of code.patial.tech/go/appcore, which is MIT licensed.
// See http://opensource.org/licenses/MIT
2025-06-16 22:19:00 +05:30
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
}
}