Skip to content

Commit

Permalink
feat(argon2): argon2 support
Browse files Browse the repository at this point in the history
  • Loading branch information
aldy505 committed May 28, 2021
1 parent 9e4ba80 commit 5837d07
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 9 deletions.
15 changes: 10 additions & 5 deletions argon2/argon2.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ func Hash(plain string, config Config) (string, error) {
ID: "argon2" + config.Variant,
Version: version,
Params: map[string]interface{}{
"m": strconv.Itoa(int(config.Memory)),
"t": strconv.Itoa(int(config.Time)),
"p": string(config.Parallelism),
"m": int(config.Memory),
"t": int(config.Time),
"p": int(config.Parallelism),
},
Salt: hex.EncodeToString(salt),
Hash: hex.EncodeToString(hash),
Expand Down Expand Up @@ -88,10 +88,15 @@ func Verify(hash string, plain string) (bool, error) {
return false, err
}

salt, err := hex.DecodeString(deserialize.Salt)
if err != nil {
return false, err
}

if deserialize.ID == "argon2id" {
verifyHash = argon2.IDKey([]byte(plain), []byte(deserialize.Salt), uint32(time), uint32(memory), uint8(parallelism), keyLen)
verifyHash = argon2.IDKey([]byte(plain), salt, uint32(time), uint32(memory), uint8(parallelism), keyLen)
} else if deserialize.ID == "argon2i" {
verifyHash = argon2.Key([]byte(plain), []byte(deserialize.Salt), uint32(time), uint32(memory), uint8(parallelism), keyLen)
verifyHash = argon2.Key([]byte(plain), salt, uint32(time), uint32(memory), uint8(parallelism), keyLen)
}

if subtle.ConstantTimeCompare(verifyHash, decodedHash) == 1 {
Expand Down
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,3 @@ module github.com/aldy505/phc-crypto
go 1.16

require golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a

require golang.org/x/sys v0.0.0-20210514084401-e8d321eab015 // indirect
3 changes: 1 addition & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a h1:kr2P4QFmQr29mSLA43kwrOcgcReGTfbE9N577tCTuBc=
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a/go.mod h1:P+XmwS30IXTQdn5tA2iutPOUgjI07+tq3H3K9MVA1s8=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 h1:nxC68pudNYkKU6jWhgrqdreuFiOQWj1Fs7T3VrH4Pjw=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210514084401-e8d321eab015 h1:hZR0X1kPW+nwyJ9xRxqZk1vx5RUObAPBdKVvXPDUH/E=
golang.org/x/sys v0.0.0-20210514084401-e8d321eab015/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
60 changes: 60 additions & 0 deletions phc-crypto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,66 @@ func TestPHCCrypto(t *testing.T) {
}
})
})
t.Run("argon2 test", func(t *testing.T) {
t.Run("should be ok without additional config", func(t *testing.T) {
crypto, err := phccrypto.Use("argon2", phccrypto.Config{})
if err != nil {
t.Error(err)
}
hash, err := crypto.Hash("password123")
if err != nil {
t.Error(err)
}
t.Log(hash)
typeof := reflect.TypeOf(hash).Kind()
if typeof != reflect.String {
t.Error("returned type is not string")
}
})
t.Run("verify should return true", func(t *testing.T) {
crypto, err := phccrypto.Use("argon2", phccrypto.Config{})
if err != nil {
t.Error(err)
}
hash, err := crypto.Hash("password123")
t.Log(hash)
if err != nil {
t.Error(err)
}
verify, err := crypto.Verify(hash, "password123")
if err != nil {
t.Error(err)
}
typeof := reflect.TypeOf(verify).Kind()
if typeof != reflect.Bool {
t.Error("returned type is not boolean")
}
if !verify {
t.Error("verify function returned false")
}
})
t.Run("verify should return false", func(t *testing.T) {
crypto, err := phccrypto.Use("argon2", phccrypto.Config{})
if err != nil {
t.Error(err)
}
hash, err := crypto.Hash("password123")
if err != nil {
t.Error(err)
}
verify, err := crypto.Verify(hash, "password321")
if err != nil {
t.Error(err)
}
typeof := reflect.TypeOf(verify).Kind()
if typeof != reflect.Bool {
t.Error("returned type is not boolean")
}
if verify {
t.Error("verify function returned false")
}
})
})
t.Run("bcrypt test", func(t *testing.T) {
t.Run("should be ok without additional config", func(t *testing.T) {
crypto, err := phccrypto.Use("bcrypt", phccrypto.Config{})
Expand Down

0 comments on commit 5837d07

Please sign in to comment.