basic common app packages
This commit is contained in:
24
crypto/random.go
Normal file
24
crypto/random.go
Normal file
@@ -0,0 +1,24 @@
|
||||
// Copyright 2024 Patial Tech (Ankit Patial).
|
||||
// All rights reserved.
|
||||
|
||||
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
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user