2015-01-08 02:00:00 -06:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
"crypto/sha256"
|
2015-03-02 02:58:35 -06:00
|
|
|
"encoding/base64"
|
2015-01-27 05:05:23 -06:00
|
|
|
"encoding/hex"
|
2015-06-30 02:37:52 -05:00
|
|
|
"errors"
|
|
|
|
"strings"
|
2020-06-29 07:08:32 -05:00
|
|
|
|
|
|
|
"golang.org/x/crypto/pbkdf2"
|
2015-01-08 02:00:00 -06:00
|
|
|
)
|
|
|
|
|
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
|
2019-10-23 03:40:12 -05:00
|
|
|
func GetRandomString(n int, alphabets ...byte) (string, error) {
|
2015-01-08 02:00:00 -06:00
|
|
|
const alphanum = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
|
|
|
|
var bytes = make([]byte, n)
|
2019-10-23 03:40:12 -05:00
|
|
|
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))]
|
|
|
|
}
|
|
|
|
}
|
2019-10-23 03:40:12 -05:00
|
|
|
return string(bytes), nil
|
2015-01-08 02:00:00 -06:00
|
|
|
}
|
|
|
|
|
2019-01-28 15:09:40 -06:00
|
|
|
// EncodePassword encodes a password using PBKDF2.
|
2019-10-23 03:40:12 -05:00
|
|
|
func EncodePassword(password string, salt string) (string, error) {
|
2019-11-06 02:03:10 -06:00
|
|
|
newPasswd := pbkdf2.Key([]byte(password), []byte(salt), 10000, 50, sha256.New)
|
2019-10-23 03:40:12 -05:00
|
|
|
return hex.EncodeToString(newPasswd), nil
|
2015-01-27 05:05:23 -06:00
|
|
|
}
|
|
|
|
|
2019-01-28 15:09:40 -06:00
|
|
|
// GetBasicAuthHeader returns a base64 encoded string from user and password.
|
2015-03-02 02:58:35 -06:00
|
|
|
func GetBasicAuthHeader(user string, password string) string {
|
|
|
|
var userAndPass = user + ":" + password
|
|
|
|
return "Basic " + base64.StdEncoding.EncodeToString([]byte(userAndPass))
|
|
|
|
}
|
2015-06-30 02:37:52 -05:00
|
|
|
|
2019-01-28 15:09:40 -06:00
|
|
|
// DecodeBasicAuthHeader decodes user and password from a basic auth header.
|
2015-06-30 02:37:52 -05:00
|
|
|
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 {
|
2020-11-05 04:29:39 -06:00
|
|
|
return "", "", errors.New("invalid basic auth header")
|
2015-06-30 02:37:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return userAndPass[0], userAndPass[1], nil
|
|
|
|
}
|
2019-01-15 04:11:32 -06:00
|
|
|
|
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
|
|
|
|
}
|