aboutsummaryrefslogtreecommitdiff
path: root/pkg
diff options
context:
space:
mode:
Diffstat (limited to 'pkg')
-rw-r--r--pkg/markup/markdown.go2
-rw-r--r--pkg/markup/markup.go2
-rw-r--r--pkg/template/template.go6
-rw-r--r--pkg/tool/tool.go26
4 files changed, 21 insertions, 15 deletions
diff --git a/pkg/markup/markdown.go b/pkg/markup/markdown.go
index fc2f54c6..43f58806 100644
--- a/pkg/markup/markdown.go
+++ b/pkg/markup/markdown.go
@@ -72,7 +72,7 @@ func (r *MarkdownRenderer) AutoLink(out *bytes.Buffer, link []byte, kind int) {
if j == -1 {
j = len(m)
}
- out.WriteString(fmt.Sprintf(` <code><a href="%s">%s</a></code>`, m, tool.ShortSha(string(m[i+7:j]))))
+ out.WriteString(fmt.Sprintf(` <code><a href="%s">%s</a></code>`, m, tool.ShortSHA1(string(m[i+7:j]))))
return
}
diff --git a/pkg/markup/markup.go b/pkg/markup/markup.go
index 98742540..c5549b6c 100644
--- a/pkg/markup/markup.go
+++ b/pkg/markup/markup.go
@@ -140,7 +140,7 @@ func RenderSha1CurrentPattern(rawBytes []byte, urlPrefix string) []byte {
if com.StrTo(m).MustInt() > 0 {
return m
}
- return fmt.Sprintf(`<a href="%s/commit/%s"><code>%s</code></a>`, urlPrefix, m, tool.ShortSha(string(m)))
+ return fmt.Sprintf(`<a href="%s/commit/%s"><code>%s</code></a>`, urlPrefix, m, tool.ShortSHA1(string(m)))
}))
}
diff --git a/pkg/template/template.go b/pkg/template/template.go
index 55bc8764..dd29ccc4 100644
--- a/pkg/template/template.go
+++ b/pkg/template/template.go
@@ -96,8 +96,8 @@ func NewFuncMap() []template.FuncMap {
"DiffTypeToStr": DiffTypeToStr,
"DiffLineTypeToStr": DiffLineTypeToStr,
"Sha1": Sha1,
- "ShortSha": tool.ShortSha,
- "MD5": tool.EncodeMD5,
+ "ShortSHA1": tool.ShortSHA1,
+ "MD5": tool.MD5,
"ActionContent2Commits": ActionContent2Commits,
"EscapePound": EscapePound,
"RenderCommitMessage": RenderCommitMessage,
@@ -142,7 +142,7 @@ func List(l *list.List) chan interface{} {
}
func Sha1(str string) string {
- return tool.EncodeSha1(str)
+ return tool.SHA1(str)
}
func ToUTF8WithErr(content []byte) (error, string) {
diff --git a/pkg/tool/tool.go b/pkg/tool/tool.go
index 167edbb6..4a5532a3 100644
--- a/pkg/tool/tool.go
+++ b/pkg/tool/tool.go
@@ -34,28 +34,30 @@ func MD5Bytes(str string) []byte {
return m.Sum(nil)
}
-// EncodeMD5 encodes string to MD5 hex value.
-func EncodeMD5(str string) string {
+// MD5 encodes string to MD5 hex value.
+func MD5(str string) string {
return hex.EncodeToString(MD5Bytes(str))
}
-// Encode string to sha1 hex value.
-func EncodeSha1(str string) string {
+// SHA1 encodes string to SHA1 hex value.
+func SHA1(str string) string {
h := sha1.New()
h.Write([]byte(str))
return hex.EncodeToString(h.Sum(nil))
}
-func ShortSha(sha1 string) string {
+// ShortSHA1 truncates SHA1 string length to at most 10.
+func ShortSHA1(sha1 string) string {
if len(sha1) > 10 {
return sha1[:10]
}
return sha1
}
+// DetectEncoding returns best guess of encoding of given content.
func DetectEncoding(content []byte) (string, error) {
if utf8.Valid(content) {
- log.Trace("Detected encoding: utf-8 (fast)")
+ log.Trace("Detected encoding: UTF-8 (fast)")
return "UTF-8", nil
}
@@ -69,6 +71,8 @@ func DetectEncoding(content []byte) (string, error) {
return result.Charset, err
}
+// BasicAuthDecode decodes username and password portions of HTTP Basic Authentication
+// from encoded content.
func BasicAuthDecode(encoded string) (string, string, error) {
s, err := base64.StdEncoding.DecodeString(encoded)
if err != nil {
@@ -79,14 +83,16 @@ func BasicAuthDecode(encoded string) (string, string, error) {
return auth[0], auth[1], nil
}
+// BasicAuthEncode encodes username and password in HTTP Basic Authentication format.
func BasicAuthEncode(username, password string) string {
return base64.StdEncoding.EncodeToString([]byte(username + ":" + password))
}
-// GetRandomString generate random string by specify chars.
-func GetRandomString(n int) (string, error) {
- const alphanum = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
+const alphanum = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
+// RandomString returns generated random string in given length of characters.
+// It also returns possible error during generation.
+func RandomString(n int) (string, error) {
buffer := make([]byte, n)
max := big.NewInt(int64(len(alphanum)))
@@ -138,7 +144,7 @@ func VerifyTimeLimitCode(data string, minutes int, code string) bool {
return false
}
-const TimeLimitCodeLength = 12 + 6 + 40
+const TIME_LIMIT_CODE_LENGTH = 12 + 6 + 40
// create a time limit code
// code format: 12 length date time string + 6 minutes string + 40 sha1 encoded string