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 ( ...@@ -12,6 +12,7 @@ import (
"strings" "strings"
) )
// GetRandomString generate random string by specify chars.
// source: https://github.com/gogits/gogs/blob/9ee80e3e5426821f03a4e99fad34418f5c736413/modules/base/tool.go#L58 // source: https://github.com/gogits/gogs/blob/9ee80e3e5426821f03a4e99fad34418f5c736413/modules/base/tool.go#L58
func GetRandomString(n int, alphabets ...byte) string { func GetRandomString(n int, alphabets ...byte) string {
const alphanum = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" const alphanum = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
...@@ -27,18 +28,21 @@ func GetRandomString(n int, alphabets ...byte) string { ...@@ -27,18 +28,21 @@ func GetRandomString(n int, alphabets ...byte) string {
return string(bytes) return string(bytes)
} }
// EncodePassword encodes a password using PBKDF2.
func EncodePassword(password string, salt string) string { func EncodePassword(password string, salt string) string {
newPasswd := PBKDF2([]byte(password), []byte(salt), 10000, 50, sha256.New) newPasswd := PBKDF2([]byte(password), []byte(salt), 10000, 50, sha256.New)
return hex.EncodeToString(newPasswd) return hex.EncodeToString(newPasswd)
} }
// Encode string to md5 hex value. // EncodeMd5 encodes a string to md5 hex value.
func EncodeMd5(str string) string { func EncodeMd5(str string) string {
m := md5.New() m := md5.New()
m.Write([]byte(str)) m.Write([]byte(str))
return hex.EncodeToString(m.Sum(nil)) 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 // 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 { func PBKDF2(password, salt []byte, iter, keyLen int, h func() hash.Hash) []byte {
prf := hmac.New(h, password) prf := hmac.New(h, password)
...@@ -77,11 +81,13 @@ func PBKDF2(password, salt []byte, iter, keyLen int, h func() hash.Hash) []byte ...@@ -77,11 +81,13 @@ func PBKDF2(password, salt []byte, iter, keyLen int, h func() hash.Hash) []byte
return dk[:keyLen] return dk[:keyLen]
} }
// GetBasicAuthHeader returns a base64 encoded string from user and password.
func GetBasicAuthHeader(user string, password string) string { func GetBasicAuthHeader(user string, password string) string {
var userAndPass = user + ":" + password var userAndPass = user + ":" + password
return "Basic " + base64.StdEncoding.EncodeToString([]byte(userAndPass)) return "Basic " + base64.StdEncoding.EncodeToString([]byte(userAndPass))
} }
// DecodeBasicAuthHeader decodes user and password from a basic auth header.
func DecodeBasicAuthHeader(header string) (string, string, error) { func DecodeBasicAuthHeader(header string) (string, string, error) {
var code string var code string
parts := strings.SplitN(header, " ", 2) parts := strings.SplitN(header, " ", 2)
...@@ -102,6 +108,7 @@ func DecodeBasicAuthHeader(header string) (string, string, error) { ...@@ -102,6 +108,7 @@ func DecodeBasicAuthHeader(header string) (string, string, error) {
return userAndPass[0], userAndPass[1], nil return userAndPass[0], userAndPass[1], nil
} }
// RandomHex returns a random string from a n seed.
func RandomHex(n int) (string, error) { func RandomHex(n int) (string, error) {
bytes := make([]byte, n) bytes := make([]byte, n)
if _, err := rand.Read(bytes); err != nil { if _, err := rand.Read(bytes); err != nil {
......
...@@ -11,6 +11,7 @@ import ( ...@@ -11,6 +11,7 @@ import (
const saltLength = 8 const saltLength = 8
// Decrypt decrypts a payload with a given secret.
func Decrypt(payload []byte, secret string) ([]byte, error) { func Decrypt(payload []byte, secret string) ([]byte, error) {
salt := payload[:saltLength] salt := payload[:saltLength]
key := encryptionKeyToBytes(secret, string(salt)) key := encryptionKeyToBytes(secret, string(salt))
...@@ -36,6 +37,7 @@ func Decrypt(payload []byte, secret string) ([]byte, error) { ...@@ -36,6 +37,7 @@ func Decrypt(payload []byte, secret string) ([]byte, error) {
return payloadDst, nil return payloadDst, nil
} }
// Encrypt encrypts a payload with a given secret.
func Encrypt(payload []byte, secret string) ([]byte, error) { func Encrypt(payload []byte, secret string) ([]byte, error) {
salt := GetRandomString(saltLength) salt := GetRandomString(saltLength)
......
...@@ -4,6 +4,7 @@ import ( ...@@ -4,6 +4,7 @@ import (
"net" "net"
) )
// SplitIpPort splits the ip string and port.
func SplitIpPort(ipStr string, portDefault string) (ip string, port string, err error) { func SplitIpPort(ipStr string, portDefault string) (ip string, port string, err error) {
ipAddr := net.ParseIP(ipStr) ipAddr := net.ParseIP(ipStr)
......
package util package util
// DynMap defines a dynamic map interface.
type DynMap map[string]interface{} type DynMap map[string]interface{}
...@@ -19,7 +19,7 @@ func Md5Sum(reader io.Reader) (string, error) { ...@@ -19,7 +19,7 @@ func Md5Sum(reader io.Reader) (string, error) {
return returnMD5String, nil return returnMD5String, nil
} }
// Md5Sum calculates the md5sum of a string // Md5SumString calculates the md5sum of a string
func Md5SumString(input string) (string, error) { func Md5SumString(input string) (string, error) {
buffer := strings.NewReader(input) buffer := strings.NewReader(input)
return Md5Sum(buffer) return Md5Sum(buffer)
......
...@@ -7,10 +7,12 @@ import ( ...@@ -7,10 +7,12 @@ import (
"time" "time"
) )
// StringsFallback2 returns the first of two not empty strings.
func StringsFallback2(val1 string, val2 string) string { func StringsFallback2(val1 string, val2 string) string {
return stringsFallback(val1, val2) return stringsFallback(val1, val2)
} }
// StringsFallback3 returns the first of three not empty strings.
func StringsFallback3(val1 string, val2 string, val3 string) string { func StringsFallback3(val1 string, val2 string, val3 string) string {
return stringsFallback(val1, val2, val3) return stringsFallback(val1, val2, val3)
} }
...@@ -24,6 +26,7 @@ func stringsFallback(vals ...string) string { ...@@ -24,6 +26,7 @@ func stringsFallback(vals ...string) string {
return "" return ""
} }
// SplitString splits a string by commas or empty spaces.
func SplitString(str string) []string { func SplitString(str string) []string {
if len(str) == 0 { if len(str) == 0 {
return []string{} return []string{}
...@@ -32,6 +35,7 @@ func SplitString(str string) []string { ...@@ -32,6 +35,7 @@ func SplitString(str string) []string {
return regexp.MustCompile("[, ]+").Split(str, -1) return regexp.MustCompile("[, ]+").Split(str, -1)
} }
// GetAgeString returns a string representing certain time from years to minutes.
func GetAgeString(t time.Time) string { func GetAgeString(t time.Time) string {
if t.IsZero() { if t.IsZero() {
return "?" return "?"
......
...@@ -5,10 +5,12 @@ import ( ...@@ -5,10 +5,12 @@ import (
"strings" "strings"
) )
// UrlQueryReader is a URL query type.
type UrlQueryReader struct { type UrlQueryReader struct {
values url.Values values url.Values
} }
// NewUrlQueryReader parses a raw query and returns it as a UrlQueryReader type.
func NewUrlQueryReader(urlInfo *url.URL) (*UrlQueryReader, error) { func NewUrlQueryReader(urlInfo *url.URL) (*UrlQueryReader, error) {
u, err := url.ParseQuery(urlInfo.RawQuery) u, err := url.ParseQuery(urlInfo.RawQuery)
if err != nil { if err != nil {
...@@ -20,6 +22,8 @@ func NewUrlQueryReader(urlInfo *url.URL) (*UrlQueryReader, error) { ...@@ -20,6 +22,8 @@ func NewUrlQueryReader(urlInfo *url.URL) (*UrlQueryReader, error) {
}, nil }, 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 { func (r *UrlQueryReader) Get(name string, def string) string {
val := r.values[name] val := r.values[name]
if len(val) == 0 { if len(val) == 0 {
...@@ -29,6 +33,7 @@ func (r *UrlQueryReader) Get(name string, def string) string { ...@@ -29,6 +33,7 @@ func (r *UrlQueryReader) Get(name string, def string) string {
return val[0] return val[0]
} }
// JoinUrlFragments joins two URL fragments into only one URL string.
func JoinUrlFragments(a, b string) string { func JoinUrlFragments(a, b string) string {
aslash := strings.HasSuffix(a, "/") aslash := strings.HasSuffix(a, "/")
bslash := strings.HasPrefix(b, "/") bslash := strings.HasPrefix(b, "/")
......
...@@ -13,6 +13,7 @@ var ( ...@@ -13,6 +13,7 @@ var (
regexEmail = regexp.MustCompile(emailRegexPattern) regexEmail = regexp.MustCompile(emailRegexPattern)
) )
// IsEmail checks if a string is a valid email address.
func IsEmail(str string) bool { func IsEmail(str string) bool {
return regexEmail.MatchString(strings.ToLower(str)) 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