diff --git a/pkg/util/encoding.go b/pkg/util/encoding.go index e82344d73f9..c8c3d72e6d7 100644 --- a/pkg/util/encoding.go +++ b/pkg/util/encoding.go @@ -12,6 +12,7 @@ import ( "strings" ) +// GetRandomString generate random string by specify chars. // source: https://github.com/gogits/gogs/blob/9ee80e3e5426821f03a4e99fad34418f5c736413/modules/base/tool.go#L58 func GetRandomString(n int, alphabets ...byte) string { const alphanum = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" @@ -27,18 +28,21 @@ func GetRandomString(n int, alphabets ...byte) string { return string(bytes) } +// EncodePassword encodes a password using PBKDF2. func EncodePassword(password string, salt string) string { newPasswd := PBKDF2([]byte(password), []byte(salt), 10000, 50, sha256.New) return hex.EncodeToString(newPasswd) } -// Encode string to md5 hex value. +// EncodeMd5 encodes a string to md5 hex value. func EncodeMd5(str string) string { m := md5.New() m.Write([]byte(str)) return hex.EncodeToString(m.Sum(nil)) } +// PBKDF2 implements Password-Based Key Derivation Function 2), aimed to reduce +// the vulnerability of encrypted keys to brute force attacks. // http://code.google.com/p/go/source/browse/pbkdf2/pbkdf2.go?repo=crypto func PBKDF2(password, salt []byte, iter, keyLen int, h func() hash.Hash) []byte { prf := hmac.New(h, password) @@ -77,11 +81,13 @@ func PBKDF2(password, salt []byte, iter, keyLen int, h func() hash.Hash) []byte return dk[:keyLen] } +// 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)) } +// 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) @@ -102,6 +108,7 @@ func DecodeBasicAuthHeader(header string) (string, string, error) { return userAndPass[0], userAndPass[1], nil } +// RandomHex returns a random string from a n seed. func RandomHex(n int) (string, error) { bytes := make([]byte, n) if _, err := rand.Read(bytes); err != nil { diff --git a/pkg/util/encryption.go b/pkg/util/encryption.go index d41665bafd6..6580e049206 100644 --- a/pkg/util/encryption.go +++ b/pkg/util/encryption.go @@ -11,6 +11,7 @@ import ( const saltLength = 8 +// Decrypt decrypts a payload with a given secret. func Decrypt(payload []byte, secret string) ([]byte, error) { salt := payload[:saltLength] key := encryptionKeyToBytes(secret, string(salt)) @@ -36,6 +37,7 @@ func Decrypt(payload []byte, secret string) ([]byte, error) { return payloadDst, nil } +// Encrypt encrypts a payload with a given secret. func Encrypt(payload []byte, secret string) ([]byte, error) { salt := GetRandomString(saltLength) diff --git a/pkg/util/ip.go b/pkg/util/ip.go index 351abd7a03b..1f53a5fb8b0 100644 --- a/pkg/util/ip.go +++ b/pkg/util/ip.go @@ -4,6 +4,7 @@ import ( "net" ) +// SplitIpPort splits the ip string and port. func SplitIpPort(ipStr string, portDefault string) (ip string, port string, err error) { ipAddr := net.ParseIP(ipStr) diff --git a/pkg/util/json.go b/pkg/util/json.go index a75a8490d93..7a2ec53cc2a 100644 --- a/pkg/util/json.go +++ b/pkg/util/json.go @@ -1,3 +1,4 @@ package util +// DynMap defines a dynamic map interface. type DynMap map[string]interface{} diff --git a/pkg/util/md5.go b/pkg/util/md5.go index 2473a1a406c..99641ca398f 100644 --- a/pkg/util/md5.go +++ b/pkg/util/md5.go @@ -19,7 +19,7 @@ func Md5Sum(reader io.Reader) (string, error) { return returnMD5String, nil } -// Md5Sum calculates the md5sum of a string +// Md5SumString calculates the md5sum of a string func Md5SumString(input string) (string, error) { buffer := strings.NewReader(input) return Md5Sum(buffer) diff --git a/pkg/util/strings.go b/pkg/util/strings.go index 854c009d1b1..9eaa141edbf 100644 --- a/pkg/util/strings.go +++ b/pkg/util/strings.go @@ -7,10 +7,12 @@ import ( "time" ) +// StringsFallback2 returns the first of two not empty strings. func StringsFallback2(val1 string, val2 string) string { return stringsFallback(val1, val2) } +// StringsFallback3 returns the first of three not empty strings. func StringsFallback3(val1 string, val2 string, val3 string) string { return stringsFallback(val1, val2, val3) } @@ -24,6 +26,7 @@ func stringsFallback(vals ...string) string { return "" } +// SplitString splits a string by commas or empty spaces. func SplitString(str string) []string { if len(str) == 0 { return []string{} @@ -32,6 +35,7 @@ func SplitString(str string) []string { return regexp.MustCompile("[, ]+").Split(str, -1) } +// GetAgeString returns a string representing certain time from years to minutes. func GetAgeString(t time.Time) string { if t.IsZero() { return "?" diff --git a/pkg/util/url.go b/pkg/util/url.go index fad2d79a6d0..9d2a89d2522 100644 --- a/pkg/util/url.go +++ b/pkg/util/url.go @@ -5,10 +5,12 @@ import ( "strings" ) +// UrlQueryReader is a URL query type. type UrlQueryReader struct { values url.Values } +// NewUrlQueryReader parses a raw query and returns it as a UrlQueryReader type. func NewUrlQueryReader(urlInfo *url.URL) (*UrlQueryReader, error) { u, err := url.ParseQuery(urlInfo.RawQuery) if err != nil { @@ -20,6 +22,8 @@ func NewUrlQueryReader(urlInfo *url.URL) (*UrlQueryReader, error) { }, nil } +// Get parse parameters from an URL. If the parameter does not exist, it returns +// the default value. func (r *UrlQueryReader) Get(name string, def string) string { val := r.values[name] if len(val) == 0 { @@ -29,6 +33,7 @@ func (r *UrlQueryReader) Get(name string, def string) string { return val[0] } +// JoinUrlFragments joins two URL fragments into only one URL string. func JoinUrlFragments(a, b string) string { aslash := strings.HasSuffix(a, "/") bslash := strings.HasPrefix(b, "/") diff --git a/pkg/util/validation.go b/pkg/util/validation.go index dd7404e6de4..88f92fdec21 100644 --- a/pkg/util/validation.go +++ b/pkg/util/validation.go @@ -13,6 +13,7 @@ var ( regexEmail = regexp.MustCompile(emailRegexPattern) ) +// IsEmail checks if a string is a valid email address. func IsEmail(str string) bool { return regexEmail.MatchString(strings.ToLower(str)) }