mirror of
https://github.com/minio/minio.git
synced 2024-12-25 22:55:54 -05:00
Add a new pkg/crypto/keys, first cut
- provides three functions - GetRandomAlphaNumeric() - GetRandomAlphaNumericFull() - GetRandomBase64() - ValidAccessKey()
This commit is contained in:
parent
cc54730eee
commit
fbafc98edb
10
pkg/utils/crypto/keys/common.go
Normal file
10
pkg/utils/crypto/keys/common.go
Normal file
@ -0,0 +1,10 @@
|
||||
package keys
|
||||
|
||||
const (
|
||||
MINIO_ACCESS_ID = 20
|
||||
MINIO_SECRET_ID = 40
|
||||
)
|
||||
|
||||
func isalnum(c byte) bool {
|
||||
return '0' <= c && c <= '9' || 'A' <= c && c <= 'Z' || 'a' <= c && c <= 'z'
|
||||
}
|
62
pkg/utils/crypto/keys/keys.go
Normal file
62
pkg/utils/crypto/keys/keys.go
Normal file
@ -0,0 +1,62 @@
|
||||
package keys
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
)
|
||||
|
||||
var alphaNumericTable = []byte("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ")
|
||||
var alphaNumericTableFull = []byte("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")
|
||||
|
||||
func GetRandomAlphaNumeric(size int) ([]byte, error) {
|
||||
alpha := make([]byte, size)
|
||||
_, err := rand.Read(alpha)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for i := 0; i < size; i++ {
|
||||
alpha[i] = alphaNumericTable[alpha[i]%byte(len(alphaNumericTable))]
|
||||
}
|
||||
return alpha, nil
|
||||
}
|
||||
|
||||
func GetRandomAlphaNumericFull(size int) ([]byte, error) {
|
||||
alphaFull := make([]byte, size)
|
||||
_, err := rand.Read(alphaFull)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for i := 0; i < size; i++ {
|
||||
alphaFull[i] = alphaNumericTableFull[alphaFull[i]%byte(len(alphaNumericTableFull))]
|
||||
}
|
||||
return alphaFull, nil
|
||||
}
|
||||
|
||||
func GetRandomBase64(size int) ([]byte, error) {
|
||||
rb := make([]byte, size)
|
||||
n, err := rand.Read(rb)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
dest := make([]byte, n)
|
||||
base64.URLEncoding.EncodeTo(dest, rb)
|
||||
return dest, nil
|
||||
}
|
||||
|
||||
func ValidateAccessKey(key []byte) bool {
|
||||
for _, char := range key {
|
||||
if isalnum(char) {
|
||||
continue
|
||||
}
|
||||
switch char {
|
||||
case '-':
|
||||
case '.':
|
||||
case '_':
|
||||
case '~':
|
||||
continue
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
Loading…
Reference in New Issue
Block a user