1
1
mirror of https://github.com/go-gitea/gitea synced 2025-07-23 02:38:35 +00:00

Hash App token (#6724)

This commit is contained in:
techknowlogick
2019-05-04 11:45:34 -04:00
committed by GitHub
parent 1fa9662946
commit 46373e7657
16 changed files with 239 additions and 44 deletions

View File

@@ -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 {

View File

@@ -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"))
}