grafana/pkg/util/encoding.go

74 lines
2.0 KiB
Go
Raw Normal View History

2015-01-08 02:00:00 -06:00
package util
import (
"crypto/rand"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"errors"
"strings"
"golang.org/x/crypto/pbkdf2"
2015-01-08 02:00:00 -06:00
)
pkg/util/*: Add missing function comments. See, $ gometalinter --vendor --deadline 10m --disable-all --enable=golint ./... encoding.go:15:1:warning: comment on exported function GetRandomString should be of the form "GetRandomString ..." (golint) encoding.go:30:1:warning: exported function EncodePassword should have comment or be unexported (golint) encoding.go:35:1:warning: comment on exported function EncodeMd5 should be of the form "EncodeMd5 ..." (golint) encoding.go:42:1:warning: comment on exported function PBKDF2 should be of the form "PBKDF2 ..." (golint) encoding.go:80:1:warning: exported function GetBasicAuthHeader should have comment or be unexported (golint) encoding.go:85:1:warning: exported function DecodeBasicAuthHeader should have comment or be unexported (golint) encoding.go:105:1:warning: exported function RandomHex should have comment or be unexported (golint) encryption.go:14:1:warning: exported function Decrypt should have comment or be unexported (golint) encryption.go:39:1:warning: exported function Encrypt should have comment or be unexported (golint) ip.go:7:1:warning: exported function SplitIpPort should have comment or be unexported (golint) json.go:3:6:warning: exported type DynMap should have comment or be unexported (golint) md5.go:22:1:warning: comment on exported function Md5SumString should be of the form "Md5SumString ..." (golint) strings.go:10:1:warning: exported function StringsFallback2 should have comment or be unexported (golint) strings.go:14:1:warning: exported function StringsFallback3 should have comment or be unexported (golint) strings.go:27:1:warning: exported function SplitString should have comment or be unexported (golint) strings.go:35:1:warning: exported function GetAgeString should have comment or be unexported (golint) url.go:8:6:warning: exported type UrlQueryReader should have comment or be unexported (golint) url.go:12:1:warning: exported function NewUrlQueryReader should have comment or be unexported (golint) url.go:23:1:warning: exported method UrlQueryReader.Get should have comment or be unexported (golint) url.go:32:1:warning: exported function JoinUrlFragments should have comment or be unexported (golint) validation.go:16:1:warning: exported function IsEmail should have comment or be unexported (golint)
2019-01-28 15:09:40 -06:00
// GetRandomString generate random string by specify chars.
2015-01-08 02:00:00 -06:00
// source: https://github.com/gogits/gogs/blob/9ee80e3e5426821f03a4e99fad34418f5c736413/modules/base/tool.go#L58
func GetRandomString(n int, alphabets ...byte) (string, error) {
2015-01-08 02:00:00 -06:00
const alphanum = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
var bytes = make([]byte, n)
if _, err := rand.Read(bytes); err != nil {
return "", err
}
2015-01-08 02:00:00 -06:00
for i, b := range bytes {
if len(alphabets) == 0 {
bytes[i] = alphanum[b%byte(len(alphanum))]
} else {
bytes[i] = alphabets[b%byte(len(alphabets))]
}
}
return string(bytes), nil
2015-01-08 02:00:00 -06:00
}
pkg/util/*: Add missing function comments. See, $ gometalinter --vendor --deadline 10m --disable-all --enable=golint ./... encoding.go:15:1:warning: comment on exported function GetRandomString should be of the form "GetRandomString ..." (golint) encoding.go:30:1:warning: exported function EncodePassword should have comment or be unexported (golint) encoding.go:35:1:warning: comment on exported function EncodeMd5 should be of the form "EncodeMd5 ..." (golint) encoding.go:42:1:warning: comment on exported function PBKDF2 should be of the form "PBKDF2 ..." (golint) encoding.go:80:1:warning: exported function GetBasicAuthHeader should have comment or be unexported (golint) encoding.go:85:1:warning: exported function DecodeBasicAuthHeader should have comment or be unexported (golint) encoding.go:105:1:warning: exported function RandomHex should have comment or be unexported (golint) encryption.go:14:1:warning: exported function Decrypt should have comment or be unexported (golint) encryption.go:39:1:warning: exported function Encrypt should have comment or be unexported (golint) ip.go:7:1:warning: exported function SplitIpPort should have comment or be unexported (golint) json.go:3:6:warning: exported type DynMap should have comment or be unexported (golint) md5.go:22:1:warning: comment on exported function Md5SumString should be of the form "Md5SumString ..." (golint) strings.go:10:1:warning: exported function StringsFallback2 should have comment or be unexported (golint) strings.go:14:1:warning: exported function StringsFallback3 should have comment or be unexported (golint) strings.go:27:1:warning: exported function SplitString should have comment or be unexported (golint) strings.go:35:1:warning: exported function GetAgeString should have comment or be unexported (golint) url.go:8:6:warning: exported type UrlQueryReader should have comment or be unexported (golint) url.go:12:1:warning: exported function NewUrlQueryReader should have comment or be unexported (golint) url.go:23:1:warning: exported method UrlQueryReader.Get should have comment or be unexported (golint) url.go:32:1:warning: exported function JoinUrlFragments should have comment or be unexported (golint) validation.go:16:1:warning: exported function IsEmail should have comment or be unexported (golint)
2019-01-28 15:09:40 -06:00
// EncodePassword encodes a password using PBKDF2.
func EncodePassword(password string, salt string) (string, error) {
newPasswd := pbkdf2.Key([]byte(password), []byte(salt), 10000, 50, sha256.New)
return hex.EncodeToString(newPasswd), nil
}
pkg/util/*: Add missing function comments. See, $ gometalinter --vendor --deadline 10m --disable-all --enable=golint ./... encoding.go:15:1:warning: comment on exported function GetRandomString should be of the form "GetRandomString ..." (golint) encoding.go:30:1:warning: exported function EncodePassword should have comment or be unexported (golint) encoding.go:35:1:warning: comment on exported function EncodeMd5 should be of the form "EncodeMd5 ..." (golint) encoding.go:42:1:warning: comment on exported function PBKDF2 should be of the form "PBKDF2 ..." (golint) encoding.go:80:1:warning: exported function GetBasicAuthHeader should have comment or be unexported (golint) encoding.go:85:1:warning: exported function DecodeBasicAuthHeader should have comment or be unexported (golint) encoding.go:105:1:warning: exported function RandomHex should have comment or be unexported (golint) encryption.go:14:1:warning: exported function Decrypt should have comment or be unexported (golint) encryption.go:39:1:warning: exported function Encrypt should have comment or be unexported (golint) ip.go:7:1:warning: exported function SplitIpPort should have comment or be unexported (golint) json.go:3:6:warning: exported type DynMap should have comment or be unexported (golint) md5.go:22:1:warning: comment on exported function Md5SumString should be of the form "Md5SumString ..." (golint) strings.go:10:1:warning: exported function StringsFallback2 should have comment or be unexported (golint) strings.go:14:1:warning: exported function StringsFallback3 should have comment or be unexported (golint) strings.go:27:1:warning: exported function SplitString should have comment or be unexported (golint) strings.go:35:1:warning: exported function GetAgeString should have comment or be unexported (golint) url.go:8:6:warning: exported type UrlQueryReader should have comment or be unexported (golint) url.go:12:1:warning: exported function NewUrlQueryReader should have comment or be unexported (golint) url.go:23:1:warning: exported method UrlQueryReader.Get should have comment or be unexported (golint) url.go:32:1:warning: exported function JoinUrlFragments should have comment or be unexported (golint) validation.go:16:1:warning: exported function IsEmail should have comment or be unexported (golint)
2019-01-28 15:09:40 -06:00
// GetBasicAuthHeader returns a base64 encoded string from user and password.
func GetBasicAuthHeader(user string, password string) string {
var userAndPass = user + ":" + password
return "Basic " + base64.StdEncoding.EncodeToString([]byte(userAndPass))
}
pkg/util/*: Add missing function comments. See, $ gometalinter --vendor --deadline 10m --disable-all --enable=golint ./... encoding.go:15:1:warning: comment on exported function GetRandomString should be of the form "GetRandomString ..." (golint) encoding.go:30:1:warning: exported function EncodePassword should have comment or be unexported (golint) encoding.go:35:1:warning: comment on exported function EncodeMd5 should be of the form "EncodeMd5 ..." (golint) encoding.go:42:1:warning: comment on exported function PBKDF2 should be of the form "PBKDF2 ..." (golint) encoding.go:80:1:warning: exported function GetBasicAuthHeader should have comment or be unexported (golint) encoding.go:85:1:warning: exported function DecodeBasicAuthHeader should have comment or be unexported (golint) encoding.go:105:1:warning: exported function RandomHex should have comment or be unexported (golint) encryption.go:14:1:warning: exported function Decrypt should have comment or be unexported (golint) encryption.go:39:1:warning: exported function Encrypt should have comment or be unexported (golint) ip.go:7:1:warning: exported function SplitIpPort should have comment or be unexported (golint) json.go:3:6:warning: exported type DynMap should have comment or be unexported (golint) md5.go:22:1:warning: comment on exported function Md5SumString should be of the form "Md5SumString ..." (golint) strings.go:10:1:warning: exported function StringsFallback2 should have comment or be unexported (golint) strings.go:14:1:warning: exported function StringsFallback3 should have comment or be unexported (golint) strings.go:27:1:warning: exported function SplitString should have comment or be unexported (golint) strings.go:35:1:warning: exported function GetAgeString should have comment or be unexported (golint) url.go:8:6:warning: exported type UrlQueryReader should have comment or be unexported (golint) url.go:12:1:warning: exported function NewUrlQueryReader should have comment or be unexported (golint) url.go:23:1:warning: exported method UrlQueryReader.Get should have comment or be unexported (golint) url.go:32:1:warning: exported function JoinUrlFragments should have comment or be unexported (golint) validation.go:16:1:warning: exported function IsEmail should have comment or be unexported (golint)
2019-01-28 15:09:40 -06:00
// DecodeBasicAuthHeader decodes user and password from a basic auth header.
func DecodeBasicAuthHeader(header string) (string, string, error) {
var code string
parts := strings.SplitN(header, " ", 2)
if len(parts) == 2 && parts[0] == "Basic" {
code = parts[1]
}
decoded, err := base64.StdEncoding.DecodeString(code)
if err != nil {
return "", "", err
}
userAndPass := strings.SplitN(string(decoded), ":", 2)
if len(userAndPass) != 2 {
return "", "", errors.New("Invalid basic auth header")
}
return userAndPass[0], userAndPass[1], nil
}
2019-01-15 04:11:32 -06:00
pkg/util/*: Add missing function comments. See, $ gometalinter --vendor --deadline 10m --disable-all --enable=golint ./... encoding.go:15:1:warning: comment on exported function GetRandomString should be of the form "GetRandomString ..." (golint) encoding.go:30:1:warning: exported function EncodePassword should have comment or be unexported (golint) encoding.go:35:1:warning: comment on exported function EncodeMd5 should be of the form "EncodeMd5 ..." (golint) encoding.go:42:1:warning: comment on exported function PBKDF2 should be of the form "PBKDF2 ..." (golint) encoding.go:80:1:warning: exported function GetBasicAuthHeader should have comment or be unexported (golint) encoding.go:85:1:warning: exported function DecodeBasicAuthHeader should have comment or be unexported (golint) encoding.go:105:1:warning: exported function RandomHex should have comment or be unexported (golint) encryption.go:14:1:warning: exported function Decrypt should have comment or be unexported (golint) encryption.go:39:1:warning: exported function Encrypt should have comment or be unexported (golint) ip.go:7:1:warning: exported function SplitIpPort should have comment or be unexported (golint) json.go:3:6:warning: exported type DynMap should have comment or be unexported (golint) md5.go:22:1:warning: comment on exported function Md5SumString should be of the form "Md5SumString ..." (golint) strings.go:10:1:warning: exported function StringsFallback2 should have comment or be unexported (golint) strings.go:14:1:warning: exported function StringsFallback3 should have comment or be unexported (golint) strings.go:27:1:warning: exported function SplitString should have comment or be unexported (golint) strings.go:35:1:warning: exported function GetAgeString should have comment or be unexported (golint) url.go:8:6:warning: exported type UrlQueryReader should have comment or be unexported (golint) url.go:12:1:warning: exported function NewUrlQueryReader should have comment or be unexported (golint) url.go:23:1:warning: exported method UrlQueryReader.Get should have comment or be unexported (golint) url.go:32:1:warning: exported function JoinUrlFragments should have comment or be unexported (golint) validation.go:16:1:warning: exported function IsEmail should have comment or be unexported (golint)
2019-01-28 15:09:40 -06:00
// RandomHex returns a random string from a n seed.
2019-01-15 04:11:32 -06:00
func RandomHex(n int) (string, error) {
bytes := make([]byte, n)
if _, err := rand.Read(bytes); err != nil {
return "", err
}
return hex.EncodeToString(bytes), nil
}