summaryrefslogtreecommitdiffstats
path: root/modules/base
diff options
context:
space:
mode:
authortechknowlogick <techknowlogick@gitea.io>2019-05-04 11:45:34 -0400
committerGitHub <noreply@github.com>2019-05-04 11:45:34 -0400
commit46373e765702a203bfcb576c9d3639314ef4965f (patch)
treea44ba4de8593a9ad7898683e0e6df80e0072ef17 /modules/base
parent1fa96629461ac4229932b0a4526fc2f60c88ec51 (diff)
downloadgitea-46373e765702a203bfcb576c9d3639314ef4965f.tar.gz
gitea-46373e765702a203bfcb576c9d3639314ef4965f.zip
Hash App token (#6724)
Diffstat (limited to 'modules/base')
-rw-r--r--modules/base/tool.go8
-rw-r--r--modules/base/tool_test.go7
2 files changed, 15 insertions, 0 deletions
diff --git a/modules/base/tool.go b/modules/base/tool.go
index d99ed2bab4..3a6e28a885 100644
--- a/modules/base/tool.go
+++ b/modules/base/tool.go
@@ -9,6 +9,7 @@ import (
"crypto/md5"
"crypto/rand"
"crypto/sha1"
+ "crypto/sha256"
"encoding/base64"
"encoding/hex"
"fmt"
@@ -54,6 +55,13 @@ func EncodeSha1(str string) string {
return hex.EncodeToString(h.Sum(nil))
}
+// EncodeSha256 string to sha1 hex value.
+func EncodeSha256(str string) string {
+ h := sha256.New()
+ h.Write([]byte(str))
+ return hex.EncodeToString(h.Sum(nil))
+}
+
// ShortSha is basically just truncating.
// It is DEPRECATED and will be removed in the future.
func ShortSha(sha1 string) string {
diff --git a/modules/base/tool_test.go b/modules/base/tool_test.go
index 04cd682907..dcaf2fcbb0 100644
--- a/modules/base/tool_test.go
+++ b/modules/base/tool_test.go
@@ -53,6 +53,13 @@ func TestEncodeSha1(t *testing.T) {
)
}
+func TestEncodeSha256(t *testing.T) {
+ assert.Equal(t,
+ "c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2",
+ EncodeSha256("foobar"),
+ )
+}
+
func TestShortSha(t *testing.T) {
assert.Equal(t, "veryverylo", ShortSha("veryverylong"))
}