Commit 5f6383a7 by Mario Trangoni

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)
parent f09c4608
......@@ -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 {
......
......@@ -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)
......
......@@ -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)
......
package util
// DynMap defines a dynamic map interface.
type DynMap map[string]interface{}
......@@ -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)
......
......@@ -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 "?"
......
......@@ -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, "/")
......
......@@ -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))
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment