Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: argon2id configuration override #96

Merged
merged 2 commits into from
Apr 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions hash/application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ func (s *ApplicationTestSuite) TestCheckHash() {
}
}

func (s *ApplicationTestSuite) TestConfigurationOverride() {
value := "$argon2id$v=19$m=65536,t=8,p=1$NlVjQm5PQUdWTHVTM1RBUg$Q5T7WfeCI7ucIdk6Na6AdQ"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我们可以根据你自定义配置的发生问题的过程生成这个加密串么?而不是固定写死,这样测试是不是不太能覆盖到问题?

Copy link
Member Author

@devhaozi devhaozi Apr 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我们可以根据你自定义配置的发生问题的过程生成这个加密串么?而不是固定写死,这样测试是不是不太能覆盖到问题?

问题的哈希是从数据库取出来的,因为是其他程序生成的,所以参数不一样。

s.True(s.hashers["argon2id"].Check("goravel", value))
s.True(s.hashers["argon2id"].NeedsRehash(value))
}

func (s *ApplicationTestSuite) TestNeedsRehash() {
for name, hasher := range s.hashers {
s.Run(name, func() {
Expand Down
14 changes: 9 additions & 5 deletions hash/argon2id.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type Argon2id struct {
func NewArgon2id() *Argon2id {
return &Argon2id{
format: "$argon2id$v=%d$m=%d,t=%d,p=%d$%s$%s",
version: 19,
version: argon2.Version,
time: uint32(facades.Config.GetInt("hashing.argon2id.time", 4)),
memory: uint32(facades.Config.GetInt("hashing.argon2id.memory", 65536)),
threads: uint8(facades.Config.GetInt("hashing.argon2id.threads", 1)),
Expand Down Expand Up @@ -64,11 +64,15 @@ func (a *Argon2id) Check(value, hash string) bool {
if err != nil {
return false
}
if version != argon2.Version {
if version != a.version {
return false
}

_, err = fmt.Sscanf(hashParts[3], "m=%d,t=%d,p=%d", &a.memory, &a.time, &a.threads)
memory := a.memory
time := a.time
threads := a.threads

_, err = fmt.Sscanf(hashParts[3], "m=%d,t=%d,p=%d", &memory, &time, &threads)
if err != nil {
return false
}
Expand All @@ -83,7 +87,7 @@ func (a *Argon2id) Check(value, hash string) bool {
return false
}

hashToCompare := argon2.IDKey([]byte(value), salt, a.time, a.memory, a.threads, uint32(len(decodedHash)))
hashToCompare := argon2.IDKey([]byte(value), salt, time, memory, threads, uint32(len(decodedHash)))

return subtle.ConstantTimeCompare(decodedHash, hashToCompare) == 1
}
Expand All @@ -99,7 +103,7 @@ func (a *Argon2id) NeedsRehash(hash string) bool {
if err != nil {
return true
}
if version != argon2.Version {
if version != a.version {
return true
}

Expand Down