mirror of
https://github.com/pomerium/pomerium.git
synced 2025-04-29 18:36:30 +02:00
* wip * storage: add filtering to SyncLatest * don't increment the record version, so intermediate changes are requested * databroker: add support for query filtering * fill server and record version * postgres: databroker storage backend * wip * serialize puts * add test * skip tests for macos * add test * return error from protojson * set data * exclude postgres from cover tests
60 lines
1.3 KiB
Go
60 lines
1.3 KiB
Go
package cryptutil
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"encoding/base64"
|
|
"encoding/binary"
|
|
)
|
|
|
|
// DefaultKeySize is the default key size in bytes.
|
|
const DefaultKeySize = 32
|
|
|
|
// NewKey generates a random 32-byte (256 bit) key.
|
|
//
|
|
// Panics if source of randomness fails.
|
|
func NewKey() []byte {
|
|
return randomBytes(DefaultKeySize)
|
|
}
|
|
|
|
// NewBase64Key generates a random base64 encoded 32-byte key.
|
|
//
|
|
// Panics if source of randomness fails.
|
|
func NewBase64Key() string {
|
|
return NewRandomStringN(DefaultKeySize)
|
|
}
|
|
|
|
// NewRandomStringN returns base64 encoded random string of a given num of bytes.
|
|
//
|
|
// Panics if source of randomness fails.
|
|
func NewRandomStringN(c int) string {
|
|
return base64.StdEncoding.EncodeToString(randomBytes(c))
|
|
}
|
|
|
|
// NewRandomUInt32 returns a random uint32.
|
|
//
|
|
// Panics if source of randomness fails.
|
|
func NewRandomUInt32() uint32 {
|
|
return binary.LittleEndian.Uint32(randomBytes(4))
|
|
}
|
|
|
|
// NewRandomUInt64 returns a random uint64.
|
|
//
|
|
// Panics if source of randomness fails.
|
|
func NewRandomUInt64() uint64 {
|
|
return binary.LittleEndian.Uint64(randomBytes(8))
|
|
}
|
|
|
|
// randomBytes generates C number of random bytes suitable for cryptographic
|
|
// operations.
|
|
//
|
|
// Panics if source of randomness fails.
|
|
func randomBytes(c int) []byte {
|
|
if c < 0 {
|
|
c = DefaultKeySize
|
|
}
|
|
b := make([]byte, c)
|
|
if _, err := rand.Read(b); err != nil {
|
|
panic(err)
|
|
}
|
|
return b
|
|
}
|