Merge pull request #79 from harshavardhana/pr_out_add_a_new_pkg_crypto_keys_first_cut

This commit is contained in:
Harshavardhana 2015-01-28 12:03:28 -08:00
commit faf6835871
2 changed files with 72 additions and 0 deletions

View 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'
}

View 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
}