Skip to content

Commit

Permalink
perf(gravatar): use sha256 hashing algorithm for gravatar (#874)
Browse files Browse the repository at this point in the history
  • Loading branch information
qwqcode committed May 22, 2024
1 parent bb218d0 commit 53d46d8
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
4 changes: 2 additions & 2 deletions internal/dao/cook.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (dao *Dao) CookComment(c *entity.Comment) entity.CookedComment {
ContentMarked: markedContent,
UserID: c.UserID,
Nick: user.Name,
EmailEncrypted: utils.GetMD5Hash(user.Email),
EmailEncrypted: utils.GetSha256Hash(user.Email),
Link: user.Link,
UA: c.UA,
Date: c.CreatedAt.Local().Format("2006-01-02 15:04:05"),
Expand Down Expand Up @@ -88,7 +88,7 @@ func (dao *Dao) CookCommentForEmail(c *entity.Comment) entity.CookedCommentForEm
Site: dao.CookSite(&site),
CookedComment: entity.CookedComment{
ID: c.ID,
EmailEncrypted: utils.GetMD5Hash(user.Email),
EmailEncrypted: utils.GetSha256Hash(user.Email),
Link: user.Link,
UA: c.UA,
IsCollapsed: c.IsCollapsed,
Expand Down
7 changes: 7 additions & 0 deletions internal/utils/encrypt.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package utils

import (
"crypto/md5"
"crypto/sha256"
"encoding/hex"
)

Expand All @@ -10,3 +11,9 @@ func GetMD5Hash(text string) string {
hasher.Write([]byte(text))
return hex.EncodeToString(hasher.Sum(nil))
}

func GetSha256Hash(text string) string {
hasher := sha256.New()
hasher.Write([]byte(text))
return hex.EncodeToString(hasher.Sum(nil))
}
16 changes: 16 additions & 0 deletions internal/utils/encrypt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,19 @@ func TestGetMD5Hash(t *testing.T) {
assert.Equal(t, test.expected, result)
}
}

func TestGetSha256Hash(t *testing.T) {
tests := []struct {
input string
expected string
}{
{"hello", "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"},
{"world", "486ea46224d1bb4fb680f34f7c9ad96a8f24ec88be73ea8e5a6c65260e9cb8a7"},
{"", "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"},
}

for _, test := range tests {
result := GetSha256Hash(test.input)
assert.Equal(t, test.expected, result)
}
}

0 comments on commit 53d46d8

Please sign in to comment.